Trening: przyczyna braku mozliwosci dodania serii jako alert-modal (przycisk klikalny, komunikat w oknie z OK)

This commit is contained in:
Tilumca
2026-10-14 21:44:11 +02:00
parent ff1d26ab9f
commit 44dae4e953
4 changed files with 83 additions and 43 deletions

View File

@@ -100,6 +100,30 @@ export function ConfirmDialog({
);
}
/* Alert — informacyjny modal z jednym przyciskiem (OK); brak akcji po drugiej stronie. */
export function AlertModal({
open,
onClose,
title,
message,
}: {
open: boolean;
onClose: () => void;
title: string;
message: string;
}) {
return (
<Modal open={open} onClose={onClose} title={title}>
<p className="text-dim text-[0.95rem]">{message}</p>
<div className="flex flex-col-reverse sm:flex-row sm:justify-end gap-3 mt-5">
<Button onClick={onClose} className="w-full sm:w-auto">
OK
</Button>
</div>
</Modal>
);
}
/* Number pad dialog — mobile-friendly numeric entry (keypad + Wpisz/Anuluj). */
export function NumberPadDialog({
open,

View File

@@ -26,6 +26,7 @@ import {
Button,
Card,
ConfirmDialog,
AlertModal,
ErrorText,
H3,
Modal,
@@ -272,8 +273,6 @@ function ExerciseAccordion({
expanded,
inProgress,
nextSetId,
canAddExtra,
addSetHint,
onToggle,
onRemove,
onEditSet,
@@ -286,8 +285,6 @@ function ExerciseAccordion({
expanded: boolean;
inProgress: boolean;
nextSetId: string | null;
canAddExtra: boolean;
addSetHint: string | null;
onToggle: () => void;
onRemove: () => void;
onEditSet: (set: SessionSet, index: number) => void;
@@ -370,15 +367,10 @@ function ExerciseAccordion({
))}
</div>
{inProgress && (
<div className="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1">
<Button variant="secondary" size="sm" onClick={onAddSet} disabled={!canAddExtra}>
<Plus className="w-4 h-4" />
Dodaj serię
</Button>
{!canAddExtra && addSetHint && (
<p className="text-xs text-dim">{addSetHint}</p>
)}
</div>
<Button variant="secondary" size="sm" className="mt-2" onClick={onAddSet}>
<Plus className="w-4 h-4" />
Dodaj serię
</Button>
)}
</>
)}
@@ -435,6 +427,8 @@ export default function DayExecution() {
const [busy, setBusy] = useState(false);
const [confirmAbandon, setConfirmAbandon] = useState(false);
const [addExerciseOpen, setAddExerciseOpen] = useState(false);
// powód zablokowania „Dodaj serię" pokazany w alercie (null = alert zamknięty)
const [addSetAlert, setAddSetAlert] = useState<string | null>(null);
// Seria edytowana w oknie „Uzupełnij serię" (wsadowo: wszystkie pola naraz).
// `planned` = wartości wyznaczonej normy (do przycisku „Norma spełniona"),
// `restSec` = zaplanowany czas przerwy po serii (do timera).
@@ -496,8 +490,15 @@ export default function DayExecution() {
const extraPending = extraSets.filter((s) => s.reps == null).length;
const extraDone = extraSets.filter((s) => s.reps != null).length;
const nextSetId = allSets.find((s) => s.reps == null)?.id ?? null;
const canAddExtra = (exSets: { planSetId: string | null; reps: number | null }[]) =>
extraPending === 0 && !exSets.some((s) => s.planSetId != null && s.reps == null);
// Powód blokady „Dodaj serię" (null = można); pokazujemy go w alercie po kliknięciu.
const addSetBlockedReason = (ex?: SessionExercise): string | null =>
!ex
? null
: ex.sets.some((s) => s.planSetId != null && s.reps == null)
? 'Najpierw zapisz wszystkie serie normowe tego ćwiczenia'
: extraPending > 0
? 'W sesji jest już niezapisana seria dodatkowa — najpierw ją uzupełnij'
: null;
// Akordeon: rozwinięte jest ćwiczenie zawierające następną serię (aktywne);
// kliknięcie nagłówka przełącza rozwinięcie (jedno ćwiczenie naraz).
@@ -665,6 +666,12 @@ export default function DayExecution() {
const addSet = async (exerciseId: string) => {
if (!session || addingRef.current) return;
// blokada po stronie klienta z komunikatem w alercie (serwer broni się dodatkowo)
const blocked = addSetBlockedReason(session.exercises.find((e) => e.id === exerciseId));
if (blocked) {
setAddSetAlert(blocked);
return;
}
addingRef.current = true;
try {
await addSessionSet(exerciseId);
@@ -794,14 +801,6 @@ export default function DayExecution() {
expanded={expandedExId === ex.id}
inProgress={session.status === 'IN_PROGRESS'}
nextSetId={nextSetId}
canAddExtra={canAddExtra(ex.sets)}
addSetHint={
ex.sets.some((s) => s.planSetId != null && s.reps == null)
? 'Najpierw zapisz wszystkie serie normowe tego ćwiczenia'
: extraPending > 0
? 'W sesji jest już niezapisana seria dodatkowa — najpierw ją uzupełnij'
: null
}
onToggle={() => toggleEx(ex.id)}
onRemove={() => removeExercise(ex)}
onEditSet={(s, i) => {
@@ -844,6 +843,13 @@ export default function DayExecution() {
busy={busy}
/>
<AlertModal
open={addSetAlert !== null}
onClose={() => setAddSetAlert(null)}
title="Nie można dodać serii"
message={addSetAlert ?? ''}
/>
<SetEditDialog
open={editSet != null}
setIndex={editSet?.index ?? 0}

View File

@@ -302,25 +302,30 @@ describe('Kolejka serii w sesji', () => {
expect(completeBtn).toBeDisabled();
expect(screen.getByText(/została 1 seria/)).toBeInTheDocument();
// „Dodaj serię” zablokowany: własna norma niedokończona + aktywna seria bonusowa (1 na sesję)
expect(screen.getAllByRole('button', { name: 'Dodaj serię' })[0]).toBeDisabled();
// przy zablokowanym przycisku widać przyczynę (norma ma priorytet nad limitem bonusu)
expect(screen.getByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia')).toBeInTheDocument();
// klik „Dodaj serię” przy niedokończonej normie => alert z przyczyną (norma przed limitem bonusu)
await user.click(screen.getByRole('button', { name: 'Dodaj serię' }));
expect(
await screen.findByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'OK' }));
expect(
screen.queryByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).not.toBeInTheDocument();
});
it('pokazuje przyczynę blokady „Dodaj serię”, gdy w sesji trwa niezapisana seria dodatkowa', async () => {
it('pokazuje alert z przyczyną, gdy w sesji trwa niezapisana seria dodatkowa', async () => {
mockApi({ 'ss-1': 10, 'ss-2': 10, 'ss-3': null });
const user = userEvent.setup();
renderApp();
await openActiveDay(user);
// norma zrobiona (100%), ale ss-3 to bonus w toku => przycisk zablokowany + komunikat
// norma zrobiona (100%), ale ss-3 to bonus w toku => klik „Dodaj serię” daje alert
expect(await screen.findByText('100%')).toBeInTheDocument();
const btn = screen.getByRole('button', { name: 'Dodaj serię' });
expect(btn).toBeDisabled();
await user.click(screen.getByRole('button', { name: 'Dodaj serię' }));
expect(
screen.getByText('W sesji jest już niezapisana seria dodatkowa — najpierw ją uzupełnij'),
await screen.findByText('W sesji jest już niezapisana seria dodatkowa — najpierw ją uzupełnij'),
).toBeInTheDocument();
expect(screen.getByRole('dialog', { name: /Nie można dodać serii/ })).toBeInTheDocument();
expect(
screen.queryByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).not.toBeInTheDocument();
@@ -332,15 +337,16 @@ describe('Kolejka serii w sesji', () => {
renderApp();
await openActiveDay(user);
// Przysiady (rozwinięte): własna norma niedokończona (ss-2) => brak bonusu + przyczyna
expect(screen.getByRole('button', { name: 'Dodaj serię' })).toBeDisabled();
expect(screen.getByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia')).toBeInTheDocument();
// Wyciskanie (dodane do sesji) nie ma normy planowej => bonus dostępny mimo ss-2, bez komunikatu
await user.click(screen.getByRole('button', { name: /Wyciskanie/ }));
expect(screen.getByRole('button', { name: 'Dodaj serię' })).toBeEnabled();
// Przysiady (rozwinięte): własna norma niedokończona (ss-2) => klik daje alert z przyczyną
await user.click(screen.getByRole('button', { name: 'Dodaj serię' }));
expect(
screen.queryByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).not.toBeInTheDocument();
await screen.findByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'OK' }));
// Wyciskanie (dodane do sesji) nie ma normy planowej => bonus dostępny mimo ss-2, bez alertu
await user.click(screen.getByRole('button', { name: /Wyciskanie/ }));
await user.click(screen.getByRole('button', { name: 'Dodaj serię' }));
expect(screen.queryByRole('dialog', { name: /Nie można dodać serii/ })).not.toBeInTheDocument();
});
it('po zakończeniu wszystkich serii normowych odblokowuje ukończenie sesji', async () => {

View File

@@ -25,12 +25,16 @@ test('wyszczególnia założenia zaplanowanej serii i oznacza ćwiczenie dodane'
await expect(page.getByText('0%')).toBeVisible();
await expect(page.getByText('Następna', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: 'Ukończ sesję' })).toBeDisabled();
// Przysiady: własna norma (ss-1) niedokończona => brak bonusu; Wyciskanie blokuje
// limit jednej aktywnej serii bonusowej w sesji (ss-2 niezapisana)
await expect(page.getByRole('button', { name: 'Dodaj serię' }).first()).toBeDisabled();
// klik „Dodaj serię” przy niedokończonej normie (Przysiady, ss-1) => alert z przyczyną;
// w Wyciskaniu dodatkowo trzyma limit jednej niezapisanej serii bonusowej w sesji (ss-2)
await page.getByRole('button', { name: 'Dodaj serię' }).first().click();
await expect(
page.getByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).toBeVisible();
await page.getByRole('button', { name: 'OK' }).click();
await expect(
page.getByText('Najpierw zapisz wszystkie serie normowe tego ćwiczenia'),
).toHaveCount(0);
});
test('uzupełnia serię wsadowo przez okno „Uzupełnij serię”', async ({ page }) => {