import { Component, OnDestroy, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { SpinnerService } from '@core/services/spinner.service'; import { StatusService } from '@core/services/status.service'; import { ApplicationForUi } from '@core/typings/application.typing'; import { ProcessingTypes } from '@core/typings/payment.typing'; import { AwardService } from '@features/awards/award.service'; import { Award } from '@features/awards/typings/award.typing'; import { ProgramService } from '@features/programs/program.service'; import { Subscription } from 'rxjs'; import { ApplicationViewService } from '../application-view.service'; @Component({ selector: 'gc-awards-tab', templateUrl: './awards-tab.component.html', styleUrls: ['./awards-tab.component.scss'] }) export class AwardsTabComponent implements OnInit, OnDestroy { awards: Award[]; budgets: number[]; application: ApplicationForUi; statusMap = this.statusService.paymentStatusMap; programId: number; cycleId: number; sub = new Subscription(); ProcessingTypes = ProcessingTypes; ready = false; constructor ( private activatedRoute: ActivatedRoute, private statusService: StatusService, private programService: ProgramService, private spinnerService: SpinnerService, private awardService: AwardService, private router: Router, private applicationViewService: ApplicationViewService ) { this.sub.add( this.applicationViewService.changesTo$('applicationViewMap') .subscribe(() => { this.onApplicationChange(); }) ); this.sub.add( this.applicationViewService.changesTo$('applicationEditMap') .subscribe(() => { this.onApplicationChange(); }) ); } get isEdit () { return this.activatedRoute.snapshot.data.isEdit; } get id () { return this.activatedRoute.snapshot.parent.params.id; } get isNomination () { return location.pathname.includes('nomination'); } get basePath () { return `/management/${this.isNomination ? 'nomination-view' : 'application-view'}/${this.id}`; } get applicationMap () { return this.isEdit ? this.applicationViewService.applicationEditMap : this.applicationViewService.applicationViewMap; } async ngOnInit () { this.spinnerService.startSpinner(); this.ready = false; this.setApplication(); const cycle = await this.programService.getCycleFromProgram( this.programId, this.cycleId ); this.budgets = cycle.budgetIds; this.setAwards(); this.ready = true; this.spinnerService.stopSpinner(); } onApplicationChange () { this.setApplication(); const hideAwardTab = this.awardService.shouldHideAwardTab( this.applicationMap[this.id].application, this.applicationMap[this.id].awards.awards, this.isNomination ); if (hideAwardTab) { this.router.navigate([`${this.basePath}/form/no-form`]); } else { if (this.applicationMap[this.id]) { this.setAwards(); } } } setApplication () { this.application = this.applicationMap[this.id] ? this.applicationMap[this.id].application : null; this.programId = this.application ? this.application.programId : null; this.cycleId = this.application ? this.application.grantProgramCycle.id : null; } setAwards () { this.awards = this.awardService.constructAwards( this.applicationMap[this.id].awards.awards ); } async onAwardChange () { await this.updateApplication(); } async updateApplication () { this.spinnerService.startSpinner(); await this.applicationViewService.setApplicationViewMap(this.id, this.isEdit); this.spinnerService.stopSpinner(); } ngOnDestroy () { this.sub.unsubscribe(); } }