feat(mui): migrate Offers Customers (Zákazníci) 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:11:20 +02:00
parent c8c40d80d6
commit 4b94efbb43

View File

@@ -1,11 +1,11 @@
import { useState, useCallback, useRef } from "react";
import { useQuery } from "@tanstack/react-query";
import { motion } from "framer-motion";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import IconButton from "@mui/material/IconButton";
import { useAlert } from "../context/AlertContext";
import { useAuth } from "../context/AuthContext";
import { motion } from "framer-motion";
import ConfirmModal from "../components/ConfirmModal";
import FormModal from "../components/FormModal";
import FormField from "../components/FormField";
import Forbidden from "../components/Forbidden";
import {
offerCustomersOptions,
@@ -13,6 +13,22 @@ import {
type CustomField,
} from "../lib/queries/offers";
import { useApiMutation } from "../lib/queries/mutations";
import {
Button,
Card,
DataTable,
Modal,
ConfirmDialog,
Field,
TextField,
CheckboxField,
StatusChip,
PageHeader,
FilterBar,
EmptyState,
LoadingState,
type DataColumn,
} from "../ui";
const API_BASE = "/api/admin";
@@ -42,6 +58,100 @@ interface CustomerForm {
vat_id: 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 SmallPlusIcon = (
<svg
width="14"
height="14"
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>
);
const RemoveIcon = (
<svg
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<line x1="18" y1="6" x2="6" y2="18" />
<line x1="6" y1="6" x2="18" y2="18" />
</svg>
);
const UpIcon = (
<svg
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M18 15l-6-6-6 6" />
</svg>
);
const DownIcon = (
<svg
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M6 9l6 6 6-6" />
</svg>
);
export default function OffersCustomers() {
const alert = useAlert();
const { hasPermission } = useAuth();
@@ -261,230 +371,201 @@ export default function OffersCustomers() {
const fullFieldOrder = getFullFieldOrder();
if (isPending) {
return (
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
return <LoadingState />;
}
const columns: DataColumn<Customer>[] = [
{
key: "name",
header: "Název",
width: "26%",
render: (c) => (
<Box>
<Box sx={{ fontWeight: 500, color: "text.primary" }}>{c.name}</Box>
{c.street && (
<Box sx={{ fontSize: "11px", color: "text.disabled" }}>
{c.street}
</Box>
)}
</Box>
),
},
{
key: "city",
header: "Město",
width: "18%",
render: (c) => c.city || "—",
},
{
key: "company_id",
header: "IČO",
width: "13%",
mono: true,
render: (c) => c.company_id || "—",
},
{
key: "vat_id",
header: "DIČ",
width: "13%",
mono: true,
render: (c) => c.vat_id || "—",
},
{
key: "quotation_count",
header: "Nabídky",
width: "10%",
render: (c) => (
<StatusChip label={String(c.quotation_count || 0)} color="info" />
),
},
{
key: "actions",
header: "Akce",
width: "10%",
align: "right",
render: (c) => {
const cannotDelete = c.quotation_count > 0;
return (
<div>
<Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
{hasPermission("customers.edit") && (
<IconButton
size="small"
onClick={() => openEditModal(c)}
aria-label="Upravit"
title="Upravit"
>
{EditIcon}
</IconButton>
)}
{hasPermission("customers.delete") && (
// Disabled buttons don't fire title/tooltip, so wrap in a titled
// span when delete is blocked (customer still has quotations).
<Box
component="span"
title={
cannotDelete ? "Nelze smazat zákazníka s nabídkami" : "Smazat"
}
>
<IconButton
size="small"
color="error"
onClick={() => setDeleteConfirm({ show: true, customer: c })}
aria-label={
cannotDelete
? "Nelze smazat zákazníka s nabídkami"
: "Smazat"
}
disabled={cannotDelete}
>
{DeleteIcon}
</IconButton>
</Box>
)}
</Box>
);
},
},
];
return (
<Box>
<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">Zákazníci</h1>
<p className="admin-page-subtitle">Správa zákazníků pro nabídky</p>
</div>
{hasPermission("customers.create") && (
<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>
<PageHeader
title="Zákazníci"
subtitle="Správa zákazníků pro nabídky"
actions={
hasPermission("customers.create") ? (
<Button startIcon={PlusIcon} onClick={openCreateModal}>
Přidat zákazníka
</button>
)}
</Button>
) : undefined
}
/>
</motion.div>
<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">
<div className="admin-search-bar mb-4">
<input
type="text"
<FilterBar>
<Box sx={{ flex: "1 1 320px" }}>
<TextField
value={search}
onChange={(e) => setSearch(e.target.value)}
className="admin-form-input"
placeholder="Hledat zákazníky..."
fullWidth
/>
</div>
</Box>
</FilterBar>
{filteredCustomers.length === 0 ? (
<div className="admin-empty-state">
<p>
{search
? "Žádní zákazníci odpovídající hledání."
: "Zatím nejsou žádní zákazníci."}
</p>
{!search && hasPermission("customers.create") && (
<button
onClick={openCreateModal}
className="admin-btn admin-btn-primary"
>
Přidat prvního zákazníka
</button>
)}
</div>
) : (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Název</th>
<th>Město</th>
<th>IČO</th>
<th>DIČ</th>
<th>Nabídky</th>
<th>Akce</th>
</tr>
</thead>
<tbody>
{filteredCustomers.map((customer) => (
<tr key={customer.id}>
<td>
<div
style={{
fontWeight: 500,
color: "var(--text-primary)",
}}
>
{customer.name}
</div>
{customer.street && (
<div
className="text-tertiary"
style={{ fontSize: "11px" }}
>
{customer.street}
</div>
)}
</td>
<td>{customer.city || "—"}</td>
<td>{customer.company_id || "—"}</td>
<td>{customer.vat_id || "—"}</td>
<td>
<span className="admin-badge admin-badge-info">
{customer.quotation_count || 0}
</span>
</td>
<td>
<div className="admin-table-actions">
{hasPermission("customers.edit") && (
<button
onClick={() => openEditModal(customer)}
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"
>
<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>
)}
{hasPermission("customers.delete") && (
<button
onClick={() =>
setDeleteConfirm({ show: true, customer })
}
className="admin-btn-icon danger"
<Card>
<DataTable<Customer>
columns={columns}
rows={filteredCustomers}
rowKey={(c) => c.id}
empty={
<EmptyState
title={
customer.quotation_count > 0
? "Nelze smazat zákazníka s nabídkami"
: "Smazat"
search
? "Žádní zákazníci odpovídající hledání."
: "Zatím nejsou žádní zákazníci."
}
aria-label={
customer.quotation_count > 0
? "Nelze smazat zákazníka s nabídkami"
: "Smazat"
action={
!search && hasPermission("customers.create") ? (
<Button startIcon={PlusIcon} onClick={openCreateModal}>
Přidat prvního zákazníka
</Button>
) : undefined
}
disabled={customer.quotation_count > 0}
>
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<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>
/>
}
/>
</Card>
{/* Create/Edit Modal */}
<FormModal
<Modal
isOpen={showModal}
onClose={closeModal}
onSubmit={handleSubmit}
title={editingCustomer ? "Upravit zákazníka" : "Nový zákazník"}
submitLabel={editingCustomer ? "Uložit změny" : "Vytvořit zákazníka"}
submitText={editingCustomer ? "Uložit změny" : "Vytvořit zákazníka"}
loading={saving}
size="lg"
maxWidth="md"
>
<div className="admin-form">
<FormField label="Název" required>
<input
type="text"
<Field label="Název" required>
<TextField
value={form.name}
onChange={(e) =>
setForm((prev) => ({ ...prev, name: e.target.value }))
}
className="admin-form-input"
placeholder="Název firmy / jméno"
/>
</FormField>
<FormField label="Ulice">
<input
type="text"
</Field>
<Field label="Ulice">
<TextField
value={form.street}
onChange={(e) =>
setForm((prev) => ({ ...prev, street: e.target.value }))
}
className="admin-form-input"
/>
</FormField>
<div className="admin-form-row">
<FormField label="Město">
<input
type="text"
</Field>
<Box
sx={{
display: "grid",
gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
gap: 2,
}}
>
<Field label="Město">
<TextField
value={form.city}
onChange={(e) =>
setForm((prev) => ({ ...prev, city: e.target.value }))
}
className="admin-form-input"
/>
</FormField>
<FormField label="PSČ">
<input
type="text"
</Field>
<Field label="PSČ">
<TextField
value={form.postal_code}
onChange={(e) =>
setForm((prev) => ({
@@ -492,24 +573,26 @@ export default function OffersCustomers() {
postal_code: e.target.value,
}))
}
className="admin-form-input"
/>
</FormField>
</div>
<FormField label="Země">
<input
type="text"
</Field>
</Box>
<Field label="Země">
<TextField
value={form.country}
onChange={(e) =>
setForm((prev) => ({ ...prev, country: e.target.value }))
}
className="admin-form-input"
/>
</FormField>
<div className="admin-form-row">
<FormField label="IČO">
<input
type="text"
</Field>
<Box
sx={{
display: "grid",
gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
gap: 2,
}}
>
<Field label="IČO">
<TextField
value={form.company_id}
onChange={(e) =>
setForm((prev) => ({
@@ -517,41 +600,43 @@ export default function OffersCustomers() {
company_id: e.target.value,
}))
}
className="admin-form-input"
/>
</FormField>
<FormField label="DIČ">
<input
type="text"
</Field>
<Field label="DIČ">
<TextField
value={form.vat_id}
onChange={(e) =>
setForm((prev) => ({ ...prev, vat_id: e.target.value }))
}
className="admin-form-input"
/>
</FormField>
</div>
</Field>
</Box>
{/* Dynamic custom fields */}
<div style={{ marginTop: 4 }}>
<label
className="admin-form-label"
style={{ display: "block", marginBottom: 4 }}
<Box sx={{ mt: 0.5 }}>
<Typography
variant="body2"
sx={{
display: "block",
mb: 0.5,
fontWeight: 600,
color: "text.secondary",
}}
>
Vlastní pole
</label>
</Typography>
{customFields.map((field, idx) => (
<div key={field._key} style={{ marginBottom: 8 }}>
<div
className="admin-form-row"
style={{ marginBottom: 0, alignItems: "flex-end" }}
<Box key={field._key} sx={{ mb: 1 }}>
<Box
sx={{
display: "grid",
gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
gap: 2,
alignItems: "flex-start",
}}
>
<FormField
label={idx === 0 ? "Název" : " "}
style={{ flex: 1 }}
>
<input
type="text"
<TextField
label={idx === 0 ? "Název" : undefined}
value={field.name}
onChange={(e) => {
const updated = [...customFields];
@@ -561,23 +646,17 @@ export default function OffersCustomers() {
};
setCustomFields(updated);
}}
className="admin-form-input"
placeholder="Např. Kontakt"
/>
</FormField>
<FormField
label={idx === 0 ? "Hodnota" : " "}
style={{ flex: 1 }}
>
<div
style={{
<Box
sx={{
display: "flex",
gap: 4,
alignItems: "center",
gap: 0.5,
alignItems: idx === 0 ? "flex-end" : "center",
}}
>
<input
type="text"
<TextField
label={idx === 0 ? "Hodnota" : undefined}
value={field.value}
onChange={(e) => {
const updated = [...customFields];
@@ -587,11 +666,11 @@ export default function OffersCustomers() {
};
setCustomFields(updated);
}}
className="admin-form-input"
style={{ flex: 1 }}
sx={{ flex: 1 }}
/>
<button
type="button"
<IconButton
size="small"
color="error"
onClick={() => {
const key = `custom_${idx}`;
setFieldOrder((prev) => {
@@ -605,50 +684,40 @@ export default function OffersCustomers() {
return k;
});
});
setCustomFields(
customFields.filter((_, i) => i !== idx),
);
setCustomFields(customFields.filter((_, i) => i !== idx));
}}
className="admin-btn-icon danger"
title="Odebrat pole"
aria-label="Odebrat pole"
>
<svg
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<line x1="18" y1="6" x2="6" y2="18" />
<line x1="6" y1="6" x2="18" y2="18" />
</svg>
</button>
</div>
</FormField>
</div>
<label className="admin-form-checkbox" style={{ marginTop: 4 }}>
<input
type="checkbox"
{RemoveIcon}
</IconButton>
</Box>
</Box>
<Box sx={{ mt: 0.5 }}>
<CheckboxField
label={
<Typography variant="body2" sx={{ fontSize: "0.8rem" }}>
Zobrazit název v PDF
</Typography>
}
checked={field.showLabel !== false}
onChange={(e) => {
onChange={(checked) => {
const updated = [...customFields];
updated[idx] = {
...updated[idx],
showLabel: e.target.checked,
showLabel: checked,
};
setCustomFields(updated);
}}
/>
<span style={{ fontSize: "0.8rem" }}>
Zobrazit název v PDF
</span>
</label>
</div>
</Box>
</Box>
))}
<button
type="button"
<Button
variant="outlined"
color="inherit"
size="small"
startIcon={SmallPlusIcon}
onClick={() =>
setCustomFields([
...customFields,
@@ -660,90 +729,83 @@ export default function OffersCustomers() {
},
])
}
className="admin-btn admin-btn-secondary"
style={{ marginTop: 4, fontSize: "0.85rem" }}
sx={{ mt: 0.5 }}
>
<svg
width="14"
height="14"
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 pole
</button>
</div>
</Button>
</Box>
{/* Field order for PDF */}
<div style={{ marginTop: 16 }}>
<label className="admin-form-label">Pořadí polí v PDF</label>
<small
className="admin-form-hint"
style={{ display: "block", marginBottom: 8 }}
<Box sx={{ mt: 2 }}>
<Typography
variant="body2"
sx={{ fontWeight: 600, color: "text.secondary" }}
>
Pořadí polí v PDF
</Typography>
<Typography
variant="caption"
color="text.secondary"
sx={{ display: "block", mb: 1 }}
>
Určuje pořadí řádků v adresním bloku zákazníka na PDF nabídce.
</small>
<div className="admin-reorder-list">
</Typography>
<Box sx={{ display: "flex", flexDirection: "column", gap: 0.5 }}>
{fullFieldOrder.map((key, index) => (
<div key={key} className="admin-reorder-item">
<div className="admin-reorder-arrows">
<button
type="button"
<Box
key={key}
sx={{
display: "flex",
alignItems: "center",
gap: 1,
px: 1,
py: 0.5,
border: 1,
borderColor: "divider",
borderRadius: 1,
}}
>
<Box sx={{ display: "flex", flexDirection: "column" }}>
<IconButton
size="small"
onClick={() => moveField(index, -1)}
disabled={index === 0}
className="admin-btn-icon"
title="Nahoru"
aria-label="Nahoru"
sx={{ p: 0.25 }}
>
<svg
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M18 15l-6-6-6 6" />
</svg>
</button>
<button
type="button"
{UpIcon}
</IconButton>
<IconButton
size="small"
onClick={() => moveField(index, 1)}
disabled={index === fullFieldOrder.length - 1}
className="admin-btn-icon"
title="Dolů"
aria-label="Dolů"
sx={{ p: 0.25 }}
>
<svg
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M6 9l6 6 6-6" />
</svg>
</button>
</div>
<span
className={`admin-reorder-label${key.startsWith("custom_") ? " accent" : ""}`}
{DownIcon}
</IconButton>
</Box>
<Typography
variant="body2"
sx={{
color: key.startsWith("custom_")
? "primary.main"
: "text.primary",
fontWeight: key.startsWith("custom_") ? 600 : 400,
}}
>
{getFieldDisplayName(key)}
</span>
</div>
</Typography>
</Box>
))}
</div>
</div>
</div>
</FormModal>
</Box>
</Box>
</Modal>
{/* Delete Confirm Modal */}
<ConfirmModal
<ConfirmDialog
isOpen={deleteConfirm.show}
onClose={() => setDeleteConfirm({ show: false, customer: null })}
onConfirm={handleDelete}
@@ -751,9 +813,9 @@ export default function OffersCustomers() {
message={`Opravdu chcete smazat zákazníka "${deleteConfirm.customer?.name}"? Tato akce je nevratná.`}
confirmText="Smazat"
cancelText="Zrušit"
type="danger"
confirmVariant="danger"
loading={deleting}
/>
</div>
</Box>
);
}