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