feat(mui): migrate Warehouse Reservations (Rezervace) onto MUI kit

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-06-07 00:28:35 +02:00
parent f51d5fcba4
commit 3d1bd49ad6

View File

@@ -4,14 +4,11 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useAlert } from "../context/AlertContext"; import { useAlert } from "../context/AlertContext";
import { useAuth } from "../context/AuthContext"; import { useAuth } from "../context/AuthContext";
import Forbidden from "../components/Forbidden"; import Forbidden from "../components/Forbidden";
import { motion, AnimatePresence } from "framer-motion"; import { motion } from "framer-motion";
import Pagination from "../components/Pagination"; import Box from "@mui/material/Box";
import ConfirmModal from "../components/ConfirmModal"; import IconButton from "@mui/material/IconButton";
import FormModal from "../components/FormModal";
import FormField from "../components/FormField";
import ItemPicker from "../components/warehouse/ItemPicker";
import useDebounce from "../hooks/useDebounce";
import { usePaginatedQuery } from "../hooks/usePaginatedQuery"; import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
import ItemPicker from "../components/warehouse/ItemPicker";
import apiFetch from "../utils/api"; import apiFetch from "../utils/api";
import { formatDate, czechPlural } from "../utils/formatters"; import { formatDate, czechPlural } from "../utils/formatters";
@@ -20,13 +17,33 @@ import {
type WarehouseReservation, type WarehouseReservation,
} from "../lib/queries/warehouse"; } from "../lib/queries/warehouse";
import { projectListOptions } from "../lib/queries/projects"; import { projectListOptions } from "../lib/queries/projects";
import {
Button,
Card,
DataTable,
Pagination,
Modal,
ConfirmDialog,
Field,
TextField,
Select,
StatusChip,
PageHeader,
FilterBar,
EmptyState,
LoadingState,
type DataColumn,
} from "../ui";
const PER_PAGE = 20; const PER_PAGE = 20;
const STATUS_BADGE: Record<string, string> = { const STATUS_COLOR: Record<
ACTIVE: "admin-badge-active", string,
FULFILLED: "admin-badge-info", "success" | "error" | "warning" | "info" | "default"
CANCELLED: "admin-badge-danger", > = {
ACTIVE: "success",
FULFILLED: "info",
CANCELLED: "error",
}; };
const STATUS_LABEL: Record<string, string> = { const STATUS_LABEL: Record<string, string> = {
@@ -35,6 +52,53 @@ const STATUS_LABEL: Record<string, string> = {
CANCELLED: "Zrušena", CANCELLED: "Zrušena",
}; };
const PlusIcon = (
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<line x1="12" y1="5" x2="12" y2="19" />
<line x1="5" y1="12" x2="19" y2="12" />
</svg>
);
const IssueIcon = (
<svg
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<polyline points="7 13 12 18 17 13" />
<line x1="12" y1="18" x2="12" y2="6" />
</svg>
);
const CancelIcon = (
<svg
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<circle cx="12" cy="12" r="10" />
<line x1="15" y1="9" x2="9" y2="15" />
<line x1="9" y1="9" x2="15" y2="15" />
</svg>
);
interface ReservationForm { interface ReservationForm {
item_id: number | null; item_id: number | null;
project_id: number | null; project_id: number | null;
@@ -153,332 +217,270 @@ export default function WarehouseReservations() {
}; };
if (isPending) { if (isPending) {
return ( return <LoadingState />;
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
} }
const total = pagination?.total ?? items.length;
const subtitle = `${total} ${czechPlural(total, "rezervace", "rezervace", "rezervací")}`;
const columns: DataColumn<WarehouseReservation>[] = [
{
key: "item",
header: "Položka",
width: "22%",
bold: true,
render: (res) => res.item?.name || `ID: ${res.item_id}`,
},
{
key: "project",
header: "Projekt",
width: "20%",
render: (res) => res.project?.name || "—",
},
{
key: "quantity",
header: "Množství",
width: "10%",
mono: true,
render: (res) => String(Number(res.quantity)),
},
{
key: "remaining_qty",
header: "Zbývá",
width: "10%",
mono: true,
render: (res) => String(Number(res.remaining_qty)),
},
{
key: "status",
header: "Stav",
width: "11%",
render: (res) => (
<StatusChip
label={STATUS_LABEL[res.status] ?? res.status}
color={STATUS_COLOR[res.status] ?? "default"}
/>
),
},
{
key: "reserved_by",
header: "Rezervoval",
width: "15%",
render: (res) =>
res.reserved_by_user
? `${res.reserved_by_user.first_name} ${res.reserved_by_user.last_name}`
: "—",
},
{
key: "created_at",
header: "Vytvořeno",
width: "12%",
mono: true,
render: (res) => formatDate(res.created_at),
},
...(canOperate
? [
{
key: "actions",
header: "Akce",
width: "10%",
align: "right" as const,
render: (res: WarehouseReservation) =>
res.status === "ACTIVE" ? (
<Box
sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}
>
<IconButton
size="small"
onClick={() =>
navigate("/warehouse/issues/new", {
state: {
reservationId: res.id,
itemId: res.item_id,
projectId: res.project_id,
quantity: Number(res.remaining_qty),
itemName: res.item?.name,
},
})
}
aria-label="Vytvořit výdej"
title="Vytvořit výdej"
>
{IssueIcon}
</IconButton>
<IconButton
size="small"
color="error"
onClick={() => setCancelTarget(res)}
aria-label="Zrušit rezervaci"
title="Zrušit rezervaci"
>
{CancelIcon}
</IconButton>
</Box>
) : null,
},
]
: []),
];
return ( return (
<div> <Box>
<motion.div <motion.div
className="admin-page-header"
initial={{ opacity: 0, y: 12 }} initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }} animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25 }} transition={{ duration: 0.25 }}
> >
<div> <PageHeader
<h1 className="admin-page-title">Rezervace</h1> title="Rezervace"
<p className="admin-page-subtitle"> subtitle={subtitle}
{pagination?.total ?? items.length}{" "} actions={
{czechPlural( canOperate ? (
pagination?.total ?? items.length, <Button
"rezervace", startIcon={PlusIcon}
"rezervace", onClick={() => setShowCreateModal(true)}
"rezervací",
)}
</p>
</div>
<div className="admin-page-actions">
{canOperate && (
<button
onClick={() => setShowCreateModal(true)}
className="admin-btn admin-btn-primary"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
> >
<line x1="12" y1="5" x2="12" y2="19" /> Nová rezervace
<line x1="5" y1="12" x2="19" y2="12" /> </Button>
</svg> ) : undefined
Nová rezervace }
</button> />
)}
</div>
</motion.div> </motion.div>
<motion.div <FilterBar>
className="admin-card" <Box sx={{ flex: "0 0 160px" }}>
initial={{ opacity: 0, y: 12 }} <Select
animate={{ opacity: 1, y: 0 }} value={statusFilter}
transition={{ duration: 0.25, delay: 0.06 }} onChange={(val) => {
style={{ opacity: isFetching ? 0.6 : 1, transition: "opacity 0.2s" }} setStatusFilter(val);
> setPage(1);
<div className="admin-card-body"> }}
<div className="admin-search-bar mb-4"> options={[
<select { value: "", label: "Všechny stavy" },
value={statusFilter} { value: "ACTIVE", label: "Aktivní" },
onChange={(e) => { { value: "FULFILLED", label: "Splněna" },
setStatusFilter(e.target.value); { value: "CANCELLED", label: "Zrušena" },
]}
/>
</Box>
{projects.length > 0 && (
<Box sx={{ flex: "0 0 220px" }}>
<Select
value={projectId === "" ? "" : String(projectId)}
onChange={(val) => {
setProjectId(val === "" ? "" : Number(val));
setPage(1); setPage(1);
}} }}
className="admin-form-select" options={[
> { value: "", label: "Všechny projekty" },
<option value="">Všechny stavy</option> ...projects.map((p) => ({
<option value="ACTIVE">Aktivní</option> value: String(p.id),
<option value="FULFILLED">Splněna</option> label: `${p.project_number} ${p.name}`,
<option value="CANCELLED">Zrušena</option> })),
</select> ]}
{projects.length > 0 && ( />
<select </Box>
value={projectId} )}
onChange={(e) => { {hasActiveFilters && (
setProjectId( <Button variant="outlined" onClick={resetFilters}>
e.target.value === "" ? "" : Number(e.target.value), Zrušit filtry
); </Button>
setPage(1); )}
}} </FilterBar>
className="admin-form-select"
>
<option value="">Všechny projekty</option>
{projects.map((p) => (
<option key={p.id} value={p.id}>
{p.project_number} {p.name}
</option>
))}
</select>
)}
{hasActiveFilters && (
<button
type="button"
className="admin-btn admin-btn-secondary"
onClick={resetFilters}
style={{ whiteSpace: "nowrap" }}
>
Zrušit filtry
</button>
)}
</div>
<AnimatePresence mode="wait"> <Card sx={{ opacity: isFetching ? 0.6 : 1, transition: "opacity .2s" }}>
<motion.div <DataTable<WarehouseReservation>
key={`${statusFilter}-${projectId}-${page}-${items.length}`} columns={columns}
initial={{ opacity: 0, y: 6 }} rows={items}
animate={{ opacity: 1, y: 0 }} rowKey={(res) => res.id}
exit={{ opacity: 0 }} empty={
transition={{ duration: 0.15 }} hasActiveFilters ? (
> <EmptyState title="Žádné rezervace pro zadaný filtr." />
{items.length === 0 && ( ) : (
<div className="admin-empty-state"> <EmptyState
<div className="admin-empty-icon"> title="Zatím nejsou žádné rezervace."
<svg action={
width="28" canOperate ? (
height="28" <Button
viewBox="0 0 24 24" startIcon={PlusIcon}
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
<polyline points="14 2 14 8 20 8" />
<line x1="16" y1="13" x2="8" y2="13" />
<line x1="16" y1="17" x2="8" y2="17" />
</svg>
</div>
<p>
{hasActiveFilters
? "Žádné rezervace pro zadaný filtr."
: "Zatím nejsou žádné rezervace."}
</p>
{canOperate && !hasActiveFilters && (
<button
onClick={() => setShowCreateModal(true)} onClick={() => setShowCreateModal(true)}
className="admin-btn admin-btn-primary"
> >
Vytvořit první rezervaci Vytvořit první rezervaci
</button> </Button>
)} ) : undefined
</div> }
)} />
{items.length > 0 && ( )
<div className="admin-table-responsive"> }
<table className="admin-table"> />
<thead> <Pagination
<tr> page={page}
<th>Položka</th> pageCount={pagination?.total_pages ?? 1}
<th>Projekt</th> onChange={setPage}
<th>Množství</th> />
<th>Zbývá</th> </Card>
<th>Stav</th>
<th>Rezervoval</th>
<th>Vytvořeno</th>
{canOperate && <th>Akce</th>}
</tr>
</thead>
<tbody>
{items.map((res) => (
<tr key={res.id}>
<td className="fw-500">
{res.item?.name || `ID: ${res.item_id}`}
</td>
<td>{res.project?.name || "—"}</td>
<td className="admin-mono">{Number(res.quantity)}</td>
<td className="admin-mono">
{Number(res.remaining_qty)}
</td>
<td>
<span
className={`admin-badge ${STATUS_BADGE[res.status] ?? ""}`}
>
{STATUS_LABEL[res.status] ?? res.status}
</span>
</td>
<td>
{res.reserved_by_user
? `${res.reserved_by_user.first_name} ${res.reserved_by_user.last_name}`
: "—"}
</td>
<td className="admin-mono">
{formatDate(res.created_at)}
</td>
{canOperate && (
<td>
<div className="admin-table-actions">
{res.status === "ACTIVE" && (
<button
onClick={() =>
navigate("/warehouse/issues/new", {
state: {
reservationId: res.id,
itemId: res.item_id,
projectId: res.project_id,
quantity: Number(res.remaining_qty),
itemName: res.item?.name,
},
})
}
className="admin-btn-icon"
title="Vytvořit výdej"
aria-label="Vytvořit výdej"
>
<svg
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<polyline points="7 13 12 18 17 13" />
<line x1="12" y1="18" x2="12" y2="6" />
</svg>
</button>
)}
{res.status === "ACTIVE" && (
<button
onClick={() => setCancelTarget(res)}
className="admin-btn-icon danger"
title="Zrušit rezervaci"
aria-label="Zrušit rezervaci"
>
<svg
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<circle cx="12" cy="12" r="10" />
<line x1="15" y1="9" x2="9" y2="15" />
<line x1="9" y1="9" x2="15" y2="15" />
</svg>
</button>
)}
</div>
</td>
)}
</tr>
))}
</tbody>
</table>
</div>
)}
</motion.div>
</AnimatePresence>
<Pagination pagination={pagination} onPageChange={setPage} />
</div>
</motion.div>
{/* Create reservation modal */} {/* Create reservation modal */}
<FormModal <Modal
isOpen={showCreateModal} isOpen={showCreateModal}
onClose={() => setShowCreateModal(false)} onClose={() => setShowCreateModal(false)}
onSubmit={handleCreate} onSubmit={handleCreate}
title="Nová rezervace" title="Nová rezervace"
submitLabel="Vytvořit rezervaci" submitText="Vytvořit rezervaci"
loading={saving} loading={saving}
> >
<div className="admin-form"> <Field label="Položka" required>
<FormField label="Položka" required> <ItemPicker
<ItemPicker value={form.item_id}
value={form.item_id} onChange={(id) => setForm((prev) => ({ ...prev, item_id: id }))}
onChange={(id) => setForm((prev) => ({ ...prev, item_id: id }))} />
/> </Field>
</FormField> <Field label="Projekt" required>
<FormField label="Projekt" required> <Select
<select value={form.project_id === null ? "" : String(form.project_id)}
value={form.project_id ?? ""} onChange={(val) =>
onChange={(e) => setForm((prev) => ({
setForm((prev) => ({ ...prev,
...prev, project_id: val ? Number(val) : null,
project_id: e.target.value ? Number(e.target.value) : null, }))
})) }
} options={[
className="admin-form-select" { value: "", label: "Vyberte projekt" },
> ...projects.map((p) => ({
<option value="">Vyberte projekt</option> value: String(p.id),
{projects.map((p) => ( label: `${p.project_number} ${p.name}`,
<option key={p.id} value={p.id}> })),
{p.project_number} {p.name} ]}
</option> />
))} </Field>
</select> <Field label="Množství" required>
</FormField> <TextField
<FormField label="Množství" required> type="number"
<input value={form.quantity || ""}
type="number" onChange={(e) =>
value={form.quantity || ""} setForm((prev) => ({
onChange={(e) => ...prev,
setForm((prev) => ({ quantity: Number(e.target.value),
...prev, }))
quantity: Number(e.target.value), }
})) inputProps={{ min: 0, step: 0.001 }}
} />
className="admin-form-input" </Field>
min="0" <Field label="Poznámky">
step="0.001" <TextField
/> multiline
</FormField> minRows={3}
<FormField label="Poznámky"> value={form.notes}
<textarea onChange={(e) =>
value={form.notes} setForm((prev) => ({ ...prev, notes: e.target.value }))
onChange={(e) => }
setForm((prev) => ({ ...prev, notes: e.target.value })) placeholder="Volitelné poznámky"
} />
className="admin-form-input" </Field>
rows={3} </Modal>
placeholder="Volitelné poznámky"
/>
</FormField>
</div>
</FormModal>
{/* Cancel confirmation modal */} {/* Cancel confirmation modal */}
<ConfirmModal <ConfirmDialog
isOpen={!!cancelTarget} isOpen={!!cancelTarget}
onClose={() => setCancelTarget(null)} onClose={() => setCancelTarget(null)}
onConfirm={handleCancel} onConfirm={handleCancel}
@@ -488,6 +490,6 @@ export default function WarehouseReservations() {
confirmVariant="danger" confirmVariant="danger"
loading={cancelling} loading={cancelling}
/> />
</div> </Box>
); );
} }