Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Component, Inject, OnInit} from '@angular/core';
- import { Event } from './event.model';
- import { Comment } from './comment.model';
- import { UnivDetails } from './univDetails.model';
- import { ActivatedRoute, Router } from '@angular/router';
- import { UserService } from '../service/dataaccess.service';
- @Component({
- selector: 'app-events',
- templateUrl: './events.component.html',
- styleUrls: ['./events.component.css'],
- providers: [UserService]
- })
- export class EventsComponent implements OnInit {
- universityName = 'UCF';
- universityInput = '';
- userService: any;
- isSuperAdmin = false;
- isLoggedIn : boolean;
- comment = '';
- rating = '';
- // Map default location of UCF
- lat = 28.602427;
- lng = -81.2000599;
- // Sets the default zoom Level.
- zoom = 15;
- // Holds the marker's name.
- markerName: string;
- // Marker array that will hold the markers on the map.
- markers: Marker[] = [];
- listOfComments: Comment[];
- // Array to store all the event objects.
- listOfEvents: Event[];
- selectedEvent: Event;
- univDetails: UnivDetails;
- constructor(private route: ActivatedRoute, private router: Router, @Inject(UserService) userService) {
- this.userService = userService;
- }
- /**
- * Every time the page is loaded, it checks if a key is stored in the session storage.
- * It also loads public events for the University of Central Florida.
- */
- ngOnInit() {
- // If an auth key exists in the session storage, then the user must be
- // logged in
- this.isLoggedIn = (sessionStorage.getItem('auth_key') !== null);
- // Gets university location and events.
- this.getUniversityDetails();
- this.getUniversityAboutInfo();
- console.log(this.isSuperAdmin);
- }
- onSelect(event: Event): void {
- console.log(event.eventID);
- const token = sessionStorage.getItem('auth_key');
- this.selectedEvent = event;
- this.userService.getComments({event_id: this.selectedEvent.eventID}, token).subscribe(response => {
- this.displayComments(response = response.json());
- });
- }
- displayComments(response) {
- this.listOfComments = [];
- console.log(response);
- for (let i = 0; i < response.length; i++) {
- this.listOfComments.push(new Comment(
- response[i].comID,
- response[i].canEdit,
- response[i].commentText,
- response[i].commentStars
- ));
- }
- }
- deleteComments(comment) {
- console.log('hi');
- this.userService.deleteComments({comment_id: comment.comID}).subscribe(response => {
- console.log(response)
- window.location.reload()
- })
- }
- // Method that handles when a marker is clicked.
- // TODO: Highlight the event that corresponds to the marker.
- clickedMarker(marker: Marker, index: number) {
- console.log('Clicked Marker: ' + marker.name + 'at index ' + index)
- }
- // Creates a new marker where the map was clicked.
- mapClicked($event: any) {
- console.log('Map clicked');
- const newMarker = {
- name: this.markerName,
- lat: $event.coords.lat,
- lng: $event.coords.lng,
- draggable: true
- };
- console.log(newMarker);
- this.markers.push(newMarker);
- }
- // Gives the ability to drag markers around the map.
- markerDragEnd(marker: any, $event: any) {
- console.log('dragEnd', marker, $event);
- const updatedMarker = {
- name: marker.name,
- lat: parseFloat(marker.lat),
- lng: parseFloat(marker.lng),
- draggable: true
- }
- }
- searchEventsForAnotherUniversity() {
- this.universityName = this.universityInput;
- this.checkSuperAdmin();
- console.log(this.universityName);
- console.log(this.universityName);
- this.userService.getAllPublicEvents({universityName: this.universityInput}).subscribe(response => {
- this.processUniversityAndEvents(response = response.json());
- });
- }
- getUniversityDetails() {
- if (this.isLoggedIn) {
- this.userService.getUniversityLocationFromDB(sessionStorage.getItem('auth_key'), {universityName: this.universityName}).subscribe(response => {
- this.processUniversityAndEvents(response = response.json());
- // Checks if user is super admin.
- this.checkSuperAdmin();
- });
- } else {
- // User is not logged in, fetch public events.
- this.userService.getAllPublicEvents({universityName: this.universityName}).subscribe(response => {
- this.processUniversityAndEvents(response = response.json());
- this.checkSuperAdmin();
- });
- }
- }
- processUniversityAndEvents(response) {
- // Sets the lat and lng for the map to center on.
- this.lat = response[0].universityLat;
- this.lng = response[0].universityLng;
- this.universityName = '';
- this.universityName = response[0].universityName;
- console.log(response[0])
- this.listOfEvents = [];
- this.markers = [];
- console.log(response);
- for (let i = 1; i < response.length; i++) {
- this.listOfEvents.push(new Event(
- response[i].eventID,
- response[i].eventName,
- response[i].eventCategory,
- response[i].eventDescription,
- response[i].eventLocation,
- response[i].eventStart.replace('T', ' ').replace('.000Z', ' '),
- response[i].eventEnd.replace('T', ' ').replace('.000Z', ' ')
- ));
- // Creates new markers
- this.markers.push({
- name: response[i].eventName,
- lat: response[i].eventLat,
- lng: response[i].eventLng,
- draggable: true
- });
- }
- console.log(this.markers);
- }
- checkSuperAdmin() {
- this.userService.getSuperAdmin({universityName: this.universityName}).subscribe(response => {
- if (response.json().isRegistered === true) {
- this.isSuperAdmin = true;
- } else {
- this.isSuperAdmin = false;
- }
- });
- }
- onRegisterUniv() {
- // If an auth key exists in the session storage.
- if (this.userService.isAuthenticated()) {
- this.router.navigate(['../signup/superAdminRegister']);
- } else {
- this.router.navigate(['../signup']);
- }
- }
- onStarClick(event) {
- this.rating = event.srcElement.value;
- console.log(event.srcElement.value);
- }
- onComment(): void {
- const token = sessionStorage.getItem('auth_key');
- console.log(this.selectedEvent)
- const commentData = {
- comment_text: this.comment,
- comment_stars: this.rating,
- event_id: this.selectedEvent.eventID
- };
- this.userService.addCommentForEvent(commentData, token).subscribe(response => {
- console.log(response);
- window.location.reload()
- })
- }
- getUniversityAboutInfo() {
- this.userService.getUniversityAboutInfo({univ_name: this.universityName}).subscribe(response => {
- this.pushUniversityAboutInfo(response = response.json());
- });
- }
- pushUniversityAboutInfo(response) {
- console.log('univ info' + response)
- this.univDetails.univName = response.univName;
- this.univDetails.univPop = response.univPop;
- this.univDetails.univDesc = response.univDesc;
- this.univDetails.univPic = response.univPic;
- }
- } // end of class
- // Marker Type
- interface Marker {
- name: string;
- lat: number;
- lng: number;
- draggable: boolean;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement