310 lines
15 KiB
React
310 lines
15 KiB
React
import { useEffect, useRef, useState } from 'react';
|
||
import { apiClient } from '../../shared/api-client.js';
|
||
import { CustomSelect } from '../../shared/ui/CustomSelect.jsx';
|
||
import { toIsoDate } from '../../shared/dates.js';
|
||
import { loadGroupMatrix } from '../group-schedule-matrix-service.js';
|
||
import {
|
||
DAY_FULL_LABELS,
|
||
formatDate,
|
||
isoDay,
|
||
} from '../schedule-view-service.js';
|
||
export function GroupScheduleMatrix() {
|
||
const [groups, setGroups] = useState([]),
|
||
[departments, setDepartments] = useState([]);
|
||
const [selected, setSelected] = useState([]),
|
||
[departmentId, setDepartmentId] = useState(''),
|
||
[query, setQuery] = useState('');
|
||
const [date, setDate] = useState(() => toIsoDate(new Date())),
|
||
[matrix, setMatrix] = useState(null);
|
||
const [loading, setLoading] = useState(true),
|
||
[error, setError] = useState('');
|
||
const request = useRef(0);
|
||
useEffect(() => {
|
||
let active = true;
|
||
Promise.all([
|
||
apiClient.get('/api/groups'),
|
||
apiClient.get('/api/departments'),
|
||
])
|
||
.then(([list, refs]) => {
|
||
if (active) {
|
||
setGroups(list);
|
||
setDepartments(refs);
|
||
setLoading(false);
|
||
}
|
||
})
|
||
.catch((e) => {
|
||
if (active) {
|
||
setError(e.data?.message || e.message);
|
||
setLoading(false);
|
||
}
|
||
});
|
||
return () => {
|
||
active = false;
|
||
request.current++;
|
||
};
|
||
}, []);
|
||
function changeSelection(id) {
|
||
request.current++;
|
||
setMatrix(null);
|
||
setLoading(false);
|
||
setSelected((current) =>
|
||
current.includes(id)
|
||
? current.filter((item) => item !== id)
|
||
: [...current, id],
|
||
);
|
||
}
|
||
async function show(event) {
|
||
event.preventDefault();
|
||
const token = ++request.current;
|
||
setLoading(true);
|
||
setError('');
|
||
try {
|
||
const next = await loadGroupMatrix(
|
||
groups.filter((g) => selected.includes(g.id)),
|
||
departments,
|
||
date,
|
||
);
|
||
if (token === request.current) setMatrix(next);
|
||
} catch (e) {
|
||
if (token === request.current) {
|
||
setError(e.data?.message || e.message);
|
||
setMatrix(null);
|
||
}
|
||
} finally {
|
||
if (token === request.current) setLoading(false);
|
||
}
|
||
}
|
||
const visible = groups.filter(
|
||
(g) =>
|
||
(!departmentId || String(g.departmentId) === departmentId) &&
|
||
g.name
|
||
.toLocaleLowerCase('ru')
|
||
.includes(query.toLocaleLowerCase('ru')),
|
||
);
|
||
return (
|
||
<section
|
||
aria-label="Матрица расписания групп"
|
||
className="materials-workspace"
|
||
>
|
||
<div className="card">
|
||
<h3>Группы по кафедрам</h3>
|
||
<p>
|
||
Выберите до 20 групп. Столбцы объединяются по выпускающей
|
||
кафедре группы; строки — дни выбранной недели и пары.
|
||
</p>
|
||
<div className="materials-filters">
|
||
<label className="form-group">
|
||
Кафедра групп
|
||
<CustomSelect
|
||
value={departmentId}
|
||
onChange={(e) => setDepartmentId(e.target.value)}
|
||
>
|
||
<option value="">Все кафедры</option>
|
||
{departments.map((d) => (
|
||
<option key={d.id} value={d.id}>
|
||
{d.departmentName || d.name}
|
||
</option>
|
||
))}
|
||
</CustomSelect>
|
||
</label>
|
||
<label className="form-group">
|
||
Поиск группы
|
||
<input
|
||
type="search"
|
||
value={query}
|
||
onChange={(e) => setQuery(e.target.value)}
|
||
/>
|
||
</label>
|
||
</div>
|
||
<div
|
||
className="matrix-group-picker"
|
||
role="group"
|
||
aria-label="Выбор групп"
|
||
>
|
||
{visible.map((group) => (
|
||
<label key={group.id} className="checkbox-item">
|
||
<input
|
||
type="checkbox"
|
||
checked={selected.includes(group.id)}
|
||
disabled={
|
||
!selected.includes(group.id) &&
|
||
selected.length >= 20
|
||
}
|
||
onChange={() => changeSelection(group.id)}
|
||
/>
|
||
<span className="checkmark" aria-hidden="true" />
|
||
<span>{group.name}</span>
|
||
</label>
|
||
))}
|
||
</div>
|
||
<p>
|
||
Выбрано:{' '}
|
||
{groups
|
||
.filter((g) => selected.includes(g.id))
|
||
.map((g) => g.name)
|
||
.join(', ') || 'нет групп'}
|
||
</p>
|
||
<form className="materials-filters" onSubmit={show}>
|
||
<label className="form-group">
|
||
Неделя даты
|
||
<input
|
||
type="date"
|
||
required
|
||
value={date}
|
||
onChange={(e) => {
|
||
request.current++;
|
||
setDate(e.target.value);
|
||
setMatrix(null);
|
||
setLoading(false);
|
||
}}
|
||
/>
|
||
</label>
|
||
<button
|
||
className="btn btn-md btn-primary"
|
||
type="submit"
|
||
disabled={loading || !selected.length}
|
||
>
|
||
Показать матрицу
|
||
</button>
|
||
</form>
|
||
</div>
|
||
{error && (
|
||
<div role="alert" className="materials-alert is-error">
|
||
{error}
|
||
</div>
|
||
)}
|
||
{loading && <p role="status">Загрузка матрицы…</p>}
|
||
{matrix && (
|
||
<div className="card">
|
||
<h3>
|
||
{formatDate(matrix.dates[0])} —{' '}
|
||
{formatDate(matrix.dates.at(-1))}
|
||
</h3>
|
||
<div className="table-wrap matrix-scroll">
|
||
<table className="group-schedule-matrix">
|
||
<caption>
|
||
Расписание выбранных групп с учётом разовых
|
||
изменений
|
||
</caption>
|
||
<thead>
|
||
<tr>
|
||
<th rowSpan="2" scope="col">
|
||
День / пара
|
||
</th>
|
||
{matrix.sections.map((section) => (
|
||
<th
|
||
key={section.id}
|
||
scope="colgroup"
|
||
colSpan={section.groups.length}
|
||
>
|
||
{section.name}
|
||
</th>
|
||
))}
|
||
</tr>
|
||
<tr>
|
||
{matrix.columns.map((group) => (
|
||
<th key={group.id} scope="col">
|
||
{group.name}
|
||
</th>
|
||
))}
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{matrix.rows.map((row) => (
|
||
<tr key={`${row.date}-${row.order}`}>
|
||
<th scope="row">
|
||
<strong>
|
||
{
|
||
DAY_FULL_LABELS[
|
||
isoDay(row.date)
|
||
]
|
||
}{' '}
|
||
· {formatDate(row.date)}
|
||
</strong>
|
||
<small>
|
||
{row.order} пара ·{' '}
|
||
{String(
|
||
row.startTime || '',
|
||
).slice(0, 5)}
|
||
–
|
||
{String(
|
||
row.endTime || '',
|
||
).slice(0, 5)}
|
||
</small>
|
||
</th>
|
||
{row.cells.map((lessons, index) => (
|
||
<td key={matrix.columns[index].id}>
|
||
{lessons.length ? (
|
||
lessons.map((lesson, i) => (
|
||
<article
|
||
className="matrix-lesson"
|
||
key={`${lesson.scheduleRuleSlotId}-${lesson.scheduleOverrideId}-${i}`}
|
||
>
|
||
<strong>
|
||
{
|
||
lesson.subjectName
|
||
}
|
||
</strong>
|
||
<div>
|
||
{
|
||
lesson.lessonTypeName
|
||
}{' '}
|
||
·{' '}
|
||
{
|
||
lesson.teacherName
|
||
}
|
||
</div>
|
||
<div>
|
||
{lesson.classroomName ||
|
||
lesson.lessonFormat}
|
||
</div>
|
||
{lesson
|
||
.subgroupNames
|
||
?.length >
|
||
0 && (
|
||
<small>
|
||
{lesson.subgroupNames.join(
|
||
', ',
|
||
)}
|
||
</small>
|
||
)}
|
||
<small>
|
||
{String(
|
||
lesson.startTime ||
|
||
'',
|
||
).slice(0, 5)}
|
||
–
|
||
{String(
|
||
lesson.endTime ||
|
||
'',
|
||
).slice(0, 5)}
|
||
</small>
|
||
{lesson.scheduleOverrideId && (
|
||
<small>
|
||
Разовое
|
||
изменение
|
||
</small>
|
||
)}
|
||
</article>
|
||
))
|
||
) : (
|
||
<span className="muted">
|
||
—
|
||
</span>
|
||
)}
|
||
</td>
|
||
))}
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
{!matrix.rows.length && (
|
||
<p>На выбранной неделе нет пар или временных слотов.</p>
|
||
)}
|
||
</div>
|
||
)}
|
||
</section>
|
||
);
|
||
}
|