feat(mui): migrate Warehouse Locations (Lokace) onto MUI kit

Replace legacy admin-* CSS, hand-rolled AnimatePresence modal,
ConfirmModal, FormField, and useModalLock with PageHeader, Card,
DataTable (rowInactive), Modal, ConfirmDialog, Field, TextField,
StatusChip (clickable toggle), EmptyState, LoadingState, and MUI
IconButton. All four mutations + ["warehouse"] invalidation and
is_active toggle preserved verbatim.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-06-06 23:28:34 +02:00
parent 0dbe78bfe6
commit af07703e7f

View File

@@ -1,18 +1,29 @@
import { useState } from "react"; import { useState } from "react";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
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 ConfirmModal from "../components/ConfirmModal";
import useModalLock from "../hooks/useModalLock";
import FormField from "../components/FormField";
import { import {
warehouseLocationListOptions, warehouseLocationListOptions,
type WarehouseLocation, type WarehouseLocation,
} from "../lib/queries/warehouse"; } from "../lib/queries/warehouse";
import { useApiMutation } from "../lib/queries/mutations"; import { useApiMutation } from "../lib/queries/mutations";
import {
Button,
Card,
DataTable,
Modal,
ConfirmDialog,
Field,
TextField,
StatusChip,
PageHeader,
EmptyState,
LoadingState,
type DataColumn,
} from "../ui";
const API_BASE = "/api/admin/warehouse/locations"; const API_BASE = "/api/admin/warehouse/locations";
@@ -22,6 +33,50 @@ interface LocationForm {
description: string; description: string;
} }
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 EditIcon = (
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
</svg>
);
const DeleteIcon = (
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<polyline points="3 6 5 6 21 6" />
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
</svg>
);
export default function WarehouseLocations() { export default function WarehouseLocations() {
const alert = useAlert(); const alert = useAlert();
const { hasPermission } = useAuth(); const { hasPermission } = useAuth();
@@ -96,8 +151,6 @@ export default function WarehouseLocations() {
invalidate: ["warehouse"], invalidate: ["warehouse"],
}); });
useModalLock(showModal);
if (!hasPermission("warehouse.manage")) return <Forbidden />; if (!hasPermission("warehouse.manage")) return <Forbidden />;
const openCreateModal = () => { const openCreateModal = () => {
@@ -175,272 +228,166 @@ export default function WarehouseLocations() {
}; };
if (isPending) { if (isPending) {
return ( return <LoadingState />;
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
} }
return ( const total = locations.length;
<div> const subtitle = `${total} ${
<motion.div total === 1
className="admin-page-header" ? "umístění"
initial={{ opacity: 0, y: 12 }} : total >= 2 && total <= 4
animate={{ opacity: 1, y: 0 }} ? "umístění"
transition={{ duration: 0.25 }} : "umístění"
> }`;
<div>
<h1 className="admin-page-title">Umístění skladu</h1>
</div>
<div className="admin-page-actions">
<button
onClick={openCreateModal}
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>
Přidat umístění
</button>
</div>
</motion.div>
<motion.div const columns: DataColumn<WarehouseLocation>[] = [
className="admin-card" {
initial={{ opacity: 0, y: 12 }} key: "code",
animate={{ opacity: 1, y: 0 }} header: "Kód",
transition={{ duration: 0.25, delay: 0.06 }} width: "15%",
> mono: true,
<div className="admin-card-body"> bold: true,
{locations.length === 0 && ( render: (l) => l.code,
<div className="admin-empty-state"> },
<div className="admin-empty-icon"> {
<svg key: "name",
width="28" header: "Název",
height="28" width: "25%",
viewBox="0 0 24 24" render: (l) => l.name,
fill="none" },
stroke="currentColor" {
strokeWidth="1.5" key: "description",
strokeLinecap="round" header: "Popis",
strokeLinejoin="round" width: "35%",
> render: (l) => l.description || "—",
<path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" /> },
<circle cx="12" cy="10" r="3" /> {
</svg> key: "status",
</div> header: "Stav",
<p>Zatím nejsou žádná umístění.</p> width: "15%",
<button render: (l) => (
onClick={openCreateModal} <StatusChip
className="admin-btn admin-btn-primary" label={l.is_active ? "Aktivní" : "Neaktivní"}
> color={l.is_active ? "success" : "default"}
Přidat první umístění onClick={() => toggleActive(l)}
</button> />
</div> ),
)} },
{locations.length > 0 && ( {
<div className="admin-table-responsive"> key: "actions",
<table className="admin-table"> header: "Akce",
<thead> width: "10%",
<tr> align: "right",
<th>Kód</th> render: (l) => (
<th>Název</th> <Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
<th>Popis</th> <IconButton
<th>Stav</th> size="small"
<th>Akce</th> onClick={() => openEditModal(l)}
</tr>
</thead>
<tbody>
{locations.map((location) => (
<tr
key={location.id}
className={
!location.is_active ? "admin-table-row-inactive" : ""
}
>
<td className="admin-mono fw-500">{location.code}</td>
<td>{location.name}</td>
<td>{location.description || "—"}</td>
<td>
<button
onClick={() => toggleActive(location)}
className={`admin-badge ${location.is_active ? "admin-badge-active" : "admin-badge-inactive"}`}
>
{location.is_active ? "Aktivní" : "Neaktivní"}
</button>
</td>
<td>
<div className="admin-table-actions">
<button
onClick={() => openEditModal(location)}
className="admin-btn-icon"
title="Upravit"
aria-label="Upravit" aria-label="Upravit"
title="Upravit"
> >
<svg {EditIcon}
width="18" </IconButton>
height="18" <IconButton
viewBox="0 0 24 24" size="small"
fill="none" color="error"
stroke="currentColor" onClick={() => setDeleteConfirm({ show: true, location: l })}
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
</svg>
</button>
<button
onClick={() =>
setDeleteConfirm({ show: true, location })
}
className="admin-btn-icon danger"
title="Smazat"
aria-label="Smazat" aria-label="Smazat"
title="Smazat"
> >
<svg {DeleteIcon}
width="18" </IconButton>
height="18" </Box>
viewBox="0 0 24 24" ),
fill="none" },
stroke="currentColor" ];
strokeWidth="2"
strokeLinecap="round" return (
strokeLinejoin="round" <Box>
> <PageHeader
<polyline points="3 6 5 6 21 6" /> title="Umístění skladu"
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" /> subtitle={subtitle}
</svg> actions={
</button> <Button startIcon={PlusIcon} onClick={openCreateModal}>
</div> Přidat umístění
</td> </Button>
</tr> }
))} />
</tbody>
</table> <Card>
</div> <DataTable<WarehouseLocation>
)} columns={columns}
</div> rows={locations}
</motion.div> rowKey={(l) => l.id}
rowInactive={(l) => !l.is_active}
empty={
<EmptyState
title="Zatím nejsou žádná umístění."
action={
<Button startIcon={PlusIcon} onClick={openCreateModal}>
Přidat první umístění
</Button>
}
/>
}
/>
</Card>
{/* Add/Edit Modal */} {/* Add/Edit Modal */}
<AnimatePresence> <Modal
{showModal && ( isOpen={showModal}
<motion.div onClose={() => setShowModal(false)}
className="admin-modal-overlay" onSubmit={handleSubmit}
initial={{ opacity: 0 }} title={editingLocation ? "Upravit umístění" : "Přidat umístění"}
animate={{ opacity: 1 }} loading={
exit={{ opacity: 0 }} createLocationMutation.isPending || updateLocationMutation.isPending
transition={{ duration: 0.2 }} }
> >
<div <Box
className="admin-modal-backdrop" sx={{
onClick={() => setShowModal(false)} display: "grid",
/> gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
<motion.div gap: 2,
className="admin-modal" }}
initial={{ opacity: 0, scale: 0.95, y: 20 }} >
animate={{ opacity: 1, scale: 1, y: 0 }} <Field label="Kód" required error={errors.code}>
exit={{ opacity: 0, scale: 0.95, y: 20 }} <TextField
transition={{ duration: 0.2 }} value={form.code}
> error={!!errors.code}
<div className="admin-modal-header"> onChange={(e) => {
<h2 className="admin-modal-title"> setForm({ ...form, code: e.target.value.toUpperCase() });
{editingLocation ? "Upravit umístění" : "Přidat umístění"} setErrors((prev) => ({ ...prev, code: "" }));
</h2>
</div>
<div className="admin-modal-body">
<div className="admin-form">
<div className="admin-form-row">
<FormField label="Kód" error={errors.code} required>
<input
type="text"
value={form.code}
onChange={(e) => {
setForm({
...form,
code: e.target.value.toUpperCase(),
});
setErrors((prev) => ({
...prev,
code: "",
}));
}} }}
className="admin-form-input"
placeholder="A-01" placeholder="A-01"
aria-invalid={!!errors.code}
/> />
</FormField> </Field>
<FormField label="Název" error={errors.name} required> <Field label="Název" required error={errors.name}>
<input <TextField
type="text"
value={form.name} value={form.name}
error={!!errors.name}
onChange={(e) => { onChange={(e) => {
setForm({ ...form, name: e.target.value }); setForm({ ...form, name: e.target.value });
setErrors((prev) => ({ setErrors((prev) => ({ ...prev, name: "" }));
...prev,
name: "",
}));
}} }}
className="admin-form-input"
placeholder="Regál A, polička 1" placeholder="Regál A, polička 1"
aria-invalid={!!errors.name}
/> />
</FormField> </Field>
</div> </Box>
<FormField label="Popis"> <Field label="Popis">
<textarea <TextField
multiline
minRows={3}
value={form.description} value={form.description}
onChange={(e) => onChange={(e) => setForm({ ...form, description: e.target.value })}
setForm({
...form,
description: e.target.value,
})
}
className="admin-form-input"
rows={3}
placeholder="Volitelný popis umístění" placeholder="Volitelný popis umístění"
/> />
</FormField> </Field>
</div> </Modal>
</div>
<div className="admin-modal-footer">
<button
type="button"
onClick={() => setShowModal(false)}
className="admin-btn admin-btn-secondary"
>
Zrušit
</button>
<button
type="button"
onClick={handleSubmit}
className="admin-btn admin-btn-primary"
>
Uložit
</button>
</div>
</motion.div>
</motion.div>
)}
</AnimatePresence>
{/* Delete Confirmation */} {/* Delete Confirmation */}
<ConfirmModal <ConfirmDialog
isOpen={deleteConfirm.show} isOpen={deleteConfirm.show}
onClose={() => setDeleteConfirm({ show: false, location: null })} onClose={() => setDeleteConfirm({ show: false, location: null })}
onConfirm={handleDelete} onConfirm={handleDelete}
@@ -454,6 +401,6 @@ export default function WarehouseLocations() {
confirmVariant="danger" confirmVariant="danger"
loading={deleteMutation.isPending} loading={deleteMutation.isPending}
/> />
</div> </Box>
); );
} }