import { Component , OnInit , Inject , PLATFORM_ID } from '@angular/core' ; import { isPlatformServer } from '@angular/common' ; import { Router } from '@angular/router' ; import { Client } from '../../../services/api/client' ; import { Session } from '../../../services/session' ; import { FeaturesService } from '../../../services/features.service' ; /**
 * Category returned from api/v2/helpdesk/categories.
 */ type Category = { uuid : string ; title : string ; questions ? : Question [ ] ; collapsed ? : boolean ; inProgress ? : boolean ; } | null ; /**
 * Question returned from api/v2/helpdesk/questions.
 */ type Question = { uuid : string ; question : string ; } | null ; /**
 * Helpdesk dashboard with collapsible categories and questions.
 */ @ Component ( { selector : 'm-helpdesk--dashboard--all' , templateUrl : 'all.component.html' , styleUrls : [ 'all.component.ng.scss' ] , } ) export class AllHelpdeskDashboardComponent implements OnInit { /**
     * Array of categories loaded in OnInit hook.
     */ public categories : Category [ ] = [ ] ; constructor ( public router , public client , public session , private features , @ Inject ( PLATFORM_ID ) private platformId ) { } async ngOnInit ( ) { if ( this . features . has ( 'helpdesk-2021' ) && ! isPlatformServer ( this . platformId ) ) { window . location . href = '/api/v3/helpdesk/zendesk' ; } await this . load ( ) ; } param { Category } returns { Promise < void > } public async toggleCategory ( category ) { category . inProgress = true ; category . collapsed = ! category . collapsed ; if ( ! category . questions || category . questions . length < 1 ) { category . questions = await this . getQuestions ( category ) ; category . inProgress = false ; } category . inProgress = false ; } returns { Promise < void > } private async load ( ) { this . categories = await this . getCategories ( ) ; } returns { Promise < void > } private async getCategories ( ) { const limit = this . getLimit ( ) ; let response = await this . client . get ( `api/v2/helpdesk/categories` , { limit , } ) ; return response . categories . sort ( ( a , b ) => a . position - b . position ) . map ( category => { category . collapsed = true ; category . inProgress = false ; return category ; } ) ; } param { Category } param { Promise < Question > } private async getQuestions ( category ) { try { const { questions } = await this . client . get ( `api/v2/helpdesk/questions` , { limit : this . getLimit ( ) , category_uuid : category . uuid , } ) ; return questions . sort ( ( a , b ) => { return a . position - b . position ; } ) ; } catch ( e ) { console . error ( e ) ; } } returns { number } private getLimit ( ) { return isPlatformServer ( this . platformId ) ? 12 : 5000 ; } }