300 lines
9.8 KiB
TypeScript
300 lines
9.8 KiB
TypeScript
import { useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { Trash2 } from 'lucide-react';
|
|
import { api } from '../../api/client';
|
|
import type { Equipment, EquipmentSpec, Gym } from '../../api/types';
|
|
import {
|
|
EMPTY_SPEC_FIELDS,
|
|
LOAD_RANGE_KINDS,
|
|
MAX_LOAD_KINDS,
|
|
formatGymEquipment,
|
|
hasSpecFields,
|
|
specFromFields,
|
|
type SpecFields,
|
|
} from '../../lib/equipment';
|
|
import { localizedName } from '../../lib/localizedName';
|
|
import { Button, ErrorText, Field, Input, Loading, Select } from '../../components/ui';
|
|
|
|
/** Pola specyfikacji zależne od rodzaju urządzenia (wzorzec: katalog admina). */
|
|
function AddSpecFields({
|
|
kind,
|
|
values,
|
|
onChange,
|
|
}: {
|
|
kind: Equipment['kind'];
|
|
values: SpecFields;
|
|
onChange: (patch: Partial<SpecFields>) => void;
|
|
}) {
|
|
const { t } = useTranslation('gyms');
|
|
if (LOAD_RANGE_KINDS.includes(kind)) {
|
|
return (
|
|
<div className="grid grid-cols-2 gap-x-4 gap-y-2">
|
|
<Field label={t('minLoad')}>
|
|
<Input
|
|
type="number"
|
|
min={0}
|
|
max={500}
|
|
step="any"
|
|
aria-label={t('minLoad')}
|
|
value={values.minKg}
|
|
onChange={(e) => onChange({ minKg: e.target.value })}
|
|
/>
|
|
</Field>
|
|
<Field label={t('maxLoad')}>
|
|
<Input
|
|
type="number"
|
|
min={0}
|
|
max={500}
|
|
step="any"
|
|
aria-label={t('maxLoad')}
|
|
value={values.maxKg}
|
|
onChange={(e) => onChange({ maxKg: e.target.value })}
|
|
/>
|
|
</Field>
|
|
</div>
|
|
);
|
|
}
|
|
if (MAX_LOAD_KINDS.includes(kind)) {
|
|
return (
|
|
<Field label={t('maxLoadFull')}>
|
|
<Input
|
|
type="number"
|
|
min={0}
|
|
max={1000}
|
|
step="any"
|
|
aria-label={t('maxLoadFull')}
|
|
value={values.maxLoadKg}
|
|
onChange={(e) => onChange({ maxLoadKg: e.target.value })}
|
|
/>
|
|
</Field>
|
|
);
|
|
}
|
|
if (kind === 'CARDIO') {
|
|
return (
|
|
<div className="grid grid-cols-2 gap-x-4 gap-y-2">
|
|
<Field label={t('maxSpeed')}>
|
|
<Input
|
|
type="number"
|
|
min={0}
|
|
max={50}
|
|
step="any"
|
|
aria-label={t('maxSpeed')}
|
|
value={values.maxSpeedKmh}
|
|
onChange={(e) => onChange({ maxSpeedKmh: e.target.value })}
|
|
/>
|
|
</Field>
|
|
<Field label={t('maxIncline')}>
|
|
<Input
|
|
type="number"
|
|
min={0}
|
|
max={40}
|
|
step="any"
|
|
aria-label={t('maxIncline')}
|
|
value={values.maxInclinePct}
|
|
onChange={(e) => onChange({ maxInclinePct: e.target.value })}
|
|
/>
|
|
</Field>
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function EquipmentSection({ gym, onChanged }: { gym: Gym; onChanged: () => void }) {
|
|
const { t } = useTranslation('gyms');
|
|
const { i18n } = useTranslation();
|
|
const { data: eqData, isLoading } = useQuery({
|
|
queryKey: ['equipment'],
|
|
queryFn: () => api<{ equipment: Equipment[] }>('/admin/equipment'),
|
|
});
|
|
const catalog = eqData?.equipment ?? [];
|
|
|
|
const [error, setError] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const [addId, setAddId] = useState('');
|
|
const [addQty, setAddQty] = useState('1');
|
|
const [addFields, setAddFields] = useState<SpecFields>(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);
|
|
setAddFields(EMPTY_SPEC_FIELDS);
|
|
setError('');
|
|
};
|
|
|
|
const add = async () => {
|
|
if (!addItem) return;
|
|
const quantity = parseInt(addQty, 10) || 1;
|
|
setError('');
|
|
let spec: EquipmentSpec | null = null;
|
|
if (hasSpecFields(addItem.kind)) {
|
|
const result = specFromFields(addItem.kind, addFields);
|
|
if ('error' in result) {
|
|
setError(result.error);
|
|
return;
|
|
}
|
|
if (result.spec == null) {
|
|
setError(t('specRequired'));
|
|
return;
|
|
}
|
|
spec = result.spec;
|
|
}
|
|
setBusy(true);
|
|
try {
|
|
await api(`/admin/gyms/${gym.id}/equipment`, {
|
|
method: 'POST',
|
|
body: { equipmentId: addItem.id, quantity, ...(spec ? { spec } : {}) },
|
|
});
|
|
setAddId('');
|
|
setAddQty('1');
|
|
setAddFields(EMPTY_SPEC_FIELDS);
|
|
onChanged();
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : t('common:error'));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const changeQty = async (instanceId: string, quantity: number) => {
|
|
try {
|
|
await api(`/admin/gyms/${gym.id}/equipment/${instanceId}`, {
|
|
method: 'PATCH',
|
|
body: { quantity },
|
|
});
|
|
onChanged();
|
|
} catch (err) {
|
|
alert(err instanceof Error ? err.message : t('common:error'));
|
|
}
|
|
};
|
|
|
|
const remove = async (instanceId: string) => {
|
|
try {
|
|
await api(`/admin/gyms/${gym.id}/equipment/${instanceId}`, { method: 'DELETE' });
|
|
onChanged();
|
|
} catch (err) {
|
|
alert(err instanceof Error ? err.message : t('common:error'));
|
|
}
|
|
};
|
|
|
|
const totalQty = (equipmentId: string) =>
|
|
gym.equipment
|
|
.filter((e) => e.equipmentId === equipmentId)
|
|
.reduce((sum, e) => sum + e.quantity, 0);
|
|
|
|
return (
|
|
<div className="space-y-5">
|
|
{/* POZYCJE NA STANIE: wiersz = nazwa/spec + ilość + usuwanie */}
|
|
<div>
|
|
<div className="mb-2 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">
|
|
{t('positionsInStock', { count: gym.equipment.length })}
|
|
</div>
|
|
{gym.equipment.length === 0 ? (
|
|
<div className="rounded-[10px] border border-dashed border-line px-4 py-6 text-center text-[0.875rem] text-dim">
|
|
{t('noStockDesc')}
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col gap-2">
|
|
{gym.equipment.map((e) => (
|
|
<div
|
|
key={e.id}
|
|
className="flex items-center gap-2.5 rounded-[10px] border border-line bg-card/50 px-3 py-2"
|
|
>
|
|
<span className="min-w-0 flex-1 truncate text-[0.875rem] font-semibold">
|
|
{formatGymEquipment(e, i18n.language)}
|
|
</span>
|
|
<input
|
|
type="number"
|
|
min={1}
|
|
max={10000}
|
|
defaultValue={e.quantity}
|
|
aria-label={t('qtyAria', { name: localizedName(e.equipment, i18n.language) })}
|
|
className="w-16 shrink-0 rounded-[10px] border border-line bg-input px-2 py-1 text-center text-[0.875rem] text-ink focus:border-accent-text focus:outline-none"
|
|
onBlur={(ev) => {
|
|
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();
|
|
}}
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => void remove(e.id)}
|
|
aria-label={t('removeAria', { name: localizedName(e.equipment, i18n.language) })}
|
|
title={t('removeAria', { name: localizedName(e.equipment, i18n.language) })}
|
|
className="shrink-0 cursor-pointer rounded-lg p-1.5 text-dim transition-colors hover:bg-danger/10 hover:text-danger"
|
|
>
|
|
<Trash2 className="h-4 w-4" aria-hidden="true" />
|
|
</button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* DODAWANIE z katalogu: pozycja zawsze z własnymi parametrami (zdjęcia pozycji zarządza właściciel w apce) */}
|
|
<div className="rounded-xl border border-line bg-input/40 p-3.5">
|
|
<div className="mb-2.5 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">
|
|
{t('addFromCatalog')}
|
|
</div>
|
|
<div className="flex flex-wrap items-end gap-2.5">
|
|
<div className="min-w-0 flex-1 basis-52">
|
|
<Select
|
|
value={addId}
|
|
onChange={(e) => selectAdd(e.target.value)}
|
|
aria-label={t('addDeviceAria')}
|
|
>
|
|
<option value="">{t('chooseDevice')}</option>
|
|
{catalog.map((e) => {
|
|
const qty = totalQty(e.id);
|
|
return (
|
|
<option key={e.id} value={e.id}>
|
|
{localizedName(e, i18n.language)}
|
|
{qty > 0 ? ` ${t('inStockOption', { count: qty })}` : ''}
|
|
</option>
|
|
);
|
|
})}
|
|
</Select>
|
|
</div>
|
|
<Field label={t('qty')} className="w-20 shrink-0">
|
|
<Input
|
|
type="number"
|
|
size="sm"
|
|
min={1}
|
|
max={10000}
|
|
value={addQty}
|
|
onChange={(e) => setAddQty(e.target.value)}
|
|
aria-label={t('qty')}
|
|
/>
|
|
</Field>
|
|
<Button onClick={add} disabled={!addId || busy} className="shrink-0">
|
|
{busy ? t('adding') : t('add')}
|
|
</Button>
|
|
</div>
|
|
{addItem && hasSpecFields(addItem.kind) && (
|
|
<div className="mt-3.5 border-t border-line pt-3.5">
|
|
<div className="mb-2 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">
|
|
{t('parameters')}
|
|
</div>
|
|
<AddSpecFields
|
|
kind={addItem.kind}
|
|
values={addFields}
|
|
onChange={(patch) => setAddFields({ ...addFields, ...patch })}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
{error && <ErrorText>{error}</ErrorText>}
|
|
{isLoading && <Loading />}
|
|
</div>
|
|
);
|
|
}
|