feat: add saving of user's selection

This commit is contained in:
2024-08-23 23:19:25 +03:00
parent 2e36b06aea
commit e0a2ba257c
10 changed files with 429 additions and 136 deletions

View File

@ -1,14 +1,16 @@
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
import {Component, EventEmitter, Output, ViewChild} from '@angular/core';
import {AsyncPipe} from "@angular/common";
import {MatAccordion, MatExpansionModule, MatExpansionPanel} from "@angular/material/expansion";
import {MatChipListboxChange, MatChipsModule} from "@angular/material/chips";
import {catchError, Observable, of} from "rxjs";
import {MatChipListbox, MatChipsModule} from "@angular/material/chips";
import {catchError} from "rxjs";
import {FormControl, ReactiveFormsModule} from "@angular/forms";
import {LoadingIndicatorComponent} from "@component/common/loading-indicator/loading-indicator.component";
import {CampusBasicInfoResponse} from "@api/v1/campusBasicInfoResponse";
import {LectureHallResponse} from "@api/v1/lectureHallResponse";
import {CampusService} from "@api/v1/campus.service";
import {LectureHallService} from "@api/v1/lectureHall.service";
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab";
import {TabSelect, TabSelectType, TabStorageComponent} from "@component/common/tab-storage/tab-storage.component";
@Component({
selector: 'app-lecture-hall',
@ -26,79 +28,115 @@ import {LectureHallService} from "@api/v1/lectureHall.service";
providers: [CampusService, LectureHallService]
})
export class LectureHallComponent {
export class LectureHallComponent implements IScheduleTab {
protected campusId: number | null = null;
protected chipLecture: FormControl = new FormControl();
protected formLectureHalls: FormControl = new FormControl();
@ViewChild('lecturePanel') lecturePanel!: MatExpansionPanel;
protected campuses: CampusBasicInfoResponse[] = [];
protected campusesLoaded: boolean | null = false;
private lectureHalls: LectureHallResponse[] = [];
protected lectureHallsFiltered: LectureHallResponse[] = [];
protected lectureHallsLoaded: boolean | null = false;
protected campuses: CampusBasicInfoResponse[] | null = null;
protected lectureHallsFiltered: LectureHallResponse[] | null = null;
@Output() eventResult = new EventEmitter<number>();
@ViewChild('lecturePanel') lecturePanel!: MatExpansionPanel;
@ViewChild('lectureIndicator') lectureIndicator!: LoadingIndicatorComponent;
@ViewChild('campusIndicator') campusIndicator!: LoadingIndicatorComponent;
@ViewChild('campusChip') campusChip!: MatChipListbox;
@ViewChild('lectureChip') lectureChip!: MatChipListbox;
private lectureHalls: LectureHallResponse[] | null = null;
private readonly selected: TabSelect[] | null = null;
constructor(private campusApi: CampusService, private lectureHallApi: LectureHallService) {
this.loadCampuses();
let selectedData = TabStorageComponent.selected;
if (selectedData !== null && selectedData.selected !== null) {
if (selectedData.type === TabSelectType.lecture)
this.selected = selectedData.selected;
}
}
protected loadCampuses() {
this.campusesLoaded = false;
this.campusApi.getCampus()
.pipe(catchError(error => {
this.campusesLoaded = null;
this.campusIndicator.loading = false;
throw error;
}))
.subscribe(data => {
this.campuses = data;
this.campusesLoaded = true;
if (this.selected !== null && this.selected.length >= 1) {
let selectedCampus = data.find(x => x.id === this.selected![0].index);
if (selectedCampus === undefined || selectedCampus.codeName !== this.selected![0].name)
selectedCampus = data.find(x => x.codeName === this.selected![0].name);
if (selectedCampus !== undefined) {
TabStorageComponent.trySelectChip(selectedCampus.id, this.campusChip);
this.onCampusSelected(selectedCampus.id);
}
}
});
}
private filteringLectureHalls() {
this.lectureHallsFiltered = this.lectureHalls.filter(x => x.campusId === this.campusId);
this.lectureHallsFiltered = this.lectureHalls?.filter(x => x.campusId === this.campusId) ?? null;
}
protected chooseCampus(event: MatChipListboxChange) {
this.chipLecture.reset();
protected onCampusSelected(index: number) {
this.formLectureHalls.reset();
if (event.value === undefined || event.value === null) {
TabStorageComponent.select(new TabSelect(index, this.campuses!.find(x => x.id === index)?.codeName ?? ''), TabSelectType.lecture, 0);
if (index === undefined) {
this.campusId = null;
this.lectureHalls = [];
return;
}
this.campusId = event.value;
this.campusId = index;
this.lecturePanel.open();
if (this.lectureHalls.length === 0)
if (this.lectureHalls === null)
this.loadLectureHalls();
else
this.filteringLectureHalls();
}
protected loadLectureHalls() {
this.lectureHallsLoaded = false;
this.lectureHallApi.getLectureHalls()
.pipe(catchError(error => {
this.lectureHallsLoaded = null;
this.lectureIndicator.loading = false;
throw error;
}))
.subscribe(data => {
this.lectureHalls = data;
this.filteringLectureHalls();
this.lectureHallsLoaded = true;
if (this.selected !== null && this.selected.length >= 2) {
let selectedLecture = data.find(x => x.id === this.selected![1].index);
if (selectedLecture === undefined || selectedLecture.name !== this.selected![1].name)
selectedLecture = data.find(x => x.name === this.selected![1].name);
if (selectedLecture !== undefined) {
TabStorageComponent.trySelectChip(selectedLecture.id, this.lectureChip);
this.onLectureHallSelected(selectedLecture.id);
}
}
});
}
protected chooseLectureHall(event: MatChipListboxChange) {
if (event.value === undefined || event.value === null)
protected onLectureHallSelected(index: number) {
if (index === undefined)
return;
TabStorageComponent.select(new TabSelect(index, this.lectureHallsFiltered!.find(x => x.id === index)?.name ?? ''), TabSelectType.lecture, 1);
this.lecturePanel.close();
this.eventResult.emit(event.value);
this.eventResult.emit(index);
}
public load() {
if (this.campuses === null)
this.loadCampuses();
}
}