From b7c70bd4dfe6ef64b7f7afdef9d393aead6ce1be Mon Sep 17 00:00:00 2001 From: Claudia Asti Date: Wed, 29 Apr 2020 16:53:35 +0200 Subject: [PATCH] Do not show HalfDays if corresponding PresenceType is inactive --- .../edit-absences-edit.component.ts | 16 ++++++++++++ .../evaluate-absences-list.component.ts | 25 +++++++++++++++++-- .../presence-control/utils/presence-types.ts | 11 ++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/app/edit-absences/components/edit-absences-edit/edit-absences-edit.component.ts b/src/app/edit-absences/components/edit-absences-edit/edit-absences-edit.component.ts index 74a198086..651a33356 100644 --- a/src/app/edit-absences/components/edit-absences-edit/edit-absences-edit.component.ts +++ b/src/app/edit-absences/components/edit-absences-edit/edit-absences-edit.component.ts @@ -28,6 +28,7 @@ import { sortPresenceTypes } from 'src/app/shared/utils/presence-types'; import { EditAbsencesStateService } from '../../services/edit-absences-state.service'; import { PresenceTypesRestService } from '../../../shared/services/presence-types-rest.service'; import { parseQueryString } from 'src/app/shared/utils/url'; +import { isHalfDay } from '../../../presence-control/utils/presence-types'; enum Category { Absent = 'absent', @@ -118,6 +119,21 @@ export class EditAbsencesEditComponent implements OnInit, OnDestroy { .pipe(takeUntil(this.destroy$)) .subscribe(this.updateAbsenceTypeIdDisabled.bind(this)); } + + // Remove Category HalfDay if the corresponding PresenceType is inactive + this.absenceTypes$ + .pipe( + map((types) => + types.filter((t) => isHalfDay(t, this.settings) && t.Active) + ) + ) + .subscribe((types) => { + if (types.length === 0) { + this.categories = this.categories.filter( + (c) => c !== Category.HalfDay + ); + } + }); } ngOnDestroy(): void { diff --git a/src/app/evaluate-absences/components/evaluate-absences-list/evaluate-absences-list.component.ts b/src/app/evaluate-absences/components/evaluate-absences-list/evaluate-absences-list.component.ts index a7658c676..fb85fe380 100644 --- a/src/app/evaluate-absences/components/evaluate-absences-list/evaluate-absences-list.component.ts +++ b/src/app/evaluate-absences/components/evaluate-absences-list/evaluate-absences-list.component.ts @@ -3,10 +3,11 @@ import { OnInit, ChangeDetectionStrategy, AfterViewInit, + Inject, } from '@angular/core'; import { ActivatedRoute, Params } from '@angular/router'; import { Observable } from 'rxjs'; -import { map, take } from 'rxjs/operators'; +import { map, take, shareReplay } from 'rxjs/operators'; import { EvaluateAbsencesStateService, @@ -14,6 +15,9 @@ import { } from '../../services/evaluate-absences-state.service'; import { LessonPresenceStatistic } from 'src/app/shared/models/lesson-presence-statistic'; import { ScrollPositionService } from 'src/app/shared/services/scroll-position.service'; +import { PresenceTypesRestService } from '../../../shared/services/presence-types-rest.service'; +import { SETTINGS, Settings } from '../../../settings'; +import { isHalfDay } from '../../../presence-control/utils/presence-types'; interface Column { key: keyof LessonPresenceStatistic; @@ -27,6 +31,8 @@ interface Column { changeDetection: ChangeDetectionStrategy.OnPush, }) export class EvaluateAbsencesListComponent implements OnInit, AfterViewInit { + presenceTypes$ = this.presenceTypesService.getList().pipe(shareReplay(1)); + columns: ReadonlyArray = [ { key: 'StudentFullName', label: 'student' }, { key: 'TotalAbsences', label: 'total' }, @@ -43,13 +49,28 @@ export class EvaluateAbsencesListComponent implements OnInit, AfterViewInit { constructor( public state: EvaluateAbsencesStateService, private scrollPosition: ScrollPositionService, - private route: ActivatedRoute + private route: ActivatedRoute, + private presenceTypesService: PresenceTypesRestService, + @Inject(SETTINGS) private settings: Settings ) {} ngOnInit(): void { this.filterFromParams$ .pipe(take(1)) .subscribe((filterValue) => this.state.setFilter(filterValue)); + + // Remove Column TotalHalfDays if the corresponding PresenceType is inactive + this.presenceTypes$ + .pipe( + map((types) => + types.filter((t) => isHalfDay(t, this.settings) && t.Active) + ) + ) + .subscribe((types) => { + if (types.length === 0) { + this.columns = this.columns.filter((c) => c.key !== 'TotalHalfDays'); + } + }); } ngAfterViewInit(): void { diff --git a/src/app/presence-control/utils/presence-types.ts b/src/app/presence-control/utils/presence-types.ts index ec5d3d5cb..0f0bc9c30 100644 --- a/src/app/presence-control/utils/presence-types.ts +++ b/src/app/presence-control/utils/presence-types.ts @@ -19,6 +19,17 @@ export function isAbsent(presenceType: Option): boolean { ); } +export function isHalfDay( + presenceType: Option, + settings: Settings +): boolean { + return Boolean( + presenceType && + settings && + presenceType.Id === settings.halfDayPresenceTypeId + ); +} + export function isDefaultAbsence( presenceType: Option, settings: Settings