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,197 +217,81 @@ export default function WarehouseReservations() {
}; };
if (isPending) { if (isPending) {
return ( return <LoadingState />;
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
} }
return ( const total = pagination?.total ?? items.length;
<div> const subtitle = `${total} ${czechPlural(total, "rezervace", "rezervace", "rezervací")}`;
<motion.div
className="admin-page-header"
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25 }}
>
<div>
<h1 className="admin-page-title">Rezervace</h1>
<p className="admin-page-subtitle">
{pagination?.total ?? items.length}{" "}
{czechPlural(
pagination?.total ?? items.length,
"rezervace",
"rezervace",
"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" />
<line x1="5" y1="12" x2="19" y2="12" />
</svg>
Nová rezervace
</button>
)}
</div>
</motion.div>
<motion.div const columns: DataColumn<WarehouseReservation>[] = [
className="admin-card" {
initial={{ opacity: 0, y: 12 }} key: "item",
animate={{ opacity: 1, y: 0 }} header: "Položka",
transition={{ duration: 0.25, delay: 0.06 }} width: "22%",
style={{ opacity: isFetching ? 0.6 : 1, transition: "opacity 0.2s" }} bold: true,
> render: (res) => res.item?.name || `ID: ${res.item_id}`,
<div className="admin-card-body"> },
<div className="admin-search-bar mb-4"> {
<select key: "project",
value={statusFilter} header: "Projekt",
onChange={(e) => { width: "20%",
setStatusFilter(e.target.value); render: (res) => res.project?.name || "—",
setPage(1); },
}} {
className="admin-form-select" key: "quantity",
> header: "Množství",
<option value="">Všechny stavy</option> width: "10%",
<option value="ACTIVE">Aktivní</option> mono: true,
<option value="FULFILLED">Splněna</option> render: (res) => String(Number(res.quantity)),
<option value="CANCELLED">Zrušena</option> },
</select> {
{projects.length > 0 && ( key: "remaining_qty",
<select header: "Zbývá",
value={projectId} width: "10%",
onChange={(e) => { mono: true,
setProjectId( render: (res) => String(Number(res.remaining_qty)),
e.target.value === "" ? "" : Number(e.target.value), },
); {
setPage(1); key: "status",
}} header: "Stav",
className="admin-form-select" width: "11%",
> render: (res) => (
<option value="">Všechny projekty</option> <StatusChip
{projects.map((p) => ( label={STATUS_LABEL[res.status] ?? res.status}
<option key={p.id} value={p.id}> color={STATUS_COLOR[res.status] ?? "default"}
{p.project_number} {p.name} />
</option> ),
))} },
</select> {
)} key: "reserved_by",
{hasActiveFilters && ( header: "Rezervoval",
<button width: "15%",
type="button" render: (res) =>
className="admin-btn admin-btn-secondary" res.reserved_by_user
onClick={resetFilters}
style={{ whiteSpace: "nowrap" }}
>
Zrušit filtry
</button>
)}
</div>
<AnimatePresence mode="wait">
<motion.div
key={`${statusFilter}-${projectId}-${page}-${items.length}`}
initial={{ opacity: 0, y: 6 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
>
{items.length === 0 && (
<div className="admin-empty-state">
<div className="admin-empty-icon">
<svg
width="28"
height="28"
viewBox="0 0 24 24"
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)}
className="admin-btn admin-btn-primary"
>
Vytvořit první rezervaci
</button>
)}
</div>
)}
{items.length > 0 && (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Položka</th>
<th>Projekt</th>
<th>Množství</th>
<th>Zbývá</th>
<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}` ? `${res.reserved_by_user.first_name} ${res.reserved_by_user.last_name}`
: "—"} : "—",
</td> },
<td className="admin-mono"> {
{formatDate(res.created_at)} key: "created_at",
</td> header: "Vytvořeno",
{canOperate && ( width: "12%",
<td> mono: true,
<div className="admin-table-actions"> render: (res) => formatDate(res.created_at),
{res.status === "ACTIVE" && ( },
<button ...(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={() => onClick={() =>
navigate("/warehouse/issues/new", { navigate("/warehouse/issues/new", {
state: { state: {
@@ -355,101 +303,158 @@ export default function WarehouseReservations() {
}, },
}) })
} }
className="admin-btn-icon"
title="Vytvořit výdej"
aria-label="Vytvořit výdej" aria-label="Vytvořit výdej"
title="Vytvořit výdej"
> >
<svg {IssueIcon}
width="16" </IconButton>
height="16" <IconButton
viewBox="0 0 24 24" size="small"
fill="none" color="error"
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)} onClick={() => setCancelTarget(res)}
className="admin-btn-icon danger"
title="Zrušit rezervaci"
aria-label="Zrušit rezervaci" aria-label="Zrušit rezervaci"
title="Zrušit rezervaci"
> >
<svg {CancelIcon}
width="16" </IconButton>
height="16" </Box>
viewBox="0 0 24 24" ) : null,
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} /> return (
</div> <Box>
<motion.div
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25 }}
>
<PageHeader
title="Rezervace"
subtitle={subtitle}
actions={
canOperate ? (
<Button
startIcon={PlusIcon}
onClick={() => setShowCreateModal(true)}
>
Nová rezervace
</Button>
) : undefined
}
/>
</motion.div> </motion.div>
<FilterBar>
<Box sx={{ flex: "0 0 160px" }}>
<Select
value={statusFilter}
onChange={(val) => {
setStatusFilter(val);
setPage(1);
}}
options={[
{ value: "", label: "Všechny stavy" },
{ value: "ACTIVE", label: "Aktivní" },
{ value: "FULFILLED", label: "Splněna" },
{ 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);
}}
options={[
{ value: "", label: "Všechny projekty" },
...projects.map((p) => ({
value: String(p.id),
label: `${p.project_number} ${p.name}`,
})),
]}
/>
</Box>
)}
{hasActiveFilters && (
<Button variant="outlined" onClick={resetFilters}>
Zrušit filtry
</Button>
)}
</FilterBar>
<Card sx={{ opacity: isFetching ? 0.6 : 1, transition: "opacity .2s" }}>
<DataTable<WarehouseReservation>
columns={columns}
rows={items}
rowKey={(res) => res.id}
empty={
hasActiveFilters ? (
<EmptyState title="Žádné rezervace pro zadaný filtr." />
) : (
<EmptyState
title="Zatím nejsou žádné rezervace."
action={
canOperate ? (
<Button
startIcon={PlusIcon}
onClick={() => setShowCreateModal(true)}
>
Vytvořit první rezervaci
</Button>
) : undefined
}
/>
)
}
/>
<Pagination
page={page}
pageCount={pagination?.total_pages ?? 1}
onChange={setPage}
/>
</Card>
{/* 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 }))}
/> />
</FormField> </Field>
<FormField label="Projekt" required> <Field label="Projekt" required>
<select <Select
value={form.project_id ?? ""} value={form.project_id === null ? "" : String(form.project_id)}
onChange={(e) => onChange={(val) =>
setForm((prev) => ({ setForm((prev) => ({
...prev, ...prev,
project_id: e.target.value ? Number(e.target.value) : null, project_id: val ? Number(val) : null,
})) }))
} }
className="admin-form-select" options={[
> { value: "", label: "Vyberte projekt" },
<option value="">Vyberte projekt</option> ...projects.map((p) => ({
{projects.map((p) => ( value: String(p.id),
<option key={p.id} value={p.id}> label: `${p.project_number} ${p.name}`,
{p.project_number} {p.name} })),
</option> ]}
))} />
</select> </Field>
</FormField> <Field label="Množství" required>
<FormField label="Množství" required> <TextField
<input
type="number" type="number"
value={form.quantity || ""} value={form.quantity || ""}
onChange={(e) => onChange={(e) =>
@@ -458,27 +463,24 @@ export default function WarehouseReservations() {
quantity: Number(e.target.value), quantity: Number(e.target.value),
})) }))
} }
className="admin-form-input" inputProps={{ min: 0, step: 0.001 }}
min="0"
step="0.001"
/> />
</FormField> </Field>
<FormField label="Poznámky"> <Field label="Poznámky">
<textarea <TextField
multiline
minRows={3}
value={form.notes} value={form.notes}
onChange={(e) => onChange={(e) =>
setForm((prev) => ({ ...prev, notes: e.target.value })) setForm((prev) => ({ ...prev, notes: e.target.value }))
} }
className="admin-form-input"
rows={3}
placeholder="Volitelné poznámky" placeholder="Volitelné poznámky"
/> />
</FormField> </Field>
</div> </Modal>
</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>
); );
} }