Advertisement
gin_kovacs

tellit sidebar ts

Jul 18th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, TemplateRef } from '@angular/core';
  2. import { NotificationService } from '../../services/notification.service';
  3. import { UserDTO, StoryDTO, ChapterDTO, BookmarkDTO } from '../../generated-models/rest';
  4. import { AuthService } from '../../services/auth.service';
  5. import { StoryService } from '../../services/story.service';
  6. import { UserService } from '../../services/user.service';
  7. import { BsModalRef, BsModalService } from "ngx-bootstrap";
  8. import { Router } from "@angular/router";
  9. import { SharedService } from '../../services/shared.service';
  10.  
  11. @Component({
  12.   selector: 'sidebar',
  13.   templateUrl: './sidebar.component.html',
  14.   styleUrls: ['./sidebar.component.css']
  15. })
  16. export class SidebarComponent implements OnInit {
  17.  
  18.   isCollapsedStory: boolean = false;
  19.   isCollapsedChapter: boolean = false;
  20.   isCollapsedBookmark: boolean = false;
  21.  
  22.   user: UserDTO;
  23.   storyList: StoryDTO[];
  24.   chapterList: ChapterDTO[];
  25.   bookmarkList: BookmarkDTO[];
  26.  
  27.   story: StoryDTO;
  28.   deleteModalRef: BsModalRef;
  29.  
  30.   constructor(private notificationService: NotificationService,
  31.     private authService: AuthService,
  32.     private storyService: StoryService,
  33.     private userService: UserService,
  34.     private modalService: BsModalService,
  35.     private router: Router,
  36.     private sharedService: SharedService) { }
  37.  
  38.   ngOnInit() {
  39.  
  40.     this.modalService.onHide.subscribe((reason: string) => this.story = null)
  41.  
  42.     this.authService.checkLogin()
  43.       .then(user =>
  44.       {
  45.         this.user = user;
  46.         if (user) {
  47.           setTimeout(() => this.sharedService.showDefaultSidebar());
  48.         }
  49.       })
  50.       .catch(error => this.handleError(error));
  51.  
  52.     this.authService.loggedIn.subscribe(value => {
  53.       if (value) {
  54.         setTimeout(() => this.sharedService.showDefaultSidebar());
  55.       }
  56.       else {
  57.         setTimeout(() => this.sharedService.hideDefaultSidebar());
  58.         this.chapterList = [];
  59.         this.storyList = [];
  60.         this.bookmarkList = [];
  61.       }
  62.     });
  63.  
  64.     this.sharedService.getStoryListObservable().subscribe(storyList =>
  65.     {
  66.       this.storyList = storyList;
  67.     });
  68.     this.sharedService.getChapterListObservable().subscribe(chapterList =>
  69.     {
  70.       this.chapterList = chapterList;
  71.     });
  72.     this.sharedService.getBookmarkListObservable().subscribe(bookmarkList =>
  73.     {
  74.       this.bookmarkList = bookmarkList;
  75.     });
  76.     this.sharedService.loadSidebarData();
  77.   }
  78.  
  79.   showDeleteModal(template: TemplateRef<any>, story) {
  80.     this.story=story;
  81.     this.deleteModalRef = this.modalService.show(template);
  82.   }
  83.  
  84.   confirmDelete(confirmed: boolean = false) {
  85.     this.deleteModalRef.hide();
  86.     if (confirmed) {
  87.       this.storyService.deleteStory(this.story)
  88.         .then(() => this.router.navigate(['/']));
  89.     }
  90.     else
  91.     {
  92.       this.story=null;
  93.     }
  94.   }
  95.  
  96.   private handleError(error) {
  97.     this.notificationService.error(error.message || error);
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement