From 60218a73f28bef44855e9af5d896a89e61b0d325 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 28 Aug 2024 02:01:39 +0300 Subject: [PATCH] refactor: move TabStorage to service --- .../schedule/tabs/group/group.component.ts | 20 ++++++++----------- .../lecture-hall/lecture-hall.component.ts | 12 +++++------ .../tabs/professor/professor.component.ts | 6 +++--- .../schedule/tabs/tabs.component.ts | 8 ++++---- .../tab-storage.service.ts} | 6 +++++- 5 files changed, 26 insertions(+), 26 deletions(-) rename src/{components/common/tab-storage/tab-storage.component.ts => services/tab-storage.service.ts} (94%) diff --git a/src/components/schedule/tabs/group/group.component.ts b/src/components/schedule/tabs/group/group.component.ts index 5a5bfdf..901a8bf 100644 --- a/src/components/schedule/tabs/group/group.component.ts +++ b/src/components/schedule/tabs/group/group.component.ts @@ -9,11 +9,7 @@ import {FacultyResponse} from "@api/v1/facultyResponse"; import {FacultyService} from "@api/v1/faculty.service"; import {GroupService} from "@api/v1/group.service"; import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab"; -import { - TabSelect, - TabSelectType, - TabStorageComponent -} from "@component/common/tab-storage/tab-storage.component"; +import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service"; @Component({ selector: 'app-group', @@ -58,7 +54,7 @@ export class GroupComponent implements IScheduleTab { @Output() eventResult = new EventEmitter(); constructor(private facultyApi: FacultyService, private groupApi: GroupService) { - let selectedData = TabStorageComponent.selected; + let selectedData = TabStorageService.selected; if (selectedData !== null && selectedData.selected !== null) { if (selectedData.type === TabSelectType.group) this.selected = selectedData.selected; @@ -81,7 +77,7 @@ export class GroupComponent implements IScheduleTab { selectedFaculty = data.find(x => x.name === this.selected![0].name); if (selectedFaculty !== undefined) { - TabStorageComponent.trySelectChip(selectedFaculty.id, this.facultyChip); + TabStorageService.trySelectChip(selectedFaculty.id, this.facultyChip); this.onFacultySelected(selectedFaculty.id); } } @@ -112,7 +108,7 @@ export class GroupComponent implements IScheduleTab { let selectedCourse = this.courseNumbers.find(x => x === this.selected![1].index); if (selectedCourse !== undefined) { - TabStorageComponent.trySelectChip(selectedCourse, this.courseChip); + TabStorageService.trySelectChip(selectedCourse, this.courseChip); this.onCourseSelected(selectedCourse); } } @@ -124,7 +120,7 @@ export class GroupComponent implements IScheduleTab { selectedGroup = data.find(x => x.name === this.selected![2].name); if (selectedGroup !== undefined) { - TabStorageComponent.trySelectChip(selectedGroup.id, this.groupChip); + TabStorageService.trySelectChip(selectedGroup.id, this.groupChip); this.onGroupSelected(selectedGroup.id); } } @@ -148,7 +144,7 @@ export class GroupComponent implements IScheduleTab { return; } - TabStorageComponent.select(new TabSelect(index, this.faculties!.find(x => x.id === index)?.name ?? ''), TabSelectType.group, 0); + TabStorageService.select(new TabSelect(index, this.faculties!.find(x => x.id === index)?.name ?? ''), TabSelectType.group, 0); this.facultyId = index; this.courseNumberPanel.open(); @@ -165,7 +161,7 @@ export class GroupComponent implements IScheduleTab { return; } - TabStorageComponent.select(new TabSelect(course, course.toString()), TabSelectType.group, 1); + TabStorageService.select(new TabSelect(course, course.toString()), TabSelectType.group, 1); this.courseNumber = course; this.groupPanel.open(); @@ -176,7 +172,7 @@ export class GroupComponent implements IScheduleTab { if (index === undefined) return; - TabStorageComponent.select(new TabSelect(index, this.groups!.find(x => x.id == index)?.name ?? ''), TabSelectType.group, 2); + TabStorageService.select(new TabSelect(index, this.groups!.find(x => x.id == index)?.name ?? ''), TabSelectType.group, 2); this.groupPanel.close(); this.eventResult.emit(index); diff --git a/src/components/schedule/tabs/lecture-hall/lecture-hall.component.ts b/src/components/schedule/tabs/lecture-hall/lecture-hall.component.ts index 830af0b..29d94dc 100644 --- a/src/components/schedule/tabs/lecture-hall/lecture-hall.component.ts +++ b/src/components/schedule/tabs/lecture-hall/lecture-hall.component.ts @@ -10,7 +10,7 @@ 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"; +import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service"; @Component({ selector: 'app-lecture-hall', @@ -48,7 +48,7 @@ export class LectureHallComponent implements IScheduleTab { private readonly selected: TabSelect[] | null = null; constructor(private campusApi: CampusService, private lectureHallApi: LectureHallService) { - let selectedData = TabStorageComponent.selected; + let selectedData = TabStorageService.selected; if (selectedData !== null && selectedData.selected !== null) { if (selectedData.type === TabSelectType.lecture) this.selected = selectedData.selected; @@ -71,7 +71,7 @@ export class LectureHallComponent implements IScheduleTab { selectedCampus = data.find(x => x.codeName === this.selected![0].name); if (selectedCampus !== undefined) { - TabStorageComponent.trySelectChip(selectedCampus.id, this.campusChip); + TabStorageService.trySelectChip(selectedCampus.id, this.campusChip); this.onCampusSelected(selectedCampus.id); } } @@ -85,7 +85,7 @@ export class LectureHallComponent implements IScheduleTab { protected onCampusSelected(index: number) { this.formLectureHalls.reset(); - TabStorageComponent.select(new TabSelect(index, this.campuses!.find(x => x.id === index)?.codeName ?? ''), TabSelectType.lecture, 0); + TabStorageService.select(new TabSelect(index, this.campuses!.find(x => x.id === index)?.codeName ?? ''), TabSelectType.lecture, 0); if (index === undefined) { this.campusId = null; @@ -118,7 +118,7 @@ export class LectureHallComponent implements IScheduleTab { selectedLecture = data.find(x => x.name === this.selected![1].name); if (selectedLecture !== undefined) { - TabStorageComponent.trySelectChip(selectedLecture.id, this.lectureChip); + TabStorageService.trySelectChip(selectedLecture.id, this.lectureChip); this.onLectureHallSelected(selectedLecture.id); } } @@ -129,7 +129,7 @@ export class LectureHallComponent implements IScheduleTab { if (index === undefined) return; - TabStorageComponent.select(new TabSelect(index, this.lectureHallsFiltered!.find(x => x.id === index)?.name ?? ''), TabSelectType.lecture, 1); + TabStorageService.select(new TabSelect(index, this.lectureHallsFiltered!.find(x => x.id === index)?.name ?? ''), TabSelectType.lecture, 1); this.lecturePanel.close(); this.eventResult.emit(index); diff --git a/src/components/schedule/tabs/professor/professor.component.ts b/src/components/schedule/tabs/professor/professor.component.ts index 2b4d329..d5a7961 100644 --- a/src/components/schedule/tabs/professor/professor.component.ts +++ b/src/components/schedule/tabs/professor/professor.component.ts @@ -8,7 +8,7 @@ import {LoadingIndicatorComponent} from "@component/common/loading-indicator/loa import {ProfessorResponse} from "@api/v1/professorResponse"; import {ProfessorService} from "@api/v1/professor.service"; import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab"; -import {TabSelect, TabSelectType, TabStorageComponent} from "@component/common/tab-storage/tab-storage.component"; +import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service"; @Component({ selector: 'app-professor', @@ -37,7 +37,7 @@ export class ProfessorComponent implements OnInit, IScheduleTab { @Output() eventResult = new EventEmitter(); constructor(private api: ProfessorService) { - let selectedData = TabStorageComponent.selected; + let selectedData = TabStorageService.selected; if (selectedData !== null && selectedData.selected !== null) { if (selectedData.type === TabSelectType.professor) this.selected = selectedData.selected; @@ -96,7 +96,7 @@ export class ProfessorComponent implements OnInit, IScheduleTab { this.professorControl.setValue(selectedOption.name); this.eventResult.emit(selectedOption.id); - TabStorageComponent.select(new TabSelect(selectedOption.id, selectedOption.name), TabSelectType.professor, 0); + TabStorageService.select(new TabSelect(selectedOption.id, selectedOption.name), TabSelectType.professor, 0); } } diff --git a/src/components/schedule/tabs/tabs.component.ts b/src/components/schedule/tabs/tabs.component.ts index 9dbf963..39a32c8 100644 --- a/src/components/schedule/tabs/tabs.component.ts +++ b/src/components/schedule/tabs/tabs.component.ts @@ -17,7 +17,7 @@ import {GroupService} from "@api/v1/group.service"; import {ProfessorService} from "@api/v1/professor.service"; import {AuthRoles} from "@model/AuthRoles"; import {HasRoleDirective} from "@/directives/has-role.directive"; -import {TabStorageComponent} from "@component/common/tab-storage/tab-storage.component"; +import {TabStorageService} from "@service/tab-storage.service"; export enum TabsSelect { Group, @@ -44,7 +44,7 @@ export enum TabsSelect { ], templateUrl: './tabs.component.html', styleUrl: './tabs.component.css', - providers: [ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService] + providers: [ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService, TabStorageService] }) export class TabsComponent implements AfterViewInit { @@ -58,13 +58,13 @@ export class TabsComponent implements AfterViewInit { } ngAfterViewInit(): void { - let selected = TabStorageComponent.selected; + let selected = TabStorageService.selected; let index = 0; if (selected !== null) index = selected.type; - if (this.tabs.selectedIndex === null || this.tabs.selectedIndex === 0) + if (index === null || index === 0) this.chooseTabs(0).then(); else this.tabs.selectedIndex = index; diff --git a/src/components/common/tab-storage/tab-storage.component.ts b/src/services/tab-storage.service.ts similarity index 94% rename from src/components/common/tab-storage/tab-storage.component.ts rename to src/services/tab-storage.service.ts index 5726f95..a590294 100644 --- a/src/components/common/tab-storage/tab-storage.component.ts +++ b/src/services/tab-storage.service.ts @@ -1,4 +1,5 @@ import {MatChipListbox} from "@angular/material/chips"; +import {Injectable} from "@angular/core"; export class TabSelect { public index: number; @@ -22,7 +23,10 @@ export interface TabSelectData { type: TabSelectType; } -export class TabStorageComponent { +@Injectable({ + providedIn: 'root' +}) +export class TabStorageService { private static dataName = 'tabSelectedData'; public static trySelectChip(index: number, chip: MatChipListbox, tryCount: number = 0) {