Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Component, Inject, OnInit} from '@angular/core';
  2. import { Event } from './event.model';
  3. import { Comment } from './comment.model';
  4. import { UnivDetails } from './univDetails.model';
  5. import { ActivatedRoute, Router } from '@angular/router';
  6. import { UserService } from '../service/dataaccess.service';
  7.  
  8. @Component({
  9.   selector: 'app-events',
  10.   templateUrl: './events.component.html',
  11.   styleUrls: ['./events.component.css'],
  12.   providers: [UserService]
  13. })
  14.  
  15. export class EventsComponent implements OnInit {
  16.  
  17.   universityName = 'UCF';
  18.   universityInput = '';
  19.   userService: any;
  20.   isSuperAdmin = false;
  21.   isLoggedIn : boolean;
  22.  
  23.   comment = '';
  24.   rating = '';
  25.  
  26.   // Map default location of UCF
  27.   lat = 28.602427;
  28.   lng = -81.2000599;
  29.  
  30.   // Sets the default zoom Level.
  31.   zoom = 15;
  32.  
  33.   // Holds the marker's name.
  34.   markerName: string;
  35.  
  36.   // Marker array that will hold the markers on the map.
  37.   markers: Marker[] = [];
  38.  
  39.   listOfComments: Comment[];
  40.   // Array to store all the event objects.
  41.   listOfEvents: Event[];
  42.   selectedEvent: Event;
  43.  
  44.   univDetails: UnivDetails;
  45.  
  46.   constructor(private route: ActivatedRoute, private router: Router, @Inject(UserService) userService) {
  47.     this.userService = userService;
  48.   }
  49.  
  50.   /**
  51.    * Every time the page is loaded, it checks if a key is stored in the session storage.
  52.    * It also loads public events for the University of Central Florida.
  53.    */
  54.   ngOnInit() {
  55.     // If an auth key exists in the session storage, then the user must be
  56.     // logged in
  57.     this.isLoggedIn = (sessionStorage.getItem('auth_key') !== null);
  58.  
  59.     // Gets university location and events.
  60.     this.getUniversityDetails();
  61.     this.getUniversityAboutInfo();
  62.     console.log(this.isSuperAdmin);
  63.  
  64.   }
  65.  
  66.   onSelect(event: Event): void {
  67.     console.log(event.eventID);
  68.    
  69.     const token = sessionStorage.getItem('auth_key');
  70.  
  71.     this.selectedEvent = event;
  72.     this.userService.getComments({event_id: this.selectedEvent.eventID}, token).subscribe(response => {
  73.       this.displayComments(response = response.json());
  74.     });
  75.   }
  76.  
  77.   displayComments(response) {
  78.     this.listOfComments = [];
  79.  
  80.     console.log(response);
  81.  
  82.     for (let i = 0; i < response.length; i++) {
  83.       this.listOfComments.push(new Comment(
  84.           response[i].comID,
  85.           response[i].canEdit,
  86.           response[i].commentText,
  87.           response[i].commentStars
  88.       ));
  89.     }
  90.   }
  91.  
  92.   deleteComments(comment) {
  93.     console.log('hi');
  94.  
  95.     this.userService.deleteComments({comment_id: comment.comID}).subscribe(response => {
  96.  
  97.       console.log(response)
  98.       window.location.reload()
  99.     })
  100.   }
  101.  
  102.  
  103.   // Method that handles when a marker is clicked.
  104.   // TODO: Highlight the event that corresponds to the marker.
  105.   clickedMarker(marker: Marker, index: number) {
  106.     console.log('Clicked Marker: ' + marker.name + 'at index ' + index)
  107.   }
  108.  
  109.  
  110.   // Creates a new marker where the map was clicked.
  111.   mapClicked($event: any) {
  112.     console.log('Map clicked');
  113.  
  114.     const newMarker = {
  115.       name: this.markerName,
  116.       lat: $event.coords.lat,
  117.       lng: $event.coords.lng,
  118.       draggable: true
  119.     };
  120.  
  121.     console.log(newMarker);
  122.  
  123.     this.markers.push(newMarker);
  124.   }
  125.  
  126.   // Gives the ability to drag markers around the map.
  127.   markerDragEnd(marker: any, $event: any) {
  128.     console.log('dragEnd', marker, $event);
  129.  
  130.     const updatedMarker = {
  131.       name: marker.name,
  132.       lat: parseFloat(marker.lat),
  133.       lng: parseFloat(marker.lng),
  134.       draggable: true
  135.     }
  136.   }
  137.  
  138.   searchEventsForAnotherUniversity() {
  139.     this.universityName = this.universityInput;
  140.     this.checkSuperAdmin();
  141.     console.log(this.universityName);
  142.     console.log(this.universityName);
  143.     this.userService.getAllPublicEvents({universityName: this.universityInput}).subscribe(response => {
  144.       this.processUniversityAndEvents(response = response.json());
  145.     });
  146.   }
  147.  
  148.   getUniversityDetails() {
  149.  
  150.     if (this.isLoggedIn) {
  151.  
  152.       this.userService.getUniversityLocationFromDB(sessionStorage.getItem('auth_key'), {universityName: this.universityName}).subscribe(response => {
  153.  
  154.         this.processUniversityAndEvents(response = response.json());
  155.  
  156.         // Checks if user is super admin.
  157.         this.checkSuperAdmin();
  158.       });
  159.     } else {
  160.       // User is not logged in, fetch public events.
  161.       this.userService.getAllPublicEvents({universityName: this.universityName}).subscribe(response => {
  162.         this.processUniversityAndEvents(response = response.json());
  163.         this.checkSuperAdmin();
  164.       });
  165.     }
  166.   }
  167.  
  168.   processUniversityAndEvents(response) {
  169.  
  170.     // Sets the lat and lng for the map to center on.
  171.     this.lat = response[0].universityLat;
  172.     this.lng = response[0].universityLng;
  173.     this.universityName = '';
  174.     this.universityName = response[0].universityName;
  175.  
  176.     console.log(response[0])
  177.  
  178.     this.listOfEvents = [];
  179.     this.markers = [];
  180.  
  181.     console.log(response);
  182.  
  183.     for (let i = 1; i < response.length; i++) {
  184.       this.listOfEvents.push(new Event(
  185.           response[i].eventID,
  186.           response[i].eventName,
  187.           response[i].eventCategory,
  188.           response[i].eventDescription,
  189.           response[i].eventLocation,
  190.           response[i].eventStart.replace('T', ' ').replace('.000Z', ' '),
  191.           response[i].eventEnd.replace('T', ' ').replace('.000Z', ' ')
  192.       ));
  193.  
  194.       // Creates new markers
  195.       this.markers.push({
  196.         name: response[i].eventName,
  197.         lat: response[i].eventLat,
  198.         lng: response[i].eventLng,
  199.         draggable: true
  200.  
  201.       });
  202.  
  203.     }
  204.  
  205.     console.log(this.markers);
  206.   }
  207.  
  208.   checkSuperAdmin() {
  209.     this.userService.getSuperAdmin({universityName: this.universityName}).subscribe(response => {
  210.       if (response.json().isRegistered === true) {
  211.         this.isSuperAdmin = true;
  212.       } else {
  213.         this.isSuperAdmin = false;
  214.       }
  215.     });
  216.   }
  217.  
  218.   onRegisterUniv() {
  219.     // If an auth key exists in the session storage.
  220.     if (this.userService.isAuthenticated()) {
  221.       this.router.navigate(['../signup/superAdminRegister']);
  222.     } else {
  223.       this.router.navigate(['../signup']);
  224.     }
  225.   }
  226.  
  227.   onStarClick(event) {
  228.  
  229.     this.rating = event.srcElement.value;
  230.  
  231.     console.log(event.srcElement.value);
  232.   }
  233.  
  234.   onComment(): void {
  235.  
  236.     const token = sessionStorage.getItem('auth_key');
  237.  
  238.     console.log(this.selectedEvent)
  239.  
  240.     const commentData = {
  241.       comment_text: this.comment,
  242.       comment_stars: this.rating,
  243.       event_id: this.selectedEvent.eventID
  244.     };
  245.  
  246.     this.userService.addCommentForEvent(commentData, token).subscribe(response => {
  247.       console.log(response);
  248.  
  249.       window.location.reload()
  250.     })
  251.   }
  252.  
  253.   getUniversityAboutInfo() {
  254.     this.userService.getUniversityAboutInfo({univ_name: this.universityName}).subscribe(response => {
  255.         this.pushUniversityAboutInfo(response = response.json());
  256.       });
  257.   }
  258.  
  259.   pushUniversityAboutInfo(response) {
  260.     console.log('univ info' + response)
  261.  
  262.     this.univDetails.univName = response.univName;
  263.     this.univDetails.univPop  = response.univPop;
  264.     this.univDetails.univDesc = response.univDesc;
  265.     this.univDetails.univPic  = response.univPic;
  266.   }
  267.  
  268. } // end of class
  269.  
  270. // Marker Type
  271. interface Marker {
  272.   name: string;
  273.   lat: number;
  274.   lng: number;
  275.   draggable: boolean;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement