Plany: kopiowanie dni z dowolnego tygodnia (modal Kopiuj z... zamiast sztywnego kopiowania z poprzedniego)

This commit is contained in:
tisachi
2026-10-11 18:44:40 +02:00
parent 3385d2f1a1
commit 03c8a09990
6 changed files with 185 additions and 27 deletions

View File

@@ -389,6 +389,20 @@ export default function PlanEditor() {
const weekSummaryActive = weekSummary(activeWi, week, weekDays);
// Lustrzany tydzień (2+ nieodłączony): panel pokazuje podgląd + jawne odłączenie zamiast edytora
const mirrored = activeWi > 0 && !week.detached;
// Źródła „Kopiuj z…” — każdy inny tydzień z podsumowaniem jego wyświetlanej treści
const copySources = editor.weeks.flatMap((w, wi) => {
if (wi === activeWi) return [];
const days = displayedDaysOf(editor.weeks, wi);
const exercises = days.reduce((n, d) => n + d.exercises.length, 0);
const alias = w.customName.trim();
return [
{
wi,
label: alias ? `Tydzień ${wi + 1} — ${alias}` : `Tydzień ${wi + 1}`,
summary: `${dayCountLabel(days.length)} · ${exerciseCountLabel(exercises)}`,
},
];
});
return (
<>
@@ -474,8 +488,8 @@ export default function PlanEditor() {
summary={weekSummaryActive}
customName={week.customName}
onCustomNameChange={(v) => editor.setWeekAlias(activeWi, v)}
canCopy={activeWi > 0}
onCopyPrev={() => editor.copyPrevWeek(activeWi)}
copySources={copySources}
onCopyFrom={(srcWi) => editor.copyFromWeek(activeWi, srcWi)}
canClear={weekDays.length > 0 || !week.detached}
onClear={() => setClearWeekIdx(activeWi)}
>

View File

@@ -1,17 +1,34 @@
import type { ReactNode } from 'react';
import { useState, type ReactNode } from 'react';
import { Copy, Trash2 } from 'lucide-react';
import { Badge, Button, Card, Field, H2, Input, type BadgeColor } from '../../../components/ui';
import {
Badge,
Button,
Card,
Field,
H2,
Input,
Modal,
type BadgeColor,
} from '../../../components/ui';
/** Źródło kopiowania dni: dowolny inny tydzień (z podsumowaniem jego treści). */
export interface CopySource {
wi: number;
label: string;
summary: string;
}
/* Panel tygodnia (zamiast akordeonu): nagłówek z badge podsumowania + akcje,
treść zawsze widoczna — przełączanie tygodni odbywa się przez zakładki.
Nazwa tygodnia należy do tego panelu (edycja zawsze dotyczy wyświetlanego tygodnia). */
Nazwa tygodnia należy do tego panelu (edycja zawsze dotyczy wyświetlanego tygodnia).
„Kopiuj z…” = wybór dowolnego tygodnia źródłowego (nie tylko poprzedniego). */
export function WeekPanel({
index,
summary,
customName,
onCustomNameChange,
canCopy,
onCopyPrev,
copySources,
onCopyFrom,
canClear,
onClear,
children,
@@ -20,22 +37,23 @@ export function WeekPanel({
summary: { text: string; color: BadgeColor };
customName: string;
onCustomNameChange: (v: string) => void;
canCopy: boolean;
onCopyPrev: () => void;
copySources: CopySource[];
onCopyFrom: (srcWi: number) => void;
canClear: boolean;
onClear: () => void;
children: ReactNode;
}) {
const [copyOpen, setCopyOpen] = useState(false);
return (
<Card>
<div className="flex flex-wrap items-center gap-x-3 gap-y-2">
<H2 className="mb-0">Tydzień {index + 1}</H2>
<Badge color={summary.color}>{summary.text}</Badge>
<div className="flex items-center flex-wrap gap-2 sm:ml-auto">
{canCopy && (
<Button variant="secondary" size="sm" onClick={onCopyPrev}>
{copySources.length > 0 && (
<Button variant="secondary" size="sm" onClick={() => setCopyOpen(true)}>
<Copy className="w-4 h-4" />
Kopiuj z tyg. {index}
Kopiuj z…
</Button>
)}
{canClear && (
@@ -57,6 +75,30 @@ export function WeekPanel({
</Field>
</div>
<div>{children}</div>
<Modal open={copyOpen} onClose={() => setCopyOpen(false)} title="Kopiuj dni tygodnia" handle>
<p className="mb-3 text-dim text-[0.875rem]">
Wybierz tydzień do skopiowania — bieżący tydzień przejmie jego dni treningowe i stanie się
samodzielny.
</p>
<div className="flex flex-col gap-1">
{copySources.map((s) => (
<button
key={s.wi}
type="button"
aria-label={`Kopiuj: ${s.label}`}
onClick={() => {
onCopyFrom(s.wi);
setCopyOpen(false);
}}
className="flex cursor-pointer items-center justify-between gap-3 rounded-[10px] px-3 py-2.5 text-left text-[0.95rem] font-semibold transition-colors hover:bg-line/30"
>
<span>{s.label}</span>
<span className="shrink-0 text-dim text-[0.8rem]">{s.summary}</span>
</button>
))}
</div>
</Modal>
</Card>
);
}

View File

@@ -1,6 +1,13 @@
import { useCallback, useMemo, useRef, useState } from 'react';
import type { LatestExerciseResult } from '../../../api/types';
import { DEFAULT_SET, emptyWeek, weekFromApi, type EditorSet, type EditorWeek } from './types';
import {
DEFAULT_SET,
displayedDaysOf,
emptyWeek,
weekFromApi,
type EditorSet,
type EditorWeek,
} from './types';
/** Shape of `plan` from the API (only the fields the editor needs). */
export interface PlanLike {
@@ -267,15 +274,14 @@ export function usePlanEditorState() {
[editWeek],
);
const copyPrevWeek = useCallback((wi: number) => {
if (wi === 0) return;
/** Kopiowanie dni z dowolnego tygodnia (nie tylko poprzedniego): źródło lustrzane
rozwiązuje się przez `displayedDaysOf`, cel zostaje odłączony; aliasu nie kopiujemy. */
const copyFromWeek = useCallback((wi: number, srcWi: number) => {
setWeeks((ws) => {
const prevDays = ws[wi - 1].detached ? ws[wi - 1].days : ws[0].days;
// Alias tygodnia jest specyficzny — nie jest kopiowany z poprzedniego tygodnia.
if (wi === srcWi || !ws[wi] || !ws[srcWi]) return ws;
const srcDays = displayedDaysOf(ws, srcWi);
return ws.map((w, i) =>
i === wi
? { days: structuredClone(prevDays), detached: true, customName: w.customName }
: w,
i === wi ? { days: structuredClone(srcDays), detached: true, customName: w.customName } : w,
);
});
}, []);
@@ -328,7 +334,7 @@ export function usePlanEditorState() {
duplicateSet,
removeSet,
prefillExercise,
copyPrevWeek,
copyFromWeek,
detachWeek,
clearWeek,
};

View File

@@ -425,6 +425,47 @@ describe('PlanEditor — data startu i menu kontekstowe', () => {
expect(screen.getByText('Przysiady')).toBeInTheDocument();
});
it('krok 2: „Kopiuj z…” pozwala skopiować dni z dowolnego tygodnia (nie tylko poprzedniego)', async () => {
mockApi({});
const user = userEvent.setup();
renderApp();
await loginAndOpenNewPlan(user);
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan');
await user.click(screen.getByRole('button', { name: 'Dalej' }));
// tydzień 1: Poniedziałek + ćwiczenie
await user.click(screen.getByRole('button', { name: 'Poniedziałek' }));
await user.click(screen.getByRole('button', { name: 'dodaj ćwiczenie…' }));
await user.click(await screen.findByRole('option', { name: /Przysiady/ }));
// tydzień 2: odłącz i dodaj Środę → źródło różne od tygodnia 1
await user.click(screen.getByRole('button', { name: /Tydzień 2/ }));
await user.click(screen.getByRole('button', { name: 'Edytuj samodzielnie' }));
await user.click(
within(screen.getByRole('group', { name: 'Dni treningowe' })).getByRole('button', {
name: 'Środa',
}),
);
// tydzień 4: kopiuj z tygodnia 2 (źródło nie jest poprzednie względem celu)
await user.click(screen.getByRole('button', { name: /Tydzień 4/ }));
await user.click(screen.getByRole('button', { name: 'Kopiuj z…' }));
expect(screen.getByText('2 dni · 1 ćwiczenie')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Kopiuj: Tydzień 4' })).not.toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Kopiuj: Tydzień 2' }));
// cel przejął dni źródła i stał się samodzielny (banner lustrzany zniknął)
expect(screen.queryByText('Ten tydzień powtarza tydzień 1.')).not.toBeInTheDocument();
const dayGroup = screen.getByRole('group', { name: 'Dni treningowe' });
expect(
within(dayGroup).getByRole('button', { name: 'Poniedziałek', pressed: true }),
).toBeInTheDocument();
expect(
within(dayGroup).getByRole('button', { name: 'Środa', pressed: true }),
).toBeInTheDocument();
expect(screen.getByText('Przysiady')).toBeInTheDocument();
});
it('pokazuje „Wstecz” w treści formularza na /plans/new', async () => {
mockApi({});
const user = userEvent.setup();

View File

@@ -104,6 +104,44 @@ test('zakładki tygodni: klik taba przełącza tydzień; lustrzany — podgląd
await expect(page.getByText('Ten tydzień powtarza tydzień 1.')).toHaveCount(0);
});
test('kopiowanie dni z dowolnego tygodnia („Kopiuj z…”)', async ({ page }) => {
mockPlans(page);
await login(page);
await page.getByRole('link', { name: 'Plany' }).click();
await page.getByRole('button', { name: 'Nowy plan' }).first().click();
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
await page.getByRole('button', { name: 'Dalej' }).click();
// tydzień 1: Poniedziałek + ćwiczenie
await page.getByRole('button', { name: 'Pon' }).click();
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
await page.getByRole('option', { name: /Przysiady/ }).click();
// tydzień 2: odłącz i dodaj Środę → źródło różne od tygodnia 1
await page.getByRole('button', { name: 'Tydzień 2' }).click();
await page.getByRole('button', { name: 'Edytuj samodzielnie' }).click();
await page
.getByRole('group', { name: 'Dni treningowe' })
.getByRole('button', { name: 'Śr' })
.click();
// tydzień 4: kopiuj z tygodnia 2 (źródło nie jest poprzednie względem celu)
await page.getByRole('button', { name: 'Tydzień 4' }).click();
await page.getByRole('button', { name: 'Kopiuj z…' }).click();
const dialog = page.getByRole('dialog');
await expect(dialog.getByText('2 dni · 1 ćwiczenie')).toBeVisible();
await expect(dialog.getByRole('button', { name: 'Kopiuj: Tydzień 4' })).toHaveCount(0);
await dialog.getByRole('button', { name: 'Kopiuj: Tydzień 2' }).click();
// cel przejął dni źródła i stał się samodzielny
await expect(page.getByText('Ten tydzień powtarza tydzień 1.')).toHaveCount(0);
const dayGroup = page.getByRole('group', { name: 'Dni treningowe' });
await expect(dayGroup.getByRole('button', { name: 'Pon', pressed: true })).toBeVisible();
await expect(dayGroup.getByRole('button', { name: 'Śr', pressed: true })).toBeVisible();
await expect(page.getByText('Przysiady')).toBeVisible();
});
test('zmiana kolejności ćwiczeń i stepper serii', async ({ page }) => {
mockPlans(page);
await login(page);

View File

@@ -80,11 +80,28 @@ describe('usePlanEditorState — akcje mutacji', () => {
expect(result.current.weeks[0].days).toHaveLength(2);
});
it('copyPrevWeek odzwierciedla tydzień 1; clearWeek czyści; setWeeksCount skaluje', () => {
it('copyFromWeek kopiuje z dowolnego tygodnia (rozwiązuje lustrzane źródło; self = no-op)', () => {
const { result } = renderHook(() => usePlanEditorState());
act(() => result.current.addDay(0, 1));
act(() => result.current.addDay(0, 3));
// źródło lustrzane: tydzień 2 (indeks 1) nieodłączony → kopiowane są dni tygodnia 1
act(() => result.current.copyFromWeek(3, 1));
expect(result.current.weeks[3].detached).toBe(true);
expect(result.current.weeks[3].days.map((d) => d.dayOfWeek)).toEqual([1, 3]);
// własne dni źródła odłączonego: tydzień 3 (indeks 2) po edycji ma inny zestaw
act(() => result.current.addDay(2, 5));
act(() => result.current.copyFromWeek(0, 2));
expect(result.current.weeks[0].days.map((d) => d.dayOfWeek)).toEqual([1, 3, 5]);
// kopiowanie „z siebie” — bez zmian
act(() => result.current.copyFromWeek(2, 2));
expect(result.current.weeks[2].days).toHaveLength(3);
});
it('copyFromWeek odzwierciedla tydzień 1; clearWeek czyści; setWeeksCount skaluje', () => {
const { result } = renderHook(() => usePlanEditorState());
act(() => result.current.addDay(0, 1));
act(() => result.current.addExercise(0, 0, 'ex-1'));
act(() => result.current.copyPrevWeek(1));
act(() => result.current.copyFromWeek(1, 0));
expect(result.current.weeks[1].days).toHaveLength(1);
expect(result.current.weeks[1].detached).toBe(true);
act(() => result.current.clearWeek(1));
@@ -140,7 +157,7 @@ describe('usePlanEditorState — akcje mutacji', () => {
expect(result.current.dirty).toBe(false);
});
it('setWeekAlias ustawia alias bez odłączania tygodnia; copyPrevWeek nie kopiuje aliasu', () => {
it('setWeekAlias ustawia alias bez odłączania tygodnia; copyFromWeek nie kopiuje aliasu', () => {
const { result } = renderHook(() => usePlanEditorState());
act(() => result.current.addDay(0, 1));
act(() => result.current.setWeekAlias(1, 'Deload'));
@@ -151,12 +168,12 @@ describe('usePlanEditorState — akcje mutacji', () => {
act(() => result.current.setWeekAlias(1, 'a'.repeat(60)));
expect(result.current.weeks[1].customName).toHaveLength(40);
act(() => result.current.copyPrevWeek(1));
// copyPrevWeek kopiuje dni, ale nie alias
act(() => result.current.copyFromWeek(1, 0));
// copyFromWeek kopiuje dni, ale nie alias
expect(result.current.weeks[1].days).toHaveLength(1);
expect(result.current.weeks[1].customName).toBe('a'.repeat(40));
act(() => result.current.setWeekAlias(2, 'Przerwa'));
act(() => result.current.copyPrevWeek(2));
act(() => result.current.copyFromWeek(2, 1));
expect(result.current.weeks[2].customName).toBe('Przerwa');
});