diff --git a/admin/src/pages/gyms/GymDetail.tsx b/admin/src/pages/gyms/GymDetail.tsx index 5edbe02..85126b6 100644 --- a/admin/src/pages/gyms/GymDetail.tsx +++ b/admin/src/pages/gyms/GymDetail.tsx @@ -10,6 +10,7 @@ import { Pencil, ShieldCheck, Star, + Trash2, UserPlus, UserX, Users, @@ -35,10 +36,8 @@ import { EMPTY_SPEC_FIELDS, LOAD_RANGE_KINDS, MAX_LOAD_KINDS, - formatEquipmentSpec, formatGymEquipment, hasSpecFields, - specFieldsFrom, specFromFields, type SpecFields, } from '../../lib/equipment'; @@ -46,7 +45,6 @@ import { BackButton, Button, Card, - Chip, ConfirmDialog, ErrorText, Field, @@ -795,6 +793,7 @@ export default function GymDetail() { open={equipmentOpen} onClose={() => setEquipmentOpen(false)} title="Zarządzaj urządzeniami" + className="sm:max-w-2xl" > void invalidate()} /> @@ -1024,16 +1023,15 @@ function EquipmentSection({ gym, onChanged }: { gym: Gym; onChanged: () => void const [busy, setBusy] = useState(false); const [addId, setAddId] = useState(''); const [addQty, setAddQty] = useState('1'); - const [addCustom, setAddCustom] = useState(false); const [addFields, setAddFields] = useState(EMPTY_SPEC_FIELDS); const addItem = catalog.find((e) => e.id === addId) ?? null; + // Parametry każdej pozycji admin wpisuje zawsze sam — bez podpowiedzi z katalogu. const selectAdd = (id: string) => { setAddId(id); - const item = catalog.find((e) => e.id === id); - setAddFields(item ? specFieldsFrom(item.spec) : EMPTY_SPEC_FIELDS); - setAddCustom(false); + setAddFields(EMPTY_SPEC_FIELDS); + setError(''); }; const add = async () => { @@ -1041,14 +1039,14 @@ function EquipmentSection({ gym, onChanged }: { gym: Gym; onChanged: () => void const quantity = parseInt(addQty, 10) || 1; setError(''); let spec: EquipmentSpec | null = null; - if (addCustom && hasSpecFields(addItem.kind)) { + if (hasSpecFields(addItem.kind)) { const result = specFromFields(addItem.kind, addFields); if ('error' in result) { setError(result.error); return; } if (result.spec == null) { - setError('Uzupełnij parametry lub wyłącz ich własną wartość.'); + setError('Podaj parametry urządzenia (prędkość lub nachylenie).'); return; } spec = result.spec; @@ -1061,7 +1059,6 @@ function EquipmentSection({ gym, onChanged }: { gym: Gym; onChanged: () => void }); setAddId(''); setAddQty('1'); - setAddCustom(false); setAddFields(EMPTY_SPEC_FIELDS); onChanged(); } catch (err) { @@ -1098,96 +1095,115 @@ function EquipmentSection({ gym, onChanged }: { gym: Gym; onChanged: () => void .reduce((sum, e) => sum + e.quantity, 0); return ( - <> - -
- {gym.equipment.length === 0 && ( - Brak urządzeń. +
+ {/* POZYCJE NA STANIE: wiersz = nazwa/spec + ilość + usuwanie */} +
+
+ Pozycje na stanie ({gym.equipment.length}) +
+ {gym.equipment.length === 0 ? ( +
+ Brak urządzeń — dodaj pierwsze z katalogu poniżej. +
+ ) : ( +
+ {gym.equipment.map((e) => ( +
+ + {formatGymEquipment(e)} + + { + const value = parseInt(ev.target.value, 10); + if (Number.isFinite(value) && value >= 1 && value !== e.quantity) { + void changeQty(e.id, value); + } else { + ev.target.value = String(e.quantity); + } + }} + onKeyDown={(ev) => { + if (ev.key === 'Enter') (ev.target as HTMLInputElement).blur(); + }} + /> + +
+ ))} +
)} - {gym.equipment.map((e) => ( - remove(e.id)} removeLabel={`Usuń ${e.equipment.name}`}> - {formatGymEquipment(e)} - + + {/* DODAWANIE z katalogu: pozycja zawsze z własnymi parametrami (zdjęcia pozycji zarządza właściciel w apce) */} +
+
+ Dodaj urządzenie z katalogu +
+
+
+ +
+ + { - const value = parseInt(ev.target.value, 10); - if (Number.isFinite(value) && value >= 1 && value !== e.quantity) { - void changeQty(e.id, value); - } else { - ev.target.value = String(e.quantity); - } - }} - onKeyDown={(ev) => { - if (ev.key === 'Enter') (ev.target as HTMLInputElement).blur(); - }} + value={addQty} + onChange={(e) => setAddQty(e.target.value)} + aria-label="Ilość" /> - - ))} -
-
- - setAddQty(e.target.value)} - className="w-20" - aria-label="Ilość" - /> - -
- {addItem && hasSpecFields(addItem.kind) && ( -
- - {addCustom && ( + + +
+ {addItem && hasSpecFields(addItem.kind) && ( +
+
+ Parametry +
setAddFields({ ...addFields, ...patch })} /> - )} -
- )} +
+ )} +
{error && {error}} {isLoading && } - +
); } diff --git a/admin/test/GymDetail.test.tsx b/admin/test/GymDetail.test.tsx index bbfaf4f..d7f6f6b 100644 --- a/admin/test/GymDetail.test.tsx +++ b/admin/test/GymDetail.test.tsx @@ -166,14 +166,21 @@ describe('admin GymDetail page', () => { fireEvent.click(actionButton('Urządzenia')); const dialog = await screen.findByRole('dialog'); - // select pokazuje cały katalog ze specyfikacją — sprzęt już dodany jest oznaczony bieżącą ilością + // select pokazuje cały katalog — sprzęt już dodany jest oznaczony bieżącą ilością expect( - await within(dialog).findByRole('option', { name: 'hantle · 2.5–25 kg (2 szt.)' }), + await within(dialog).findByRole('option', { name: 'hantle (na stanie: 2)' }), ).toBeInTheDocument(); fireEvent.change(within(dialog).getByRole('combobox', { name: 'Dodaj urządzenie' }), { target: { value: 'eq-2' }, }); - fireEvent.click(within(dialog).getByRole('button', { name: 'Dodaj urządzenie' })); + // pozycja z katalogu DUMBBELL wymaga własnych parametrów — zawsze wpisywanych przez admina + fireEvent.change(within(dialog).getByRole('spinbutton', { name: 'Min. obciążenie (kg)' }), { + target: { value: '2.5' }, + }); + fireEvent.change(within(dialog).getByRole('spinbutton', { name: 'Max. obciążenie (kg)' }), { + target: { value: '25' }, + }); + fireEvent.click(within(dialog).getByRole('button', { name: 'Dodaj', exact: true })); // każde dodanie = POST tworzący NOWĄ instancję (bez PATCH-a ilości istniejącej) await waitFor(() => @@ -183,7 +190,7 @@ describe('admin GymDetail page', () => { ); expect( calls.find((c) => c.method === 'POST' && c.url.endsWith('/admin/gyms/g1/equipment'))?.body, - ).toEqual({ equipmentId: 'eq-2', quantity: 1 }); + ).toEqual({ equipmentId: 'eq-2', quantity: 1, spec: { minKg: 2.5, maxKg: 25 } }); expect(calls.some((c) => c.method === 'PATCH')).toBe(false); }); @@ -210,9 +217,7 @@ describe('admin GymDetail page', () => { fireEvent.click(await within(dialog).findByRole('option', { name: /Jan Kowalski/ })); fireEvent.click(within(dialog).getByRole('button', { name: 'Nadaj właściciela' })); await waitFor(() => - expect( - calls.find((c) => c.method === 'POST' && c.url.endsWith('/owners')), - ).toBeTruthy(), + expect(calls.find((c) => c.method === 'POST' && c.url.endsWith('/owners'))).toBeTruthy(), ); expect(calls.find((c) => c.method === 'POST' && c.url.endsWith('/owners'))?.body).toEqual({ userId: 'u1', diff --git a/admin/test/e2e/admin.spec.ts b/admin/test/e2e/admin.spec.ts index 88a96c2..ff07620 100644 --- a/admin/test/e2e/admin.spec.ts +++ b/admin/test/e2e/admin.spec.ts @@ -44,7 +44,7 @@ test('ponowne dodanie tego samego typu tworzy osobną instancję (bez inkrementa (req) => req.url().endsWith('/admin/gyms/gym-1/equipment') && req.method() === 'POST', ); await page.getByRole('combobox', { name: 'Dodaj urządzenie' }).selectOption('eq-2'); - await page.getByRole('button', { name: 'Dodaj urządzenie' }).click(); + await page.getByRole('button', { name: 'Dodaj', exact: true }).click(); expect((await addReq).postDataJSON()).toMatchObject({ equipmentId: 'eq-2', quantity: 1 }); // dotychczasowa instancja bez zmian, nowa instancja to osobny chip (×1 = bez licznika) @@ -63,9 +63,8 @@ test('dodanie urządzenia z własnymi parametrami wysyła spec w POST', async ({ (req) => req.url().endsWith('/admin/gyms/gym-1/equipment') && req.method() === 'POST', ); await page.getByRole('combobox', { name: 'Dodaj urządzenie' }).selectOption('eq-1'); - await page.getByRole('checkbox', { name: /Ustaw własne parametry/ }).check(); await page.getByLabel('Maksymalne obciążenie (kg)').fill('80'); - await page.getByRole('button', { name: 'Dodaj urządzenie' }).click(); + await page.getByRole('button', { name: 'Dodaj', exact: true }).click(); expect((await addReq).postDataJSON()).toEqual({ equipmentId: 'eq-1', quantity: 1, diff --git a/app/src/pages/gyms/GymDetail.tsx b/app/src/pages/gyms/GymDetail.tsx index cf3896a..f7665f0 100644 --- a/app/src/pages/gyms/GymDetail.tsx +++ b/app/src/pages/gyms/GymDetail.tsx @@ -667,6 +667,7 @@ export default function GymDetail() { {}} /> setEquipmentOpen(false)} title="Zarządzaj urządzeniami" diff --git a/app/src/pages/gyms/GymSections.tsx b/app/src/pages/gyms/GymSections.tsx index e97b825..c9fad81 100644 --- a/app/src/pages/gyms/GymSections.tsx +++ b/app/src/pages/gyms/GymSections.tsx @@ -1,5 +1,6 @@ import { useState } from 'react'; import { useQueryClient } from '@tanstack/react-query'; +import { Check, Dumbbell, ImageIcon, Trash2, X } from 'lucide-react'; import { addEquipmentPhotos, addGymEquipment, @@ -14,14 +15,13 @@ import { EMPTY_SPEC_FIELDS, LOAD_RANGE_KINDS, MAX_LOAD_KINDS, - formatEquipmentSpec, formatGymEquipment, hasSpecFields, specFieldsFrom, specFromFields, type SpecFields, } from '../../lib/equipment'; -import { Button, Chip, Field, Input, Loading, SectionHeader, Select } from '../../components/ui'; +import { Button, Field, Input, Loading, Select } from '../../components/ui'; import { AuthImage } from '../../components/AuthImage'; import { useToast } from '../../components/toast'; import { AvailabilityEditor } from '../../components/AvailabilityEditor'; @@ -151,34 +151,59 @@ function PhotoPicker({ }) { const { push } = useToast(); return ( -
- { - const picked = Array.from(e.target.files ?? []); - e.target.value = ''; - if (picked.length === 0) return; - if (files.length + picked.length > MAX_EQUIPMENT_PHOTO_FILES) { - push(`Maksymalnie ${MAX_EQUIPMENT_PHOTO_FILES} zdjęć na urządzenie`, 'error'); - return; - } - if (picked.some((f) => !EQUIPMENT_PHOTO_TYPES.includes(f.type))) { - push('Dozwolone formaty: JPG, PNG, WebP', 'error'); - return; - } - if (picked.some((f) => f.size > MAX_EQUIPMENT_PHOTO_SIZE)) { - push('Maksymalny rozmiar zdjęcia to 5 MB', 'error'); - return; - } - onChange([...files, ...picked]); - }} - /> +
+
+ + JPG, PNG lub WebP · maks. 5 MB +
{files.length > 0 && ( - {files.map((f) => f.name).join(', ')} +
+ {files.map((f, i) => ( + + {f.name} + + + ))} +
)}
); @@ -275,15 +300,14 @@ export function EquipmentSection({ gym }: { gym: Gym }) { const { data: eqData, isLoading } = useEquipment(); const catalog = eqData?.equipment ?? []; - const [busy, setBusy] = useState(false); const [addId, setAddId] = useState(''); const [addQty, setAddQty] = useState('1'); - const [addCustom, setAddCustom] = useState(false); const [addFields, setAddFields] = useState(EMPTY_SPEC_FIELDS); const [addFiles, setAddFiles] = useState([]); + const [addBusy, setAddBusy] = useState(false); + const [addPhotosOpen, setAddPhotosOpen] = useState(false); const [editingId, setEditingId] = useState(null); - const [editCustom, setEditCustom] = useState(false); const [editFields, setEditFields] = useState(EMPTY_SPEC_FIELDS); const [editFiles, setEditFiles] = useState([]); const [savingEdit, setSavingEdit] = useState(false); @@ -296,33 +320,42 @@ export function EquipmentSection({ gym }: { gym: Gym }) { queryClient.invalidateQueries({ queryKey: ['gym', gym.id] }), ]); + // Parametry każdej pozycji właściciel wpisuje sam — bez podpowiedzi z katalogu. const selectAdd = (id: string) => { setAddId(id); - const item = catalog.find((e) => e.id === id); - setAddFields(item ? specFieldsFrom(item.spec) : EMPTY_SPEC_FIELDS); - setAddCustom(false); + setAddFields(EMPTY_SPEC_FIELDS); + setAddFiles([]); + setAddPhotosOpen(false); + }; + + const resetAdd = () => { + setAddId(''); + setAddQty('1'); + setAddFields(EMPTY_SPEC_FIELDS); + setAddFiles([]); + setAddPhotosOpen(false); }; const add = async () => { if (!addItem) return; const quantity = parseInt(addQty, 10) || 1; let spec: EquipmentSpec | null = null; - if (addCustom && hasSpecFields(addItem.kind)) { + if (hasSpecFields(addItem.kind)) { const result = specFromFields(addItem.kind, addFields); if ('error' in result) { push(result.error, 'error'); return; } if (result.spec == null) { - push('Uzupełnij parametry lub wyłącz ich własną wartość.', 'error'); + push('Podaj parametry urządzenia (prędkość lub nachylenie).', 'error'); return; } spec = result.spec; } - setBusy(true); + setAddBusy(true); try { const { instance } = await addGymEquipment(gym.id, addItem.id, quantity, spec); - if (addFiles.length > 0) { + if (addPhotosOpen && addFiles.length > 0) { try { await addEquipmentPhotos(gym.id, instance.id, addFiles); } catch { @@ -330,16 +363,12 @@ export function EquipmentSection({ gym }: { gym: Gym }) { } } push('Sprzęt dodany', 'success'); - setAddId(''); - setAddQty('1'); - setAddCustom(false); - setAddFields(EMPTY_SPEC_FIELDS); - setAddFiles([]); + resetAdd(); await invalidate(); } catch (err) { push((err as Error).message, 'error'); } finally { - setBusy(false); + setAddBusy(false); } }; @@ -367,7 +396,6 @@ export function EquipmentSection({ gym }: { gym: Gym }) { const openEdit = (ge: GymEquipment) => { setEditingId(ge.id); - setEditCustom(ge.spec != null); setEditFields(specFieldsFrom(ge.spec ?? ge.equipment.spec)); setEditFiles([]); }; @@ -379,26 +407,24 @@ export function EquipmentSection({ gym }: { gym: Gym }) { const saveEdit = async () => { if (!editing) return; - let specBody: EquipmentSpec | null | undefined; - if (editCustom && hasSpecFields(editing.equipment.kind)) { + let specBody: EquipmentSpec | null = null; + if (hasSpecFields(editing.equipment.kind)) { const result = specFromFields(editing.equipment.kind, editFields); if ('error' in result) { push(result.error, 'error'); return; } if (result.spec == null) { - push('Uzupełnij parametry lub wróć do specyfikacji katalogu.', 'error'); + push('Podaj parametry lub przywróć specyfikację katalogu.', 'error'); return; } specBody = result.spec; - } else if (!editCustom && editing.spec != null) { + } else if (editing.spec != null) { specBody = null; } setSavingEdit(true); try { - if (specBody !== undefined) { - await updateGymEquipment(gym.id, editing.id, { spec: specBody }); - } + await updateGymEquipment(gym.id, editing.id, { spec: specBody }); if (editFiles.length > 0) { try { await addEquipmentPhotos(gym.id, editing.id, editFiles); @@ -416,6 +442,21 @@ export function EquipmentSection({ gym }: { gym: Gym }) { } }; + const restoreCatalogSpec = async () => { + if (!editing) return; + setSavingEdit(true); + try { + await updateGymEquipment(gym.id, editing.id, { spec: null }); + push('Przywrócono specyfikację katalogu', 'success'); + closeEdit(); + await invalidate(); + } catch (err) { + push((err as Error).message, 'error'); + } finally { + setSavingEdit(false); + } + }; + const removePhoto = async (ge: GymEquipment, photoId: string) => { try { await deleteEquipmentPhoto(gym.id, ge.id, photoId); @@ -431,205 +472,240 @@ export function EquipmentSection({ gym }: { gym: Gym }) { .reduce((sum, e) => sum + e.quantity, 0); return ( - <> - -
- {gym.equipment.length === 0 && ( - Brak urządzeń. - )} - {gym.equipment.map((e) => ( - remove(e.id, e.equipment.name)} - removeLabel={`Usuń ${e.equipment.name}`} - className={editingId === e.id ? 'border-accent' : undefined} - > - {e.photos && e.photos.length > 0 && ( - - )} - - { - const value = parseInt(ev.target.value, 10); - if (Number.isFinite(value) && value >= 1 && value !== e.quantity) { - void changeQty(e.id, value); - } else { - ev.target.value = String(e.quantity); - } - }} - onKeyDown={(ev) => { - if (ev.key === 'Enter') (ev.target as HTMLInputElement).blur(); - }} - /> - - ))} -
- {editing && ( -
-
- - {editing.equipment.name} — edycja - - +
+ {isLoading && } + {/* POZYCJE NA STANIE: wiersz = miniatura + nazwa/spec + ilość + akcje */} +
+
+ Pozycje na stanie ({gym.equipment.length}) +
+ {gym.equipment.length === 0 ? ( +
+ Brak urządzeń — dodaj pierwsze z katalogu poniżej.
- {hasSpecFields(editing.equipment.kind) && ( -
-
-
- )} -
- - setAddQty(e.target.value)} - className="w-20" - aria-label="Ilość" - /> - + )}
- {addItem && hasSpecFields(addItem.kind) && ( -
- - {addCustom && ( - setAddFields({ ...addFields, ...patch })} - /> - )} + + {/* DODAWANIE z katalogu: pozycja zawsze z własnymi parametrami i opcjonalnymi zdjęciami */} +
+
+ Dodaj urządzenie z katalogu
- )} - {addItem && ( -
- +
+
+ +
+ + setAddQty(e.target.value)} + aria-label="Ilość" + /> + +
- )} - {isLoading && } - + {addItem && ( +
+ {hasSpecFields(addItem.kind) && ( +
+
+ Parametry +
+ setAddFields({ ...addFields, ...patch })} + /> +
+ )} +
+ + {addPhotosOpen && ( + + )} +
+
+ )} +
+
); } diff --git a/app/test/e2e/gyms.spec.ts b/app/test/e2e/gyms.spec.ts index c682162..9488fea 100644 --- a/app/test/e2e/gyms.spec.ts +++ b/app/test/e2e/gyms.spec.ts @@ -130,8 +130,10 @@ test('ponowne dodanie tego samego typu tworzy osobną instancję urządzenia', a // zarządzanie sprzętem z menu kontekstowego (grupa „Urządzenia” → modal) await page.locator('button[data-slot="button"]', { hasText: 'Urządzenia' }).click(); const dialog = page.getByRole('dialog'); - // każde dodanie = NOWA instancja (osobny chip), bez inkrementacji ilości + // każde dodanie = NOWA instancja z własnymi parametrami (bez inkrementacji ilości) await dialog.getByLabel('Dodaj urządzenie').selectOption('eq-2'); + await dialog.getByLabel('Min. obciążenie (kg)').fill('2.5'); + await dialog.getByLabel('Max. obciążenie (kg)').fill('25'); await dialog.getByRole('button', { name: 'Dodaj', exact: true }).click(); await expect(dialog.getByText('Hantle · 2.5–25 kg', { exact: true })).toBeVisible(); await expect(dialog.getByText('Hantle ×2 · 2.5–25 kg', { exact: true })).toBeVisible(); @@ -170,7 +172,6 @@ test('właściciel może nadać instancji własne parametry (nadpisanie speca)', // panel edycji instancji Hantle: własny zakres 5–5 kg = ciężar stały „5 kg” await dialog.getByRole('button', { name: 'Edytuj Hantle' }).click(); - await dialog.getByRole('checkbox', { name: /Własne parametry/ }).check(); await dialog.getByLabel('Min. obciążenie (kg)').fill('5'); await dialog.getByLabel('Max. obciążenie (kg)').fill('5'); await dialog.getByRole('button', { name: 'Zapisz' }).click();