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 { useQuery } from "@tanstack/react-query";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
import { useAlert } from "../context/AlertContext";
import { useAuth } from "../context/AuthContext";
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 {
warehouseLocationListOptions,
type WarehouseLocation,
} from "../lib/queries/warehouse";
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";
@@ -22,6 +33,50 @@ interface LocationForm {
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() {
const alert = useAlert();
const { hasPermission } = useAuth();
@@ -96,8 +151,6 @@ export default function WarehouseLocations() {
invalidate: ["warehouse"],
});
useModalLock(showModal);
if (!hasPermission("warehouse.manage")) return <Forbidden />;
const openCreateModal = () => {
@@ -175,272 +228,166 @@ export default function WarehouseLocations() {
};
if (isPending) {
return (
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
return <LoadingState />;
}
return (
<div>
<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">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>
const total = locations.length;
const subtitle = `${total} ${
total === 1
? "umístění"
: total >= 2 && total <= 4
? "umístění"
: "umístění"
}`;
<motion.div
className="admin-card"
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25, delay: 0.06 }}
>
<div className="admin-card-body">
{locations.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="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>
</div>
<p>Zatím nejsou žádná umístění.</p>
<button
onClick={openCreateModal}
className="admin-btn admin-btn-primary"
>
Přidat první umístění
</button>
</div>
)}
{locations.length > 0 && (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Kód</th>
<th>Název</th>
<th>Popis</th>
<th>Stav</th>
<th>Akce</th>
</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"
>
<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>
</button>
<button
onClick={() =>
setDeleteConfirm({ show: true, location })
}
className="admin-btn-icon danger"
title="Smazat"
aria-label="Smazat"
>
<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>
</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</motion.div>
const columns: DataColumn<WarehouseLocation>[] = [
{
key: "code",
header: "Kód",
width: "15%",
mono: true,
bold: true,
render: (l) => l.code,
},
{
key: "name",
header: "Název",
width: "25%",
render: (l) => l.name,
},
{
key: "description",
header: "Popis",
width: "35%",
render: (l) => l.description || "—",
},
{
key: "status",
header: "Stav",
width: "15%",
render: (l) => (
<StatusChip
label={l.is_active ? "Aktivní" : "Neaktivní"}
color={l.is_active ? "success" : "default"}
onClick={() => toggleActive(l)}
/>
),
},
{
key: "actions",
header: "Akce",
width: "10%",
align: "right",
render: (l) => (
<Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
<IconButton
size="small"
onClick={() => openEditModal(l)}
aria-label="Upravit"
title="Upravit"
>
{EditIcon}
</IconButton>
<IconButton
size="small"
color="error"
onClick={() => setDeleteConfirm({ show: true, location: l })}
aria-label="Smazat"
title="Smazat"
>
{DeleteIcon}
</IconButton>
</Box>
),
},
];
return (
<Box>
<PageHeader
title="Umístění skladu"
subtitle={subtitle}
actions={
<Button startIcon={PlusIcon} onClick={openCreateModal}>
Přidat umístění
</Button>
}
/>
<Card>
<DataTable<WarehouseLocation>
columns={columns}
rows={locations}
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 */}
<AnimatePresence>
{showModal && (
<motion.div
className="admin-modal-overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
>
<div
className="admin-modal-backdrop"
onClick={() => setShowModal(false)}
<Modal
isOpen={showModal}
onClose={() => setShowModal(false)}
onSubmit={handleSubmit}
title={editingLocation ? "Upravit umístění" : "Přidat umístění"}
loading={
createLocationMutation.isPending || updateLocationMutation.isPending
}
>
<Box
sx={{
display: "grid",
gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
gap: 2,
}}
>
<Field label="Kód" required error={errors.code}>
<TextField
value={form.code}
error={!!errors.code}
onChange={(e) => {
setForm({ ...form, code: e.target.value.toUpperCase() });
setErrors((prev) => ({ ...prev, code: "" }));
}}
placeholder="A-01"
/>
<motion.div
className="admin-modal"
initial={{ opacity: 0, scale: 0.95, y: 20 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: 20 }}
transition={{ duration: 0.2 }}
>
<div className="admin-modal-header">
<h2 className="admin-modal-title">
{editingLocation ? "Upravit umístění" : "Přidat umístění"}
</h2>
</div>
</Field>
<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"
aria-invalid={!!errors.code}
/>
</FormField>
<Field label="Název" required error={errors.name}>
<TextField
value={form.name}
error={!!errors.name}
onChange={(e) => {
setForm({ ...form, name: e.target.value });
setErrors((prev) => ({ ...prev, name: "" }));
}}
placeholder="Regál A, polička 1"
/>
</Field>
</Box>
<FormField label="Název" error={errors.name} required>
<input
type="text"
value={form.name}
onChange={(e) => {
setForm({ ...form, name: e.target.value });
setErrors((prev) => ({
...prev,
name: "",
}));
}}
className="admin-form-input"
placeholder="Regál A, polička 1"
aria-invalid={!!errors.name}
/>
</FormField>
</div>
<FormField label="Popis">
<textarea
value={form.description}
onChange={(e) =>
setForm({
...form,
description: e.target.value,
})
}
className="admin-form-input"
rows={3}
placeholder="Volitelný popis umístění"
/>
</FormField>
</div>
</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>
<Field label="Popis">
<TextField
multiline
minRows={3}
value={form.description}
onChange={(e) => setForm({ ...form, description: e.target.value })}
placeholder="Volitelný popis umístění"
/>
</Field>
</Modal>
{/* Delete Confirmation */}
<ConfirmModal
<ConfirmDialog
isOpen={deleteConfirm.show}
onClose={() => setDeleteConfirm({ show: false, location: null })}
onConfirm={handleDelete}
@@ -454,6 +401,6 @@ export default function WarehouseLocations() {
confirmVariant="danger"
loading={deleteMutation.isPending}
/>
</div>
</Box>
);
}