feat(mui): migrate Vozidla (Vehicles) page onto MUI kit
DataTable (dense, mono numerics, inactive dimming, Chip status toggle), Modal add/edit form (Field + TextField + SwitchField), ConfirmDialog delete. All data logic, mutations (invalidate vehicles+trips), validation (SPZ/Nazev), and SPZ-uppercasing preserved verbatim. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,16 +1,27 @@
|
|||||||
import { useState } from "react";
|
import { useState } 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 Chip from "@mui/material/Chip";
|
||||||
|
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 } from "framer-motion";
|
|
||||||
import ConfirmModal from "../components/ConfirmModal";
|
|
||||||
import FormModal from "../components/FormModal";
|
|
||||||
|
|
||||||
import { formatKm } from "../utils/formatters";
|
import { formatKm } from "../utils/formatters";
|
||||||
import FormField from "../components/FormField";
|
|
||||||
import { vehicleListOptions } from "../lib/queries/vehicles";
|
import { vehicleListOptions } from "../lib/queries/vehicles";
|
||||||
import { useApiMutation } from "../lib/queries/mutations";
|
import { useApiMutation } from "../lib/queries/mutations";
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
Card,
|
||||||
|
DataTable,
|
||||||
|
Modal,
|
||||||
|
ConfirmDialog,
|
||||||
|
Field,
|
||||||
|
TextField,
|
||||||
|
SwitchField,
|
||||||
|
type DataColumn,
|
||||||
|
} from "../ui";
|
||||||
|
|
||||||
const API_BASE = "/api/admin";
|
const API_BASE = "/api/admin";
|
||||||
|
|
||||||
@@ -35,6 +46,50 @@ interface VehicleForm {
|
|||||||
is_active: boolean;
|
is_active: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 Vehicles() {
|
export default function Vehicles() {
|
||||||
const alert = useAlert();
|
const alert = useAlert();
|
||||||
const { hasPermission } = useAuth();
|
const { hasPermission } = useAuth();
|
||||||
@@ -174,272 +229,205 @@ export default function Vehicles() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const columns: DataColumn<Vehicle>[] = [
|
||||||
|
{ key: "spz", header: "SPZ", mono: true, render: (v) => v.spz },
|
||||||
|
{ key: "name", header: "Název", render: (v) => v.name },
|
||||||
|
{
|
||||||
|
key: "brand",
|
||||||
|
header: "Značka / Model",
|
||||||
|
render: (v) =>
|
||||||
|
v.brand || v.model ? `${v.brand || ""} ${v.model || ""}`.trim() : "—",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "initial_km",
|
||||||
|
header: "Počáteční km",
|
||||||
|
align: "right",
|
||||||
|
mono: true,
|
||||||
|
render: (v) => `${formatKm(v.initial_km)} km`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "current_km",
|
||||||
|
header: "Aktuální km",
|
||||||
|
align: "right",
|
||||||
|
mono: true,
|
||||||
|
render: (v) => `${formatKm(v.current_km)} km`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "trips",
|
||||||
|
header: "Počet jízd",
|
||||||
|
align: "right",
|
||||||
|
mono: true,
|
||||||
|
render: (v) => v.trip_count,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "status",
|
||||||
|
header: "Stav",
|
||||||
|
render: (v) => (
|
||||||
|
<Chip
|
||||||
|
label={v.is_active ? "Aktivní" : "Neaktivní"}
|
||||||
|
size="small"
|
||||||
|
color={v.is_active ? "success" : "default"}
|
||||||
|
onClick={() => toggleActive(v)}
|
||||||
|
sx={{ cursor: "pointer", fontWeight: 600 }}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "actions",
|
||||||
|
header: "Akce",
|
||||||
|
align: "right",
|
||||||
|
render: (v) => (
|
||||||
|
<Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={() => openEditModal(v)}
|
||||||
|
aria-label="Upravit"
|
||||||
|
title="Upravit"
|
||||||
|
>
|
||||||
|
{EditIcon}
|
||||||
|
</IconButton>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
color="error"
|
||||||
|
onClick={() => setDeleteConfirm({ show: true, vehicle: v })}
|
||||||
|
aria-label="Smazat"
|
||||||
|
title="Smazat"
|
||||||
|
>
|
||||||
|
{DeleteIcon}
|
||||||
|
</IconButton>
|
||||||
|
</Box>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<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>
|
<Box
|
||||||
<h1 className="admin-page-title">Správa vozidel</h1>
|
sx={{
|
||||||
</div>
|
display: "flex",
|
||||||
<div className="admin-page-actions">
|
alignItems: "center",
|
||||||
<button
|
justifyContent: "space-between",
|
||||||
onClick={openCreateModal}
|
mb: 3,
|
||||||
className="admin-btn admin-btn-primary"
|
flexWrap: "wrap",
|
||||||
|
gap: 2,
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<svg
|
<Typography variant="h4">Správa vozidel</Typography>
|
||||||
width="20"
|
<Button startIcon={PlusIcon} onClick={openCreateModal}>
|
||||||
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 vozidlo
|
Přidat vozidlo
|
||||||
</button>
|
</Button>
|
||||||
</div>
|
</Box>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
|
|
||||||
<motion.div
|
<motion.div
|
||||||
className="admin-card"
|
|
||||||
initial={{ opacity: 0, y: 12 }}
|
initial={{ opacity: 0, y: 12 }}
|
||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
transition={{ duration: 0.25, delay: 0.06 }}
|
transition={{ duration: 0.25, delay: 0.06 }}
|
||||||
>
|
>
|
||||||
<div className="admin-card-body">
|
<Card>
|
||||||
{vehicles.length === 0 && (
|
<DataTable<Vehicle>
|
||||||
<div className="admin-empty-state">
|
columns={columns}
|
||||||
<div className="admin-empty-icon">
|
rows={vehicles}
|
||||||
<svg
|
rowKey={(v) => v.id}
|
||||||
width="28"
|
rowInactive={(v) => !v.is_active}
|
||||||
height="28"
|
empty={
|
||||||
viewBox="0 0 24 24"
|
<Box sx={{ textAlign: "center", py: 6 }}>
|
||||||
fill="none"
|
<Typography color="text.secondary" gutterBottom>
|
||||||
stroke="currentColor"
|
Zatím nejsou žádná vozidla.
|
||||||
strokeWidth="1.5"
|
</Typography>
|
||||||
strokeLinecap="round"
|
<Button startIcon={PlusIcon} onClick={openCreateModal}>
|
||||||
strokeLinejoin="round"
|
|
||||||
>
|
|
||||||
<rect x="1" y="3" width="15" height="13" />
|
|
||||||
<polygon points="16 8 20 8 23 11 23 16 16 16 16 8" />
|
|
||||||
<circle cx="5.5" cy="18.5" r="2.5" />
|
|
||||||
<circle cx="18.5" cy="18.5" r="2.5" />
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
<p>Zatím nejsou žádná vozidla.</p>
|
|
||||||
<button
|
|
||||||
onClick={openCreateModal}
|
|
||||||
className="admin-btn admin-btn-primary"
|
|
||||||
>
|
|
||||||
Přidat první vozidlo
|
Přidat první vozidlo
|
||||||
</button>
|
</Button>
|
||||||
</div>
|
</Box>
|
||||||
)}
|
|
||||||
{vehicles.length > 0 && (
|
|
||||||
<div className="admin-table-responsive">
|
|
||||||
<table className="admin-table">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>SPZ</th>
|
|
||||||
<th>Název</th>
|
|
||||||
<th>Značka / Model</th>
|
|
||||||
<th>Počáteční km</th>
|
|
||||||
<th>Aktuální km</th>
|
|
||||||
<th>Počet jízd</th>
|
|
||||||
<th>Stav</th>
|
|
||||||
<th>Akce</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{vehicles.map((vehicle) => (
|
|
||||||
<tr
|
|
||||||
key={vehicle.id}
|
|
||||||
className={
|
|
||||||
!vehicle.is_active ? "admin-table-row-inactive" : ""
|
|
||||||
}
|
}
|
||||||
>
|
/>
|
||||||
<td className="admin-mono fw-500">{vehicle.spz}</td>
|
</Card>
|
||||||
<td>{vehicle.name}</td>
|
|
||||||
<td>
|
|
||||||
{vehicle.brand || vehicle.model
|
|
||||||
? `${vehicle.brand || ""} ${vehicle.model || ""}`.trim()
|
|
||||||
: "—"}
|
|
||||||
</td>
|
|
||||||
<td className="admin-mono">
|
|
||||||
{formatKm(vehicle.initial_km)} km
|
|
||||||
</td>
|
|
||||||
<td className="admin-mono fw-500">
|
|
||||||
{formatKm(vehicle.current_km)} km
|
|
||||||
</td>
|
|
||||||
<td className="admin-mono">{vehicle.trip_count}</td>
|
|
||||||
<td>
|
|
||||||
<button
|
|
||||||
onClick={() => toggleActive(vehicle)}
|
|
||||||
className={`admin-badge ${vehicle.is_active ? "admin-badge-active" : "admin-badge-inactive"}`}
|
|
||||||
>
|
|
||||||
{vehicle.is_active ? "Aktivní" : "Neaktivní"}
|
|
||||||
</button>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<div className="admin-table-actions">
|
|
||||||
<button
|
|
||||||
onClick={() => openEditModal(vehicle)}
|
|
||||||
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, vehicle })
|
|
||||||
}
|
|
||||||
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>
|
</motion.div>
|
||||||
|
|
||||||
{/* Add/Edit Modal */}
|
<Modal
|
||||||
<FormModal
|
|
||||||
isOpen={showModal}
|
isOpen={showModal}
|
||||||
onClose={() => setShowModal(false)}
|
onClose={() => setShowModal(false)}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
title={editingVehicle ? "Upravit vozidlo" : "Přidat vozidlo"}
|
title={editingVehicle ? "Upravit vozidlo" : "Přidat vozidlo"}
|
||||||
loading={saving}
|
loading={saving}
|
||||||
>
|
>
|
||||||
<div className="admin-form">
|
<Box sx={{ display: "flex", gap: 2, flexWrap: "wrap" }}>
|
||||||
<div className="admin-form-row">
|
<Box sx={{ flex: "1 1 200px" }}>
|
||||||
<FormField label="SPZ" error={errors.spz} required>
|
<Field label="SPZ" required error={errors.spz}>
|
||||||
<input
|
<TextField
|
||||||
type="text"
|
|
||||||
value={form.spz}
|
value={form.spz}
|
||||||
|
placeholder="1AB 2345"
|
||||||
|
error={!!errors.spz}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setForm({
|
setForm({ ...form, spz: e.target.value.toUpperCase() });
|
||||||
...form,
|
|
||||||
spz: e.target.value.toUpperCase(),
|
|
||||||
});
|
|
||||||
setErrors((prev) => ({ ...prev, spz: "" }));
|
setErrors((prev) => ({ ...prev, spz: "" }));
|
||||||
}}
|
}}
|
||||||
className="admin-form-input"
|
|
||||||
placeholder="1AB 2345"
|
|
||||||
aria-invalid={!!errors.spz}
|
|
||||||
/>
|
/>
|
||||||
</FormField>
|
</Field>
|
||||||
|
</Box>
|
||||||
<FormField label="Název" error={errors.name} required>
|
<Box sx={{ flex: "1 1 200px" }}>
|
||||||
<input
|
<Field label="Název" required error={errors.name}>
|
||||||
type="text"
|
<TextField
|
||||||
value={form.name}
|
value={form.name}
|
||||||
|
placeholder="Služební #1"
|
||||||
|
error={!!errors.name}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setForm({ ...form, name: e.target.value });
|
setForm({ ...form, name: e.target.value });
|
||||||
setErrors((prev) => ({ ...prev, name: "" }));
|
setErrors((prev) => ({ ...prev, name: "" }));
|
||||||
}}
|
}}
|
||||||
className="admin-form-input"
|
|
||||||
placeholder="Služební #1"
|
|
||||||
aria-invalid={!!errors.name}
|
|
||||||
/>
|
/>
|
||||||
</FormField>
|
</Field>
|
||||||
</div>
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
<div className="admin-form-row">
|
<Box sx={{ display: "flex", gap: 2, flexWrap: "wrap" }}>
|
||||||
<FormField label="Značka">
|
<Box sx={{ flex: "1 1 200px" }}>
|
||||||
<input
|
<Field label="Značka">
|
||||||
type="text"
|
<TextField
|
||||||
value={form.brand}
|
value={form.brand}
|
||||||
onChange={(e) => setForm({ ...form, brand: e.target.value })}
|
|
||||||
className="admin-form-input"
|
|
||||||
placeholder="Škoda"
|
placeholder="Škoda"
|
||||||
|
onChange={(e) => setForm({ ...form, brand: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</FormField>
|
</Field>
|
||||||
|
</Box>
|
||||||
<FormField label="Model">
|
<Box sx={{ flex: "1 1 200px" }}>
|
||||||
<input
|
<Field label="Model">
|
||||||
type="text"
|
<TextField
|
||||||
value={form.model}
|
value={form.model}
|
||||||
onChange={(e) => setForm({ ...form, model: e.target.value })}
|
|
||||||
className="admin-form-input"
|
|
||||||
placeholder="Octavia Combi"
|
placeholder="Octavia Combi"
|
||||||
|
onChange={(e) => setForm({ ...form, model: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</FormField>
|
</Field>
|
||||||
</div>
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
<div className="admin-form-group">
|
<Field
|
||||||
<label className="admin-form-label">Počáteční stav km</label>
|
label="Počáteční stav km"
|
||||||
<input
|
hint="Stav tachometru při přidání vozidla"
|
||||||
|
>
|
||||||
|
<TextField
|
||||||
type="number"
|
type="number"
|
||||||
inputMode="numeric"
|
|
||||||
value={form.initial_km}
|
value={form.initial_km}
|
||||||
|
slotProps={{ htmlInput: { min: 0, inputMode: "numeric" } }}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
setForm({
|
setForm({ ...form, initial_km: parseInt(e.target.value) || 0 })
|
||||||
...form,
|
|
||||||
initial_km: parseInt(e.target.value) || 0,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
className="admin-form-input"
|
|
||||||
min="0"
|
|
||||||
/>
|
/>
|
||||||
<small className="admin-form-hint">
|
</Field>
|
||||||
Stav tachometru při přidání vozidla
|
|
||||||
</small>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<label className="admin-form-checkbox">
|
<SwitchField
|
||||||
<input
|
label="Vozidlo je aktivní"
|
||||||
type="checkbox"
|
|
||||||
checked={form.is_active}
|
checked={form.is_active}
|
||||||
onChange={(e) =>
|
onChange={(v) => setForm({ ...form, is_active: v })}
|
||||||
setForm({ ...form, is_active: e.target.checked })
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
<span>Vozidlo je aktivní</span>
|
</Modal>
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</FormModal>
|
|
||||||
|
|
||||||
{/* Delete Confirmation */}
|
<ConfirmDialog
|
||||||
<ConfirmModal
|
|
||||||
isOpen={deleteConfirm.show}
|
isOpen={deleteConfirm.show}
|
||||||
onClose={() => setDeleteConfirm({ show: false, vehicle: null })}
|
onClose={() => setDeleteConfirm({ show: false, vehicle: null })}
|
||||||
onConfirm={handleDelete}
|
onConfirm={handleDelete}
|
||||||
@@ -452,6 +440,6 @@ export default function Vehicles() {
|
|||||||
confirmText="Smazat"
|
confirmText="Smazat"
|
||||||
confirmVariant="danger"
|
confirmVariant="danger"
|
||||||
/>
|
/>
|
||||||
</div>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user