702 lines
35 KiB
React
702 lines
35 KiB
React
import { useEffect, useRef, useState } from 'react';
|
|
import { getSession } from '../../../admin/js/api.js';
|
|
import { CustomSelect } from '../../shared/ui/CustomSelect.jsx';
|
|
import { ConfirmDialog } from '../../shared/ui/ConfirmDialog.jsx';
|
|
import {
|
|
confirmAbandonDirtyChanges,
|
|
createDirtyScope,
|
|
} from '../../../admin/js/dirty-state.js';
|
|
import {
|
|
emptyPlanForm,
|
|
compatibleGroups,
|
|
fetchPlans,
|
|
fetchPlanMetadata,
|
|
fetchPlan,
|
|
savePlan,
|
|
savePlanSubject,
|
|
removePlanSubject,
|
|
assignPlan,
|
|
unassignPlan,
|
|
removePlan,
|
|
} from '../study-plans-service.js';
|
|
|
|
export function StudyPlansTab() {
|
|
const [session] = useState(() => getSession());
|
|
const editable = ['ADMIN', 'EDUCATION_OFFICE'].includes(session?.role);
|
|
const [plans, setPlans] = useState([]);
|
|
const [metadata, setMetadata] = useState(null);
|
|
const [details, setDetails] = useState(null);
|
|
const [form, setForm] = useState(emptyPlanForm);
|
|
const [phase, setPhase] = useState('loading');
|
|
const [busy, setBusy] = useState(false);
|
|
const [alert, setAlert] = useState(null);
|
|
const [query, setQuery] = useState('');
|
|
const [subjectId, setSubjectId] = useState('');
|
|
const [editingSubjectId, setEditingSubjectId] = useState(null);
|
|
const [semesterId, setSemesterId] = useState('');
|
|
const [groupId, setGroupId] = useState('');
|
|
const [confirm, setConfirm] = useState(null);
|
|
const draftChanges = useRef({ header: false, subject: false });
|
|
const request = useRef(0);
|
|
const dirty = useRef(null);
|
|
const mounted = useRef(false);
|
|
useEffect(() => {
|
|
mounted.current = true;
|
|
dirty.current = createDirtyScope('study-plans');
|
|
let active = true;
|
|
Promise.all([fetchPlans(), fetchPlanMetadata()])
|
|
.then(([list, refs]) => {
|
|
if (!active) return;
|
|
setPlans(list);
|
|
setMetadata(refs);
|
|
setPhase('ready');
|
|
})
|
|
.catch((error) => {
|
|
if (!active) return;
|
|
setPhase('error');
|
|
setAlert({ error: true, text: error.message });
|
|
});
|
|
return () => {
|
|
active = false;
|
|
mounted.current = false;
|
|
request.current++;
|
|
dirty.current?.dispose();
|
|
};
|
|
}, []);
|
|
function applyDetails(next) {
|
|
setDetails(next);
|
|
setForm(next ? { ...next.plan } : emptyPlanForm());
|
|
setSubjectId('');
|
|
setEditingSubjectId(null);
|
|
draftChanges.current = { header: false, subject: false };
|
|
dirty.current?.clear();
|
|
}
|
|
async function selectPlan(id) {
|
|
if (busy || !(await confirmAbandonDirtyChanges())) return;
|
|
const token = ++request.current;
|
|
setAlert(null);
|
|
if (!id) {
|
|
applyDetails(null);
|
|
return;
|
|
}
|
|
setBusy(true);
|
|
try {
|
|
const next = await fetchPlan(id);
|
|
if (token === request.current) applyDetails(next);
|
|
} catch (error) {
|
|
if (token === request.current)
|
|
setAlert({ error: true, text: error.message });
|
|
} finally {
|
|
if (token === request.current) setBusy(false);
|
|
}
|
|
}
|
|
async function run(action, success, resetSubject = false, part = null) {
|
|
if (busy) return;
|
|
if (!part && dirty.current?.isDirty()) {
|
|
if (!(await confirmAbandonDirtyChanges())) return;
|
|
applyDetails(details);
|
|
}
|
|
setBusy(true);
|
|
setAlert(null);
|
|
try {
|
|
const next = await action();
|
|
if (!mounted.current) return;
|
|
if (next?.plan) {
|
|
setDetails(next);
|
|
if (part === 'header' || !draftChanges.current.header)
|
|
setForm({ ...next.plan });
|
|
if (part === 'header') draftChanges.current.header = false;
|
|
if (resetSubject) draftChanges.current.subject = false;
|
|
if (resetSubject) {
|
|
setSubjectId('');
|
|
setEditingSubjectId(null);
|
|
}
|
|
}
|
|
const list = await fetchPlans();
|
|
if (!mounted.current) return;
|
|
setPlans(list);
|
|
setAlert({ text: success });
|
|
if (!draftChanges.current.header && !draftChanges.current.subject)
|
|
dirty.current?.clear();
|
|
} catch (error) {
|
|
if (mounted.current) setAlert({ error: true, text: error.message });
|
|
} finally {
|
|
if (mounted.current) setBusy(false);
|
|
}
|
|
}
|
|
function changeForm(key, value) {
|
|
draftChanges.current.header = true;
|
|
dirty.current?.mark();
|
|
setForm((current) => ({ ...current, [key]: value }));
|
|
}
|
|
async function editSubject(row) {
|
|
if (!(await confirmAbandonDirtyChanges())) return;
|
|
setForm({ ...details.plan });
|
|
draftChanges.current = { header: false, subject: false };
|
|
setSubjectId(String(row.subjectId));
|
|
setEditingSubjectId(row.subjectId);
|
|
}
|
|
const plan = details?.plan;
|
|
const visiblePlans = plans.filter(
|
|
(p) =>
|
|
p.id === plan?.id ||
|
|
`${p.title} ${p.specialtyCode} ${p.profileName}`
|
|
.toLocaleLowerCase('ru')
|
|
.includes(query.toLocaleLowerCase('ru')),
|
|
);
|
|
const groups = compatibleGroups(metadata, plan, semesterId);
|
|
const contextLocked = Boolean(details?.assignments.length);
|
|
if (phase !== 'ready')
|
|
return (
|
|
<div className="card">
|
|
<h2>Учебные планы</h2>
|
|
<p role="status">
|
|
{phase === 'loading'
|
|
? 'Загрузка учебных планов…'
|
|
: alert?.text}
|
|
</p>
|
|
{phase === 'error' && (
|
|
<button
|
|
className="btn btn-md btn-secondary"
|
|
onClick={() => window.location.reload()}
|
|
>
|
|
Повторить загрузку
|
|
</button>
|
|
)}
|
|
</div>
|
|
);
|
|
return (
|
|
<section className="study-plans-workspace" aria-label="Учебные планы">
|
|
<div className="card study-plans-catalog">
|
|
<div className="study-plan-heading">
|
|
<div>
|
|
<p className="study-plan-kicker">Учебный процесс</p>
|
|
<h2>Учебные планы</h2>
|
|
<p className="muted">
|
|
Профиль → семестр → дисциплины → группы
|
|
</p>
|
|
</div>
|
|
<div className="study-plan-row-actions">
|
|
{plan && (
|
|
<button
|
|
type="button"
|
|
className="btn btn-md btn-secondary"
|
|
disabled={busy}
|
|
onClick={() => void selectPlan(plan.id)}
|
|
>
|
|
Обновить
|
|
</button>
|
|
)}
|
|
{editable && (
|
|
<button
|
|
type="button"
|
|
className="btn btn-md btn-primary"
|
|
disabled={busy}
|
|
onClick={() => void selectPlan(null)}
|
|
>
|
|
Новый план
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="study-plan-controls">
|
|
<label className="form-group">
|
|
Поиск по названию, шифру и профилю
|
|
<input
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
type="search"
|
|
placeholder="Название или шифр специальности"
|
|
/>
|
|
</label>
|
|
<label className="form-group">
|
|
Учебный план
|
|
<CustomSelect
|
|
disabled={busy}
|
|
value={plan?.id || ''}
|
|
onChange={(e) => void selectPlan(e.target.value)}
|
|
>
|
|
<option value="">Выберите план</option>
|
|
{visiblePlans.map((p) => (
|
|
<option value={p.id} key={p.id}>
|
|
{p.title} · {p.specialtyCode} ·{' '}
|
|
{p.profileName} · {p.semesterNumber} семестр
|
|
</option>
|
|
))}
|
|
</CustomSelect>
|
|
</label>
|
|
</div>
|
|
{alert && (
|
|
<p
|
|
role={alert.error ? 'alert' : 'status'}
|
|
className={`form-alert study-plan-alert ${alert.error ? 'error' : 'success'}`}
|
|
>
|
|
{alert.text}
|
|
</p>
|
|
)}
|
|
</div>
|
|
{(plan || editable) && (
|
|
<div className="card">
|
|
<div className="study-plan-heading">
|
|
<h3>{plan ? plan.title : 'Новый учебный план'}</h3>
|
|
{plan && (
|
|
<span className="badge">
|
|
{plan.specialtyCode} · {plan.profileName}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
void run(
|
|
() => savePlan(plan?.id, form, plan?.revision),
|
|
'Учебный план сохранён',
|
|
false,
|
|
'header',
|
|
);
|
|
}}
|
|
>
|
|
<fieldset
|
|
disabled={!editable || busy}
|
|
className="study-plan-fieldset"
|
|
>
|
|
<div className="study-plan-controls">
|
|
<label className="form-group">
|
|
Название
|
|
<input
|
|
required
|
|
maxLength={255}
|
|
value={form.title}
|
|
onChange={(e) =>
|
|
changeForm('title', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="form-group">
|
|
Шифр и профиль
|
|
<CustomSelect
|
|
required
|
|
disabled={contextLocked}
|
|
value={form.specialtyProfileId}
|
|
onChange={(e) =>
|
|
changeForm(
|
|
'specialtyProfileId',
|
|
e.target.value,
|
|
)
|
|
}
|
|
>
|
|
<option value="">
|
|
Выберите профиль
|
|
</option>
|
|
{metadata.profiles.map((p) => (
|
|
<option value={p.id} key={p.id}>
|
|
{p.specialtyCode} · {p.name}
|
|
</option>
|
|
))}
|
|
</CustomSelect>
|
|
</label>
|
|
<label className="form-group">
|
|
Форма обучения
|
|
<CustomSelect
|
|
required
|
|
disabled={contextLocked}
|
|
value={form.educationFormId}
|
|
onChange={(e) =>
|
|
changeForm(
|
|
'educationFormId',
|
|
e.target.value,
|
|
)
|
|
}
|
|
>
|
|
<option value="">Выберите форму</option>
|
|
{metadata.educationForms.map((f) => (
|
|
<option key={f.id} value={f.id}>
|
|
{f.name}
|
|
</option>
|
|
))}
|
|
</CustomSelect>
|
|
</label>
|
|
<label className="form-group">
|
|
Номер семестра обучения
|
|
<input
|
|
type="number"
|
|
min="1"
|
|
max="20"
|
|
required
|
|
disabled={contextLocked}
|
|
value={form.semesterNumber}
|
|
onChange={(e) =>
|
|
changeForm(
|
|
'semesterNumber',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
</div>
|
|
{editable && (
|
|
<button
|
|
className="btn btn-md btn-primary"
|
|
type="submit"
|
|
>
|
|
{plan ? 'Сохранить план' : 'Создать план'}
|
|
</button>
|
|
)}
|
|
</fieldset>
|
|
</form>
|
|
{contextLocked && (
|
|
<p className="muted">
|
|
Профиль, форма и номер семестра закреплённого плана
|
|
защищены от изменения.
|
|
</p>
|
|
)}
|
|
{plan && editable && !contextLocked && (
|
|
<button
|
|
disabled={busy}
|
|
type="button"
|
|
className="btn btn-md btn-danger study-plan-delete"
|
|
onClick={() =>
|
|
setConfirm({
|
|
text: `Удалить план «${plan.title}» вместе с дисциплинами?`,
|
|
action: async () => {
|
|
await removePlan(
|
|
plan.id,
|
|
plan.revision,
|
|
);
|
|
applyDetails(null);
|
|
},
|
|
})
|
|
}
|
|
>
|
|
Удалить план
|
|
</button>
|
|
)}
|
|
</div>
|
|
)}
|
|
{plan && (
|
|
<>
|
|
<div className="card">
|
|
<div className="study-plan-heading">
|
|
<div>
|
|
<h3>Дисциплины плана</h3>
|
|
<p className="muted">
|
|
Часы по видам нагрузки и подгруппы задаются в календарном графике для каждой группы.
|
|
</p>
|
|
</div>
|
|
|
|
</div>
|
|
<div className="table-wrap">
|
|
<table className="study-plan-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Дисциплина</th>
|
|
{editable && <th>Действия</th>}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{!details.subjects.length && (
|
|
<tr>
|
|
<td
|
|
colSpan={editable ? 2 : 1}
|
|
className="loading-row"
|
|
>
|
|
Добавьте первую дисциплину в
|
|
план
|
|
</td>
|
|
</tr>
|
|
)}
|
|
{details.subjects.map((row) => (
|
|
<tr key={row.subjectId}>
|
|
<td>
|
|
<strong>{row.name}</strong>
|
|
<div className="muted">
|
|
{row.code}
|
|
</div>
|
|
</td>
|
|
{editable && (
|
|
<td>
|
|
<div className="study-plan-row-actions">
|
|
<button
|
|
type="button"
|
|
disabled={busy}
|
|
className="btn btn-sm btn-secondary"
|
|
onClick={() =>
|
|
void editSubject(
|
|
row,
|
|
)
|
|
}
|
|
>
|
|
Изменить
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={busy}
|
|
className="btn btn-sm btn-danger"
|
|
onClick={() =>
|
|
setConfirm({
|
|
text: `Удалить дисциплину «${row.name}» из плана?`,
|
|
action: () =>
|
|
removePlanSubject(
|
|
plan.id,
|
|
row.subjectId,
|
|
plan.revision,
|
|
),
|
|
})
|
|
}
|
|
>
|
|
Удалить
|
|
</button>
|
|
</div>
|
|
</td>
|
|
)}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
{editable && (
|
|
<form
|
|
className="study-plan-subject-editor"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
void run(
|
|
() =>
|
|
savePlanSubject(
|
|
plan.id,
|
|
subjectId,
|
|
[],
|
|
plan.revision,
|
|
),
|
|
'Дисциплина сохранена',
|
|
true,
|
|
'subject',
|
|
);
|
|
}}
|
|
>
|
|
<fieldset
|
|
className="study-plan-fieldset"
|
|
disabled={busy}
|
|
>
|
|
<h4>
|
|
{editingSubjectId
|
|
? 'Изменение дисциплины'
|
|
: 'Добавление дисциплины'}
|
|
</h4>
|
|
<label className="form-group">
|
|
Дисциплина
|
|
<CustomSelect
|
|
required
|
|
disabled={Boolean(editingSubjectId)}
|
|
value={subjectId}
|
|
onChange={(e) => {
|
|
draftChanges.current.subject = true;
|
|
dirty.current?.mark();
|
|
setSubjectId(e.target.value);
|
|
}}
|
|
>
|
|
<option value="">
|
|
Выберите дисциплину
|
|
</option>
|
|
{metadata.subjects
|
|
.filter(
|
|
(s) =>
|
|
s.id ===
|
|
editingSubjectId ||
|
|
!details.subjects.some(
|
|
(row) =>
|
|
row.subjectId ===
|
|
s.id,
|
|
),
|
|
)
|
|
.map((s) => (
|
|
<option
|
|
value={s.id}
|
|
key={s.id}
|
|
>
|
|
{s.code
|
|
? `${s.code} · `
|
|
: ''}
|
|
{s.name}
|
|
</option>
|
|
))}
|
|
</CustomSelect>
|
|
</label>
|
|
<p>Часы редактируются в <a href="#academic-calendar">календарном графике</a>, преподаватели — в <a href="#schedule-materials">материалах к расписанию</a>.</p>
|
|
<div className="study-plan-row-actions">
|
|
<button
|
|
type="submit"
|
|
className="btn btn-md btn-primary"
|
|
disabled={!subjectId}
|
|
>
|
|
Сохранить дисциплину
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="btn btn-md btn-secondary"
|
|
onClick={async () => {
|
|
if (
|
|
await confirmAbandonDirtyChanges()
|
|
) {
|
|
applyDetails(details);
|
|
}
|
|
}}
|
|
>
|
|
Очистить форму
|
|
</button>
|
|
</div>
|
|
</fieldset>
|
|
</form>
|
|
)}
|
|
</div>
|
|
<div className="card">
|
|
<h3>Закрепление за группами</h3>
|
|
<p className="muted">
|
|
На один календарный семестр группе назначается один
|
|
план её профиля и формы обучения.
|
|
</p>
|
|
<div className="table-wrap">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Группа</th>
|
|
<th>Календарный семестр</th>
|
|
{editable && <th>Действия</th>}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{!details.assignments.length && (
|
|
<tr>
|
|
<td
|
|
colSpan={editable ? 3 : 2}
|
|
className="loading-row"
|
|
>
|
|
План пока не закреплён
|
|
</td>
|
|
</tr>
|
|
)}
|
|
{details.assignments.map((a) => (
|
|
<tr
|
|
key={`${a.groupId}-${a.semesterId}`}
|
|
>
|
|
<td>{a.groupName}</td>
|
|
<td>{a.semesterTitle}</td>
|
|
{editable && (
|
|
<td>
|
|
<button
|
|
disabled={busy}
|
|
className="btn btn-sm btn-danger"
|
|
type="button"
|
|
onClick={() =>
|
|
setConfirm({
|
|
text: `Снять закрепление плана за группой ${a.groupName} (${a.semesterTitle})?`,
|
|
action: () =>
|
|
unassignPlan(
|
|
plan.id,
|
|
a.groupId,
|
|
a.semesterId,
|
|
),
|
|
})
|
|
}
|
|
>
|
|
Снять закрепление
|
|
</button>
|
|
</td>
|
|
)}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
{editable && (
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
void run(
|
|
() =>
|
|
assignPlan(
|
|
plan.id,
|
|
groupId,
|
|
semesterId,
|
|
),
|
|
'План закреплён за группой',
|
|
);
|
|
}}
|
|
>
|
|
<fieldset
|
|
className="study-plan-fieldset study-plan-controls"
|
|
disabled={busy}
|
|
>
|
|
<label className="form-group">
|
|
Календарный семестр
|
|
<CustomSelect
|
|
required
|
|
value={semesterId}
|
|
onChange={(e) => {
|
|
setSemesterId(e.target.value);
|
|
setGroupId('');
|
|
}}
|
|
>
|
|
<option value="">
|
|
Выберите семестр
|
|
</option>
|
|
{metadata.semesters.map((s) => (
|
|
<option key={s.id} value={s.id}>
|
|
{s.name}
|
|
</option>
|
|
))}
|
|
</CustomSelect>
|
|
</label>
|
|
<label className="form-group">
|
|
Совместимая группа
|
|
<CustomSelect
|
|
required
|
|
value={groupId}
|
|
onChange={(e) =>
|
|
setGroupId(e.target.value)
|
|
}
|
|
>
|
|
<option value="">
|
|
{semesterId && !groups.length
|
|
? 'Нет групп этого профиля и семестра'
|
|
: 'Выберите группу'}
|
|
</option>
|
|
{groups.map((g) => (
|
|
<option key={g.id} value={g.id}>
|
|
{g.name}
|
|
</option>
|
|
))}
|
|
</CustomSelect>
|
|
</label>
|
|
<button
|
|
type="submit"
|
|
className="btn btn-md btn-primary"
|
|
disabled={
|
|
!groupId || !details.subjects.length
|
|
}
|
|
>
|
|
Закрепить план
|
|
</button>
|
|
</fieldset>
|
|
</form>
|
|
)}
|
|
</div>
|
|
</>
|
|
)}
|
|
{confirm && (
|
|
<ConfirmDialog
|
|
title="Подтвердите действие"
|
|
message={confirm.text}
|
|
danger
|
|
confirmLabel="Подтвердить"
|
|
onClose={() => setConfirm(null)}
|
|
onConfirm={() => {
|
|
const action = confirm.action;
|
|
setConfirm(null);
|
|
void run(action, 'Изменения сохранены', true);
|
|
}}
|
|
/>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|