feat(mui): migrate Warehouse Categories (Kategorie) onto MUI kit

Replace legacy admin-* CSS, FormModal, ConfirmModal, FormField, and
framer-motion wrappers with PageHeader, Card, DataTable, Modal,
ConfirmDialog, Field, TextField, EmptyState, LoadingState, and MUI
IconButton. All data logic 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:30 +02:00
parent b22cd6b4da
commit 0dbe78bfe6

View File

@@ -1,18 +1,28 @@
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 } from "framer-motion";
import ConfirmModal from "../components/ConfirmModal";
import FormModal from "../components/FormModal";
import FormField from "../components/FormField";
import {
warehouseCategoryListOptions,
type WarehouseCategory,
} from "../lib/queries/warehouse";
import { useApiMutation } from "../lib/queries/mutations";
import {
Button,
Card,
DataTable,
Modal,
ConfirmDialog,
Field,
TextField,
PageHeader,
EmptyState,
LoadingState,
type DataColumn,
} from "../ui";
const API_BASE = "/api/admin/warehouse/categories";
@@ -22,6 +32,50 @@ interface CategoryForm {
sort_order: number;
}
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 WarehouseCategories() {
const alert = useAlert();
const { hasPermission } = useAuth();
@@ -121,207 +175,145 @@ export default function WarehouseCategories() {
};
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">Kategorie 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 kategorii
</button>
</div>
</motion.div>
const total = categories.length;
const subtitle = `${total} ${
total === 1
? "kategorie"
: total >= 2 && total <= 4
? "kategorie"
: "kategorií"
}`;
<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">
{categories.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="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z" />
</svg>
</div>
<p>Zatím nejsou žádné kategorie.</p>
<button
onClick={openCreateModal}
className="admin-btn admin-btn-primary"
>
Přidat první kategorii
</button>
</div>
)}
{categories.length > 0 && (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Název</th>
<th>Popis</th>
<th>Pořadí</th>
<th>Akce</th>
</tr>
</thead>
<tbody>
{categories.map((category) => (
<tr key={category.id}>
<td className="fw-500">{category.name}</td>
<td>{category.description || "—"}</td>
<td className="admin-mono">{category.sort_order}</td>
<td>
<div className="admin-table-actions">
<button
onClick={() => openEditModal(category)}
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, category })
}
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<WarehouseCategory>[] = [
{
key: "name",
header: "Název",
width: "35%",
bold: true,
render: (c) => c.name,
},
{
key: "description",
header: "Popis",
width: "40%",
render: (c) => c.description || "—",
},
{
key: "sort_order",
header: "Pořadí",
width: "15%",
mono: true,
render: (c) => String(c.sort_order),
},
{
key: "actions",
header: "Akce",
width: "10%",
align: "right",
render: (c) => (
<Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
<IconButton
size="small"
onClick={() => openEditModal(c)}
aria-label="Upravit"
title="Upravit"
>
{EditIcon}
</IconButton>
<IconButton
size="small"
color="error"
onClick={() => setDeleteConfirm({ show: true, category: c })}
aria-label="Smazat"
title="Smazat"
>
{DeleteIcon}
</IconButton>
</Box>
),
},
];
return (
<Box>
<PageHeader
title="Kategorie skladu"
subtitle={subtitle}
actions={
<Button startIcon={PlusIcon} onClick={openCreateModal}>
Přidat kategorii
</Button>
}
/>
<Card>
<DataTable<WarehouseCategory>
columns={columns}
rows={categories}
rowKey={(c) => c.id}
empty={
<EmptyState
title="Zatím nejsou žádné kategorie."
action={
<Button startIcon={PlusIcon} onClick={openCreateModal}>
Přidat první kategorii
</Button>
}
/>
}
/>
</Card>
{/* Add/Edit Modal */}
<FormModal
<Modal
isOpen={showModal}
onClose={() => setShowModal(false)}
onSubmit={handleSubmit}
title={editingCategory ? "Upravit kategorii" : "Přidat kategorii"}
loading={saving}
>
<div className="admin-form">
<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="Název kategorie"
aria-invalid={!!errors.name}
/>
</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="Název kategorie"
/>
</Field>
<FormField label="Popis">
<textarea
value={form.description}
onChange={(e) =>
setForm({ ...form, description: e.target.value })
}
className="admin-form-input"
rows={3}
placeholder="Volitelný popis kategorie"
/>
</FormField>
<Field label="Popis">
<TextField
multiline
minRows={3}
value={form.description}
onChange={(e) => setForm({ ...form, description: e.target.value })}
placeholder="Volitelný popis kategorie"
/>
</Field>
<FormField label="Pořadí řazení">
<input
type="number"
inputMode="numeric"
value={form.sort_order}
onChange={(e) =>
setForm({
...form,
sort_order: parseInt(e.target.value) || 0,
})
}
className="admin-form-input"
min="0"
/>
<small className="admin-form-hint">
Nižší hodnota = výše v seznamu
</small>
</FormField>
</div>
</FormModal>
<Field label="Pořadí řazení">
<TextField
type="number"
value={String(form.sort_order)}
onChange={(e) =>
setForm({
...form,
sort_order: parseInt(e.target.value) || 0,
})
}
inputProps={{ min: 0 }}
/>
</Field>
</Modal>
{/* Delete Confirmation */}
<ConfirmModal
<ConfirmDialog
isOpen={deleteConfirm.show}
onClose={() => setDeleteConfirm({ show: false, category: null })}
onConfirm={handleDelete}
@@ -333,7 +325,8 @@ export default function WarehouseCategories() {
}
confirmText="Smazat"
confirmVariant="danger"
loading={deleteMutation.isPending}
/>
</div>
</Box>
);
}