refactor: move TabStorage to service

This commit is contained in:
Polianin Nikita 2024-08-28 02:01:39 +03:00
parent 42e454c4d6
commit 60218a73f2
5 changed files with 26 additions and 26 deletions

View File

@ -9,11 +9,7 @@ import {FacultyResponse} from "@api/v1/facultyResponse";
import {FacultyService} from "@api/v1/faculty.service"; import {FacultyService} from "@api/v1/faculty.service";
import {GroupService} from "@api/v1/group.service"; import {GroupService} from "@api/v1/group.service";
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab"; import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab";
import { import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service";
TabSelect,
TabSelectType,
TabStorageComponent
} from "@component/common/tab-storage/tab-storage.component";
@Component({ @Component({
selector: 'app-group', selector: 'app-group',
@ -58,7 +54,7 @@ export class GroupComponent implements IScheduleTab {
@Output() eventResult = new EventEmitter<number>(); @Output() eventResult = new EventEmitter<number>();
constructor(private facultyApi: FacultyService, private groupApi: GroupService) { constructor(private facultyApi: FacultyService, private groupApi: GroupService) {
let selectedData = TabStorageComponent.selected; let selectedData = TabStorageService.selected;
if (selectedData !== null && selectedData.selected !== null) { if (selectedData !== null && selectedData.selected !== null) {
if (selectedData.type === TabSelectType.group) if (selectedData.type === TabSelectType.group)
this.selected = selectedData.selected; this.selected = selectedData.selected;
@ -81,7 +77,7 @@ export class GroupComponent implements IScheduleTab {
selectedFaculty = data.find(x => x.name === this.selected![0].name); selectedFaculty = data.find(x => x.name === this.selected![0].name);
if (selectedFaculty !== undefined) { if (selectedFaculty !== undefined) {
TabStorageComponent.trySelectChip(selectedFaculty.id, this.facultyChip); TabStorageService.trySelectChip(selectedFaculty.id, this.facultyChip);
this.onFacultySelected(selectedFaculty.id); this.onFacultySelected(selectedFaculty.id);
} }
} }
@ -112,7 +108,7 @@ export class GroupComponent implements IScheduleTab {
let selectedCourse = this.courseNumbers.find(x => x === this.selected![1].index); let selectedCourse = this.courseNumbers.find(x => x === this.selected![1].index);
if (selectedCourse !== undefined) { if (selectedCourse !== undefined) {
TabStorageComponent.trySelectChip(selectedCourse, this.courseChip); TabStorageService.trySelectChip(selectedCourse, this.courseChip);
this.onCourseSelected(selectedCourse); this.onCourseSelected(selectedCourse);
} }
} }
@ -124,7 +120,7 @@ export class GroupComponent implements IScheduleTab {
selectedGroup = data.find(x => x.name === this.selected![2].name); selectedGroup = data.find(x => x.name === this.selected![2].name);
if (selectedGroup !== undefined) { if (selectedGroup !== undefined) {
TabStorageComponent.trySelectChip(selectedGroup.id, this.groupChip); TabStorageService.trySelectChip(selectedGroup.id, this.groupChip);
this.onGroupSelected(selectedGroup.id); this.onGroupSelected(selectedGroup.id);
} }
} }
@ -148,7 +144,7 @@ export class GroupComponent implements IScheduleTab {
return; 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.facultyId = index;
this.courseNumberPanel.open(); this.courseNumberPanel.open();
@ -165,7 +161,7 @@ export class GroupComponent implements IScheduleTab {
return; 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.courseNumber = course;
this.groupPanel.open(); this.groupPanel.open();
@ -176,7 +172,7 @@ export class GroupComponent implements IScheduleTab {
if (index === undefined) if (index === undefined)
return; 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.groupPanel.close();
this.eventResult.emit(index); this.eventResult.emit(index);

View File

@ -10,7 +10,7 @@ import {LectureHallResponse} from "@api/v1/lectureHallResponse";
import {CampusService} from "@api/v1/campus.service"; import {CampusService} from "@api/v1/campus.service";
import {LectureHallService} from "@api/v1/lectureHall.service"; import {LectureHallService} from "@api/v1/lectureHall.service";
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab"; 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({ @Component({
selector: 'app-lecture-hall', selector: 'app-lecture-hall',
@ -48,7 +48,7 @@ export class LectureHallComponent implements IScheduleTab {
private readonly selected: TabSelect[] | null = null; private readonly selected: TabSelect[] | null = null;
constructor(private campusApi: CampusService, private lectureHallApi: LectureHallService) { constructor(private campusApi: CampusService, private lectureHallApi: LectureHallService) {
let selectedData = TabStorageComponent.selected; let selectedData = TabStorageService.selected;
if (selectedData !== null && selectedData.selected !== null) { if (selectedData !== null && selectedData.selected !== null) {
if (selectedData.type === TabSelectType.lecture) if (selectedData.type === TabSelectType.lecture)
this.selected = selectedData.selected; this.selected = selectedData.selected;
@ -71,7 +71,7 @@ export class LectureHallComponent implements IScheduleTab {
selectedCampus = data.find(x => x.codeName === this.selected![0].name); selectedCampus = data.find(x => x.codeName === this.selected![0].name);
if (selectedCampus !== undefined) { if (selectedCampus !== undefined) {
TabStorageComponent.trySelectChip(selectedCampus.id, this.campusChip); TabStorageService.trySelectChip(selectedCampus.id, this.campusChip);
this.onCampusSelected(selectedCampus.id); this.onCampusSelected(selectedCampus.id);
} }
} }
@ -85,7 +85,7 @@ export class LectureHallComponent implements IScheduleTab {
protected onCampusSelected(index: number) { protected onCampusSelected(index: number) {
this.formLectureHalls.reset(); 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) { if (index === undefined) {
this.campusId = null; this.campusId = null;
@ -118,7 +118,7 @@ export class LectureHallComponent implements IScheduleTab {
selectedLecture = data.find(x => x.name === this.selected![1].name); selectedLecture = data.find(x => x.name === this.selected![1].name);
if (selectedLecture !== undefined) { if (selectedLecture !== undefined) {
TabStorageComponent.trySelectChip(selectedLecture.id, this.lectureChip); TabStorageService.trySelectChip(selectedLecture.id, this.lectureChip);
this.onLectureHallSelected(selectedLecture.id); this.onLectureHallSelected(selectedLecture.id);
} }
} }
@ -129,7 +129,7 @@ export class LectureHallComponent implements IScheduleTab {
if (index === undefined) if (index === undefined)
return; 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.lecturePanel.close();
this.eventResult.emit(index); this.eventResult.emit(index);

View File

@ -8,7 +8,7 @@ import {LoadingIndicatorComponent} from "@component/common/loading-indicator/loa
import {ProfessorResponse} from "@api/v1/professorResponse"; import {ProfessorResponse} from "@api/v1/professorResponse";
import {ProfessorService} from "@api/v1/professor.service"; import {ProfessorService} from "@api/v1/professor.service";
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab"; 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({ @Component({
selector: 'app-professor', selector: 'app-professor',
@ -37,7 +37,7 @@ export class ProfessorComponent implements OnInit, IScheduleTab {
@Output() eventResult = new EventEmitter<number>(); @Output() eventResult = new EventEmitter<number>();
constructor(private api: ProfessorService) { constructor(private api: ProfessorService) {
let selectedData = TabStorageComponent.selected; let selectedData = TabStorageService.selected;
if (selectedData !== null && selectedData.selected !== null) { if (selectedData !== null && selectedData.selected !== null) {
if (selectedData.type === TabSelectType.professor) if (selectedData.type === TabSelectType.professor)
this.selected = selectedData.selected; this.selected = selectedData.selected;
@ -96,7 +96,7 @@ export class ProfessorComponent implements OnInit, IScheduleTab {
this.professorControl.setValue(selectedOption.name); this.professorControl.setValue(selectedOption.name);
this.eventResult.emit(selectedOption.id); 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);
} }
} }

View File

@ -17,7 +17,7 @@ import {GroupService} from "@api/v1/group.service";
import {ProfessorService} from "@api/v1/professor.service"; import {ProfessorService} from "@api/v1/professor.service";
import {AuthRoles} from "@model/AuthRoles"; import {AuthRoles} from "@model/AuthRoles";
import {HasRoleDirective} from "@/directives/has-role.directive"; 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 { export enum TabsSelect {
Group, Group,
@ -44,7 +44,7 @@ export enum TabsSelect {
], ],
templateUrl: './tabs.component.html', templateUrl: './tabs.component.html',
styleUrl: './tabs.component.css', styleUrl: './tabs.component.css',
providers: [ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService] providers: [ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService, TabStorageService]
}) })
export class TabsComponent implements AfterViewInit { export class TabsComponent implements AfterViewInit {
@ -58,13 +58,13 @@ export class TabsComponent implements AfterViewInit {
} }
ngAfterViewInit(): void { ngAfterViewInit(): void {
let selected = TabStorageComponent.selected; let selected = TabStorageService.selected;
let index = 0; let index = 0;
if (selected !== null) if (selected !== null)
index = selected.type; index = selected.type;
if (this.tabs.selectedIndex === null || this.tabs.selectedIndex === 0) if (index === null || index === 0)
this.chooseTabs(0).then(); this.chooseTabs(0).then();
else else
this.tabs.selectedIndex = index; this.tabs.selectedIndex = index;

View File

@ -1,4 +1,5 @@
import {MatChipListbox} from "@angular/material/chips"; import {MatChipListbox} from "@angular/material/chips";
import {Injectable} from "@angular/core";
export class TabSelect { export class TabSelect {
public index: number; public index: number;
@ -22,7 +23,10 @@ export interface TabSelectData {
type: TabSelectType; type: TabSelectType;
} }
export class TabStorageComponent { @Injectable({
providedIn: 'root'
})
export class TabStorageService {
private static dataName = 'tabSelectedData'; private static dataName = 'tabSelectedData';
public static trySelectChip(index: number, chip: MatChipListbox, tryCount: number = 0) { public static trySelectChip(index: number, chip: MatChipListbox, tryCount: number = 0) {