From 4bfd919bbc4082f0ecf898d64ae0b6cc8fa7bc4c Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 30 Aug 2024 01:30:34 +0300 Subject: [PATCH] feat: grouped pair by group --- .../schedule/table/table.component.html | 19 +++---- .../schedule/table/table.component.ts | 57 +++++++++++-------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/components/schedule/table/table.component.html b/src/components/schedule/table/table.component.html index 4c486c9..ad828ed 100644 --- a/src/components/schedule/table/table.component.html +++ b/src/components/schedule/table/table.component.html @@ -1,4 +1,5 @@ -
+
@if (dataSource.length === 0 || isLoad) {
@if (isLoad) { @@ -53,7 +54,7 @@ @if ($index !== 0) {
} -
({{typeOfOccupation}})
+
({{ typeOfOccupation }})
} @@ -97,14 +98,12 @@ } - @if (!isOneGroup) { -
- - - - {{ elementData["group"] }} -
- } +
+ + + + {{ elementData["group"] }} +
@if ($index + 1 !== element.data[daysOfWeek.indexOf(day) + 1].length) {
diff --git a/src/components/schedule/table/table.component.ts b/src/components/schedule/table/table.component.ts index 162fbff..add7aca 100644 --- a/src/components/schedule/table/table.component.ts +++ b/src/components/schedule/table/table.component.ts @@ -31,17 +31,16 @@ interface Dictionary { }) export class TableComponent implements OnChanges { + @Input() currentWeek!: number; + @Input() startWeek!: Date; + @Input() isLoad: boolean = false; + private isDisciplineWithWeeks: boolean = false; protected tableDataSource: MatTableDataSource = new MatTableDataSource([]); private backupDisciplines: string[] = []; protected daysOfWeek: string[] = ['Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота']; protected displayedColumns: string[] = ['pairNumber']; protected dataSource: ScheduleResponse[] = []; - protected isOneGroup: boolean = false; - - @Input() currentWeek!: number; - @Input() startWeek!: Date; - @Input() isLoad: boolean = false; @Input() set disciplineWithWeeks(value: boolean) { this.isDisciplineWithWeeks = value; @@ -51,7 +50,6 @@ export class TableComponent implements OnChanges { @Input() set data(schedule: ScheduleResponse[]) { this.dataSource = schedule; this.convertData(); - this.isOneGroup = schedule.every((item, _, array) => item.group === array[0].group); } ngOnChanges(changes: any) { @@ -68,16 +66,16 @@ export class TableComponent implements OnChanges { this.isLoad = true; let tableData: TableData[] = []; - for (let i: number = 1; i <= 7; i++) { + for (let pairNumber: number = 1; pairNumber <= 7; pairNumber++) { let convertedData: TableData = { - pairNumber: i, + pairNumber: pairNumber, data: {} }; - for (let k: number = 1; k < 7; k++) { + for (let dayOfWeek: number = 1; dayOfWeek < 7; dayOfWeek++) { let filteredData = this.dataSource.filter(x => - x.pairNumber === i && - x.dayOfWeek === k && + x.pairNumber === pairNumber && + x.dayOfWeek === dayOfWeek && x.isEven === (this.currentWeek % 2 === 0) ); @@ -85,25 +83,34 @@ export class TableComponent implements OnChanges { filteredData = filteredData.filter(x => x.isExcludedWeeks == undefined || x.weeks == undefined || - (x.isExcludedWeeks && (!x.weeks.includes(this.currentWeek))) || - (!x.isExcludedWeeks && (x.weeks.includes(this.currentWeek))) + (x.isExcludedWeeks && !x.weeks.includes(this.currentWeek)) || + (!x.isExcludedWeeks && x.weeks.includes(this.currentWeek)) ); - filteredData.forEach(x => { - if (this.isDisciplineWithWeeks) { - if (x.isExcludedWeeks != undefined && x.weeks != undefined) { - if (this.backupDisciplines[x.disciplineId]) - x.discipline = this.backupDisciplines[x.disciplineId]; - else - this.backupDisciplines[x.disciplineId] = x.discipline; + const groupedData = filteredData.reduce((acc, item) => { + const key = `${item.lectureHalls}-${item.campus}-${item.discipline}-${item.professors.join(', ')}-${item.isExcludedWeeks}-${item.weeks?.join(', ') || ''}`; + if (!acc[key]) + acc[key] = {...item, groups: [item.group]}; + else + acc[key].groups.push(item.group); + return acc; + }, {} as { [key: string]: ScheduleResponse & { groups: string[] } }); - x.discipline = `${(x.isExcludedWeeks ? 'кр.' : 'н.')} ${x.weeks.join(', ')} ${x.discipline}`; + convertedData.data[dayOfWeek.toString()] = Object.values(groupedData).map(item => { + item.group = item.groups.join(', '); + + if (this.isDisciplineWithWeeks && item.weeks && item.isExcludedWeeks !== undefined) { + if (this.backupDisciplines[item.disciplineId]) { + item.discipline = this.backupDisciplines[item.disciplineId]; + } else { + this.backupDisciplines[item.disciplineId] = item.discipline; } - } else if (this.backupDisciplines[x.disciplineId]) - x.discipline = this.backupDisciplines[x.disciplineId]; - }); + item.discipline = `${item.isExcludedWeeks ? 'кр.' : 'н.'} ${item.weeks.join(', ')} ${item.discipline}`; + } else if (this.backupDisciplines[item.disciplineId]) + item.discipline = this.backupDisciplines[item.disciplineId]; - convertedData.data[k.toString()] = filteredData; + return item; + }); } tableData.push(convertedData);