Files
MireaFrontend/src/pages/schedule/schedule.component.ts
Polianin Nikita 79393a39c3 feat: add parameters as navigation
Now there is automatic navigation on the inserted url and on localStorage
2024-08-28 03:52:47 +03:00

146 lines
4.6 KiB
TypeScript

import {Component, LOCALE_ID, OnInit, ViewChild} from '@angular/core';
import {TableComponent} from "@component/schedule/table/table.component";
import {MatFormField, MatInput} from "@angular/material/input";
import {MatButton} from "@angular/material/button";
import {FormsModule} from "@angular/forms";
import {AdditionalText, TableHeaderComponent} from "@component/schedule/table-header/table-header.component";
import {addDays, weekInYear} from "@progress/kendo-date-math";
import {MatCard} from "@angular/material/card";
import {MatSidenavModule} from "@angular/material/sidenav";
import {TabsComponent, TabsSelect} from "@component/schedule/tabs/tabs.component";
import {catchError, Observable} from "rxjs";
import {ScheduleService} from "@api/v1/schedule.service";
import {ScheduleResponse} from "@api/v1/scheduleResponse";
import {PeriodTimes} from "@model/pairPeriodTime";
import {MatCheckbox} from "@angular/material/checkbox";
import {ActivatedRoute} from "@angular/router";
import {TabStorageService} from "@service/tab-storage.service";
@Component({
selector: 'app-schedule',
standalone: true,
imports: [
TableComponent,
MatInput,
MatFormField,
MatButton,
FormsModule,
TableHeaderComponent,
MatCard,
MatSidenavModule,
TabsComponent,
MatCheckbox
],
templateUrl: './schedule.component.html',
styleUrl: './schedule.component.css',
providers: [
ScheduleService,
{provide: LOCALE_ID, useValue: 'ru-RU'}
]
})
export class ScheduleComponent implements OnInit {
protected startWeek: Date;
protected data: ScheduleResponse[] = [];
protected startTerm: Date;
protected isLoadTable: boolean = false;
protected pairPeriods: PeriodTimes = {};
protected disciplineWithWeeks: boolean = false;
@ViewChild('tableHeader') childComponent!: TableHeaderComponent;
constructor(api: ScheduleService, route: ActivatedRoute) {
route.queryParams.subscribe(params => {
TabStorageService.selectDataFromQuery(params);
});
this.startTerm = new Date(1, 1, 1);
this.startWeek = new Date(1, 1, 1);
let disciplineWithWeeksStorage = localStorage.getItem('disciplineWithWeeks');
if (disciplineWithWeeksStorage)
this.disciplineWithWeeks = disciplineWithWeeksStorage.toLowerCase() === 'true';
api.pairPeriod().subscribe(date => {
this.pairPeriods = date;
});
api.startTerm().subscribe(date => {
this.startTerm = date.date;
this.calculateCurrentWeek();
});
}
ngOnInit(): void {
}
protected result(data: [TabsSelect, number, Observable<ScheduleResponse[]>]) {
this.isLoadTable = true;
data[2]
.pipe(catchError(error => {
this.data = [];
throw error;
}))
.subscribe(x => {
this.data = x;
switch (data[0]) {
case TabsSelect.Group:
this.childComponent.AdditionalText(AdditionalText.Group, this.data[0].group);
break;
case TabsSelect.Professor:
let indexProfessor = this.data[0].professorsId.findIndex(p => p === data[1]);
this.childComponent.AdditionalText(AdditionalText.Professor, this.data[0].professors[indexProfessor]);
break;
case TabsSelect.LectureHall:
this.childComponent.AdditionalText(AdditionalText.LectureHall, `${this.data[0].lectureHalls[0]} (${this.data[0].campus[0]})`);
break;
case TabsSelect.Other:
this.childComponent.AdditionalText(AdditionalText.Other, '');
break;
}
this.isLoadTable = false;
});
}
private calculateCurrentWeek() {
let currentDate = new Date();
if (currentDate.getDate() < this.startTerm.getDate())
currentDate = this.startTerm;
function startOfWeek(date: Date) {
return addDays(date, -date.getDay() + 1);
}
this.startWeek = currentDate.getDay() === 0 ? startOfWeek(addDays(currentDate, 1)) : startOfWeek(currentDate);
if (this.startWeek < this.startTerm)
this.startWeek = this.startTerm;
}
protected handleWeekEvent(eventData: boolean | null) {
if (eventData === null) {
this.calculateCurrentWeek();
} else if (eventData) {
this.startWeek = addDays(this.startWeek, 7);
} else {
this.startWeek = addDays(this.startWeek, -7);
}
}
get currentWeek(): number {
let result = (weekInYear(this.startWeek) - weekInYear(this.startTerm)) + 1;
if (result <= 0)
result = 1;
return result;
}
protected changeDisciplineWeeksView(checked: boolean) {
localStorage.setItem('disciplineWithWeeks', checked.toString());
this.disciplineWithWeeks = checked;
}
}