feat(warehouse): add items list and detail pages
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
671
src/admin/pages/WarehouseItemDetail.tsx
Normal file
671
src/admin/pages/WarehouseItemDetail.tsx
Normal file
@@ -0,0 +1,671 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useParams, useNavigate, Link } from "react-router-dom";
|
||||
import { useAlert } from "../context/AlertContext";
|
||||
import { useAuth } from "../context/AuthContext";
|
||||
import Forbidden from "../components/Forbidden";
|
||||
import { motion } from "framer-motion";
|
||||
import FormField from "../components/FormField";
|
||||
import useModalLock from "../hooks/useModalLock";
|
||||
|
||||
import apiFetch from "../utils/api";
|
||||
import { formatCurrency, formatDate } from "../utils/formatters";
|
||||
import {
|
||||
warehouseItemDetailOptions,
|
||||
warehouseCategoryListOptions,
|
||||
type WarehouseItem,
|
||||
} from "../lib/queries/warehouse";
|
||||
|
||||
interface Batch {
|
||||
id: number;
|
||||
quantity: number;
|
||||
original_qty: number;
|
||||
unit_price: number;
|
||||
received_at: string | null;
|
||||
is_consumed: boolean;
|
||||
}
|
||||
|
||||
interface ItemLocation {
|
||||
id: number;
|
||||
quantity: number;
|
||||
location: {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
} | null;
|
||||
}
|
||||
|
||||
interface ItemDetail extends WarehouseItem {
|
||||
batches?: Batch[];
|
||||
item_locations?: ItemLocation[];
|
||||
}
|
||||
|
||||
const API_BASE = "/api/admin/warehouse/items";
|
||||
|
||||
interface ItemForm {
|
||||
item_number: string;
|
||||
name: string;
|
||||
description: string;
|
||||
category_id: string;
|
||||
unit: string;
|
||||
min_quantity: string;
|
||||
notes: string;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export default function WarehouseItemDetail() {
|
||||
const { id } = useParams();
|
||||
const alert = useAlert();
|
||||
const { hasPermission } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [form, setForm] = useState<ItemForm>({
|
||||
item_number: "",
|
||||
name: "",
|
||||
description: "",
|
||||
category_id: "",
|
||||
unit: "ks",
|
||||
min_quantity: "",
|
||||
notes: "",
|
||||
is_active: true,
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const itemQuery = useQuery(warehouseItemDetailOptions(id));
|
||||
const item = itemQuery.data as ItemDetail | undefined;
|
||||
const isPending = itemQuery.isPending;
|
||||
|
||||
const { data: categories = [] } = useQuery(warehouseCategoryListOptions());
|
||||
|
||||
const formInitialized = useRef(false);
|
||||
useEffect(() => {
|
||||
formInitialized.current = false;
|
||||
}, [id]);
|
||||
|
||||
useEffect(() => {
|
||||
if (item && !formInitialized.current) {
|
||||
setForm({
|
||||
item_number: item.item_number || "",
|
||||
name: item.name,
|
||||
description: item.description || "",
|
||||
category_id: item.category_id ? String(item.category_id) : "",
|
||||
unit: item.unit,
|
||||
min_quantity:
|
||||
item.min_quantity != null ? String(item.min_quantity) : "",
|
||||
notes: item.notes || "",
|
||||
is_active: item.is_active,
|
||||
});
|
||||
formInitialized.current = true;
|
||||
}
|
||||
}, [item]);
|
||||
|
||||
useEffect(() => {
|
||||
if (itemQuery.error) {
|
||||
alert.error("Položka nenalezena");
|
||||
navigate("/warehouse/items");
|
||||
}
|
||||
}, [itemQuery.error]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
useModalLock(editing);
|
||||
|
||||
if (!hasPermission("warehouse.view")) return <Forbidden />;
|
||||
|
||||
const canManage = hasPermission("warehouse.manage");
|
||||
|
||||
const updateForm = (field: keyof ItemForm, value: string | boolean) =>
|
||||
setForm((prev) => ({ ...prev, [field]: value }));
|
||||
|
||||
const handleSave = async () => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!form.name.trim()) newErrors.name = "Zadejte název položky";
|
||||
if (!form.unit.trim()) newErrors.unit = "Zadejte jednotku";
|
||||
setErrors(newErrors);
|
||||
if (Object.keys(newErrors).length > 0) return;
|
||||
|
||||
setSaving(true);
|
||||
try {
|
||||
const isNew = id === "new";
|
||||
const url = isNew ? API_BASE : `${API_BASE}/${id}`;
|
||||
const method = isNew ? "POST" : "PUT";
|
||||
|
||||
const payload: Record<string, unknown> = {
|
||||
item_number: form.item_number.trim() || null,
|
||||
name: form.name.trim(),
|
||||
description: form.description.trim() || null,
|
||||
category_id: form.category_id ? Number(form.category_id) : null,
|
||||
unit: form.unit.trim(),
|
||||
min_quantity: form.min_quantity ? Number(form.min_quantity) : null,
|
||||
notes: form.notes.trim() || null,
|
||||
is_active: form.is_active,
|
||||
};
|
||||
|
||||
const response = await apiFetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
setEditing(false);
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
alert.success(result.message || "Položka byla uložena");
|
||||
|
||||
if (isNew && result.data?.id) {
|
||||
navigate(`/warehouse/items/${result.data.id}`, { replace: true });
|
||||
}
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se uložit položku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!item) return;
|
||||
try {
|
||||
const response = await apiFetch(`${API_BASE}/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
navigate("/warehouse/items");
|
||||
setTimeout(() => alert.success("Položka byla smazána"), 300);
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se smazat položku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
}
|
||||
};
|
||||
|
||||
const handleToggleActive = async () => {
|
||||
if (!item) return;
|
||||
try {
|
||||
const response = await apiFetch(`${API_BASE}/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ is_active: !item.is_active }),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
alert.success(
|
||||
item.is_active
|
||||
? "Položka byla deaktivována"
|
||||
: "Položka byla aktivována",
|
||||
);
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se změnit stav");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
}
|
||||
};
|
||||
|
||||
if (isPending && id !== "new") {
|
||||
return (
|
||||
<div className="admin-loading">
|
||||
<div className="admin-spinner" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const batches: Batch[] = item?.batches ?? [];
|
||||
const locations: ItemLocation[] = item?.item_locations ?? [];
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* Header */}
|
||||
<motion.div
|
||||
className="admin-page-header"
|
||||
initial={{ opacity: 0, y: 12 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.25 }}
|
||||
>
|
||||
<div style={{ display: "flex", alignItems: "center", gap: "1rem" }}>
|
||||
<Link
|
||||
to="/warehouse/items"
|
||||
className="admin-btn-icon"
|
||||
title="Zpět na seznam"
|
||||
aria-label="Zpět na seznam"
|
||||
>
|
||||
<svg
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
>
|
||||
<path d="M19 12H5M12 19l-7-7 7-7" />
|
||||
</svg>
|
||||
</Link>
|
||||
<div>
|
||||
<h1 className="admin-page-title">
|
||||
{id === "new"
|
||||
? "Nová položka"
|
||||
: item
|
||||
? item.name
|
||||
: "Detail položky"}
|
||||
</h1>
|
||||
{item?.item_number && (
|
||||
<p className="admin-page-subtitle">{item.item_number}</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{canManage && (
|
||||
<div className="admin-page-actions">
|
||||
{editing ? (
|
||||
<>
|
||||
<button
|
||||
onClick={() => {
|
||||
setEditing(false);
|
||||
setErrors({});
|
||||
if (item) {
|
||||
setForm({
|
||||
item_number: item.item_number || "",
|
||||
name: item.name,
|
||||
description: item.description || "",
|
||||
category_id: item.category_id
|
||||
? String(item.category_id)
|
||||
: "",
|
||||
unit: item.unit,
|
||||
min_quantity:
|
||||
item.min_quantity != null
|
||||
? String(item.min_quantity)
|
||||
: "",
|
||||
notes: item.notes || "",
|
||||
is_active: item.is_active,
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
disabled={saving}
|
||||
>
|
||||
Zrušit
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSave}
|
||||
className="admin-btn admin-btn-primary"
|
||||
disabled={saving}
|
||||
>
|
||||
{saving ? (
|
||||
<>
|
||||
<div className="admin-spinner admin-spinner-sm" />
|
||||
Ukládání...
|
||||
</>
|
||||
) : (
|
||||
"Uložit"
|
||||
)}
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
{id !== "new" && item && (
|
||||
<>
|
||||
<button
|
||||
onClick={handleToggleActive}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
>
|
||||
{item.is_active ? "Deaktivovat" : "Aktivovat"}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
>
|
||||
Smazat
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
onClick={() => setEditing(true)}
|
||||
className="admin-btn admin-btn-primary"
|
||||
>
|
||||
<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>
|
||||
Upravit
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</motion.div>
|
||||
|
||||
{/* Basic info */}
|
||||
<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">
|
||||
<h3 className="admin-card-title">Základní údaje</h3>
|
||||
<div className="admin-form">
|
||||
{editing || id === "new" ? (
|
||||
<>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Číslo položky" error={errors.item_number}>
|
||||
<input
|
||||
type="text"
|
||||
value={form.item_number}
|
||||
onChange={(e) => {
|
||||
updateForm("item_number", e.target.value);
|
||||
setErrors((prev) => ({ ...prev, item_number: "" }));
|
||||
}}
|
||||
className="admin-form-input"
|
||||
placeholder="Např. SKL-001"
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Název" error={errors.name} required>
|
||||
<input
|
||||
type="text"
|
||||
value={form.name}
|
||||
onChange={(e) => {
|
||||
updateForm("name", e.target.value);
|
||||
setErrors((prev) => ({ ...prev, name: "" }));
|
||||
}}
|
||||
className="admin-form-input"
|
||||
placeholder="Název položky"
|
||||
aria-invalid={!!errors.name}
|
||||
/>
|
||||
</FormField>
|
||||
</div>
|
||||
<FormField label="Popis">
|
||||
<textarea
|
||||
value={form.description}
|
||||
onChange={(e) => updateForm("description", e.target.value)}
|
||||
className="admin-form-input"
|
||||
rows={3}
|
||||
placeholder="Volitelný popis položky"
|
||||
/>
|
||||
</FormField>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Kategorie">
|
||||
<select
|
||||
value={form.category_id}
|
||||
onChange={(e) =>
|
||||
updateForm("category_id", e.target.value)
|
||||
}
|
||||
className="admin-form-select"
|
||||
>
|
||||
<option value="">— Bez kategorie —</option>
|
||||
{categories.map((cat) => (
|
||||
<option key={cat.id} value={cat.id}>
|
||||
{cat.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</FormField>
|
||||
<FormField label="Jednotka" error={errors.unit} required>
|
||||
<input
|
||||
type="text"
|
||||
value={form.unit}
|
||||
onChange={(e) => {
|
||||
updateForm("unit", e.target.value);
|
||||
setErrors((prev) => ({ ...prev, unit: "" }));
|
||||
}}
|
||||
className="admin-form-input"
|
||||
placeholder="ks, m, kg..."
|
||||
aria-invalid={!!errors.unit}
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Minimální množství">
|
||||
<input
|
||||
type="number"
|
||||
inputMode="numeric"
|
||||
value={form.min_quantity}
|
||||
onChange={(e) =>
|
||||
updateForm("min_quantity", e.target.value)
|
||||
}
|
||||
className="admin-form-input"
|
||||
placeholder="0"
|
||||
min="0"
|
||||
/>
|
||||
<small className="admin-form-hint">
|
||||
Upozornění při poklesu pod tuto hranici
|
||||
</small>
|
||||
</FormField>
|
||||
</div>
|
||||
<FormField label="Poznámky">
|
||||
<textarea
|
||||
value={form.notes}
|
||||
onChange={(e) => updateForm("notes", e.target.value)}
|
||||
className="admin-form-input"
|
||||
rows={2}
|
||||
placeholder="Volitelné poznámky"
|
||||
/>
|
||||
</FormField>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Číslo položky">
|
||||
<div style={{ fontWeight: 500 }} className="admin-mono">
|
||||
{item?.item_number || "—"}
|
||||
</div>
|
||||
</FormField>
|
||||
<FormField label="Název">
|
||||
<div style={{ fontWeight: 500 }}>{item?.name || "—"}</div>
|
||||
</FormField>
|
||||
</div>
|
||||
<FormField label="Popis">
|
||||
<div>{item?.description || "—"}</div>
|
||||
</FormField>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Kategorie">
|
||||
<div>{item?.category?.name || "—"}</div>
|
||||
</FormField>
|
||||
<FormField label="Jednotka">
|
||||
<div>{item?.unit || "—"}</div>
|
||||
</FormField>
|
||||
<FormField label="Minimální množství">
|
||||
<div className="admin-mono">
|
||||
{item?.min_quantity != null ? item.min_quantity : "—"}
|
||||
</div>
|
||||
</FormField>
|
||||
</div>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Stav">
|
||||
{item?.is_active ? (
|
||||
<span className="admin-badge admin-badge-active">
|
||||
Aktivní
|
||||
</span>
|
||||
) : (
|
||||
<span className="admin-badge admin-badge-inactive">
|
||||
Neaktivní
|
||||
</span>
|
||||
)}
|
||||
</FormField>
|
||||
</div>
|
||||
{item?.notes && (
|
||||
<FormField label="Poznámky">
|
||||
<div style={{ whiteSpace: "pre-wrap" }}>{item.notes}</div>
|
||||
</FormField>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
{/* Stock info — only in view mode, for existing items */}
|
||||
{id !== "new" && !editing && item && (
|
||||
<motion.div
|
||||
className="admin-card"
|
||||
initial={{ opacity: 0, y: 12 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.25, delay: 0.08 }}
|
||||
>
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Skladové zásoby</h3>
|
||||
<div className="admin-kpi-grid admin-kpi-4">
|
||||
<div className="admin-stat-card info">
|
||||
<div className="admin-stat-label">Celkové množství</div>
|
||||
<div className="admin-stat-value admin-mono">
|
||||
{item.total_quantity ?? 0} {item.unit}
|
||||
</div>
|
||||
</div>
|
||||
<div className="admin-stat-card success">
|
||||
<div className="admin-stat-label">K dispozici</div>
|
||||
<div className="admin-stat-value admin-mono">
|
||||
{item.available_quantity ?? 0} {item.unit}
|
||||
</div>
|
||||
</div>
|
||||
<div className="admin-stat-card warning">
|
||||
<div className="admin-stat-label">Hodnota zásob</div>
|
||||
<div className="admin-stat-value admin-mono">
|
||||
{item.stock_value != null
|
||||
? formatCurrency(item.stock_value, "CZK")
|
||||
: "0,00 Kč"}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
className={`admin-stat-card ${item.below_minimum ? "danger" : "info"}`}
|
||||
>
|
||||
<div className="admin-stat-label">Pod minimem</div>
|
||||
<div className="admin-stat-value admin-mono">
|
||||
{item.below_minimum ? "Ano" : "Ne"}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
)}
|
||||
|
||||
{/* Batches table — only in view mode, for existing items */}
|
||||
{id !== "new" && !editing && (
|
||||
<motion.div
|
||||
className="admin-card"
|
||||
initial={{ opacity: 0, y: 12 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.25, delay: 0.1 }}
|
||||
>
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Dávky (FIFO)</h3>
|
||||
{batches.length === 0 ? (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--text-tertiary)",
|
||||
fontSize: "0.875rem",
|
||||
textAlign: "center",
|
||||
padding: "1.5rem 0",
|
||||
}}
|
||||
>
|
||||
Zatím žádné dávky
|
||||
</div>
|
||||
) : (
|
||||
<div className="admin-table-responsive">
|
||||
<table className="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Datum příjmu</th>
|
||||
<th className="text-right">Původní množství</th>
|
||||
<th className="text-right">Zůstatek</th>
|
||||
<th className="text-right">Cena za jednotku</th>
|
||||
<th className="text-right">Hodnota</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{batches.map((batch) => (
|
||||
<tr key={batch.id}>
|
||||
<td className="admin-mono">
|
||||
{formatDate(batch.received_at)}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{Number(batch.original_qty)}
|
||||
</td>
|
||||
<td className="admin-mono text-right fw-500">
|
||||
{Number(batch.quantity)}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{formatCurrency(Number(batch.unit_price), "CZK")}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{formatCurrency(
|
||||
Number(batch.quantity) * Number(batch.unit_price),
|
||||
"CZK",
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
)}
|
||||
|
||||
{/* Locations table — only in view mode, for existing items */}
|
||||
{id !== "new" && !editing && (
|
||||
<motion.div
|
||||
className="admin-card"
|
||||
initial={{ opacity: 0, y: 12 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.25, delay: 0.12 }}
|
||||
>
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Umístění</h3>
|
||||
{locations.length === 0 ? (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--text-tertiary)",
|
||||
fontSize: "0.875rem",
|
||||
textAlign: "center",
|
||||
padding: "1.5rem 0",
|
||||
}}
|
||||
>
|
||||
Zatím žádná umístění
|
||||
</div>
|
||||
) : (
|
||||
<div className="admin-table-responsive">
|
||||
<table className="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Kód</th>
|
||||
<th>Název</th>
|
||||
<th className="text-right">Množství</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{locations.map((loc) => (
|
||||
<tr key={loc.id}>
|
||||
<td className="admin-mono fw-500">
|
||||
{loc.location?.code || "—"}
|
||||
</td>
|
||||
<td>{loc.location?.name || "—"}</td>
|
||||
<td className="admin-mono text-right fw-500">
|
||||
{Number(loc.quantity)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
319
src/admin/pages/WarehouseItems.tsx
Normal file
319
src/admin/pages/WarehouseItems.tsx
Normal file
@@ -0,0 +1,319 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useAlert } from "../context/AlertContext";
|
||||
import { useAuth } from "../context/AuthContext";
|
||||
import Forbidden from "../components/Forbidden";
|
||||
import { motion, AnimatePresence } from "framer-motion";
|
||||
import Pagination from "../components/Pagination";
|
||||
import SortIcon from "../components/SortIcon";
|
||||
import useTableSort from "../hooks/useTableSort";
|
||||
import useDebounce from "../hooks/useDebounce";
|
||||
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
||||
|
||||
import { formatCurrency } from "../utils/formatters";
|
||||
import {
|
||||
warehouseItemListOptions,
|
||||
warehouseCategoryListOptions,
|
||||
type WarehouseItem,
|
||||
} from "../lib/queries/warehouse";
|
||||
|
||||
const PER_PAGE = 20;
|
||||
|
||||
export default function WarehouseItems() {
|
||||
const alert = useAlert();
|
||||
const { hasPermission } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [page, setPage] = useState(1);
|
||||
const [search, setSearch] = useState("");
|
||||
const [categoryId, setCategoryId] = useState<number | "">("");
|
||||
const debouncedSearch = useDebounce(search, 300);
|
||||
|
||||
const { sort, order, handleSort, activeSort } = useTableSort("item_number");
|
||||
|
||||
const { data: categories = [] } = useQuery(warehouseCategoryListOptions());
|
||||
|
||||
const { items, pagination, isPending, isFetching } =
|
||||
usePaginatedQuery<WarehouseItem>(
|
||||
warehouseItemListOptions({
|
||||
search: debouncedSearch || undefined,
|
||||
page,
|
||||
perPage: PER_PAGE,
|
||||
sort,
|
||||
order,
|
||||
category_id: categoryId ? Number(categoryId) : undefined,
|
||||
}),
|
||||
);
|
||||
|
||||
if (!hasPermission("warehouse.view")) return <Forbidden />;
|
||||
|
||||
const canManage = hasPermission("warehouse.manage");
|
||||
|
||||
const handleRowClick = (item: WarehouseItem) => {
|
||||
navigate(`/warehouse/items/${item.id}`);
|
||||
};
|
||||
|
||||
if (isPending) {
|
||||
return (
|
||||
<div className="admin-loading">
|
||||
<div className="admin-spinner" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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">Skladové položky</h1>
|
||||
<p className="admin-page-subtitle">
|
||||
{pagination?.total ?? items.length}{" "}
|
||||
{pagination?.total === 1
|
||||
? "položka"
|
||||
: pagination?.total !== undefined &&
|
||||
pagination.total >= 2 &&
|
||||
pagination.total <= 4
|
||||
? "položky"
|
||||
: "položek"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="admin-page-actions">
|
||||
{canManage && (
|
||||
<button
|
||||
onClick={() => navigate("/warehouse/items/new")}
|
||||
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 položku
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
<motion.div
|
||||
className="admin-card"
|
||||
initial={{ opacity: 0, y: 12 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.25, delay: 0.06 }}
|
||||
style={{ opacity: isFetching ? 0.6 : 1, transition: "opacity 0.2s" }}
|
||||
>
|
||||
<div className="admin-card-body">
|
||||
<div className="admin-search-bar mb-4">
|
||||
<div className="admin-search">
|
||||
<svg
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65" />
|
||||
</svg>
|
||||
<input
|
||||
type="text"
|
||||
value={search}
|
||||
onChange={(e) => {
|
||||
setSearch(e.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
placeholder="Hledat podle názvu nebo čísla položky..."
|
||||
className="admin-form-input"
|
||||
/>
|
||||
</div>
|
||||
{categories.length > 0 && (
|
||||
<select
|
||||
value={categoryId}
|
||||
onChange={(e) => {
|
||||
setCategoryId(
|
||||
e.target.value === "" ? "" : Number(e.target.value),
|
||||
);
|
||||
setPage(1);
|
||||
}}
|
||||
className="admin-form-select"
|
||||
style={{ minWidth: 180 }}
|
||||
>
|
||||
<option value="">Všechny kategorie</option>
|
||||
{categories.map((cat) => (
|
||||
<option key={cat.id} value={cat.id}>
|
||||
{cat.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<AnimatePresence mode="wait">
|
||||
<motion.div
|
||||
key={`${debouncedSearch}-${categoryId}-${page}-${sort}-${order}-${items.length}`}
|
||||
initial={{ opacity: 0, y: 6 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0 }}
|
||||
transition={{ duration: 0.15 }}
|
||||
>
|
||||
{items.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 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" />
|
||||
<polyline points="3.27 6.96 12 12.01 20.73 6.96" />
|
||||
<line x1="12" y1="22.08" x2="12" y2="12" />
|
||||
</svg>
|
||||
</div>
|
||||
<p>
|
||||
{search || categoryId
|
||||
? "Žádné položky pro zadaný filtr."
|
||||
: "Zatím nejsou žádné skladové položky."}
|
||||
</p>
|
||||
{canManage && !search && !categoryId && (
|
||||
<button
|
||||
onClick={() => navigate("/warehouse/items/new")}
|
||||
className="admin-btn admin-btn-primary"
|
||||
>
|
||||
Přidat první položku
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{items.length > 0 && (
|
||||
<div className="admin-table-responsive">
|
||||
<table className="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th
|
||||
style={{ cursor: "pointer" }}
|
||||
onClick={() => handleSort("item_number")}
|
||||
>
|
||||
Číslo{" "}
|
||||
<SortIcon
|
||||
column="item_number"
|
||||
sort={activeSort}
|
||||
order={order}
|
||||
/>
|
||||
</th>
|
||||
<th
|
||||
style={{ cursor: "pointer" }}
|
||||
onClick={() => handleSort("name")}
|
||||
>
|
||||
Název{" "}
|
||||
<SortIcon
|
||||
column="name"
|
||||
sort={activeSort}
|
||||
order={order}
|
||||
/>
|
||||
</th>
|
||||
<th>Kategorie</th>
|
||||
<th>Jednotka</th>
|
||||
<th
|
||||
style={{ cursor: "pointer" }}
|
||||
onClick={() => handleSort("total_quantity")}
|
||||
className="text-right"
|
||||
>
|
||||
Množství{" "}
|
||||
<SortIcon
|
||||
column="total_quantity"
|
||||
sort={activeSort}
|
||||
order={order}
|
||||
/>
|
||||
</th>
|
||||
<th className="text-right">K dispozici</th>
|
||||
<th
|
||||
style={{ cursor: "pointer" }}
|
||||
onClick={() => handleSort("stock_value")}
|
||||
className="text-right"
|
||||
>
|
||||
Hodnota{" "}
|
||||
<SortIcon
|
||||
column="stock_value"
|
||||
sort={activeSort}
|
||||
order={order}
|
||||
/>
|
||||
</th>
|
||||
<th>Stav</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{items.map((item) => (
|
||||
<tr
|
||||
key={item.id}
|
||||
onClick={() => handleRowClick(item)}
|
||||
className={
|
||||
!item.is_active ? "admin-table-row-inactive" : ""
|
||||
}
|
||||
style={{ cursor: "pointer" }}
|
||||
>
|
||||
<td className="admin-mono">
|
||||
{item.item_number || "—"}
|
||||
</td>
|
||||
<td className="fw-500">{item.name}</td>
|
||||
<td>{item.category?.name || "—"}</td>
|
||||
<td>{item.unit}</td>
|
||||
<td className="admin-mono text-right">
|
||||
{item.total_quantity ?? 0}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{item.available_quantity ?? 0}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{item.stock_value != null
|
||||
? formatCurrency(item.stock_value, "CZK")
|
||||
: "0,00 Kč"}
|
||||
</td>
|
||||
<td>
|
||||
{item.below_minimum ? (
|
||||
<span className="admin-badge admin-badge-danger">
|
||||
Pod min.
|
||||
</span>
|
||||
) : item.is_active ? (
|
||||
<span className="admin-badge admin-badge-active">
|
||||
OK
|
||||
</span>
|
||||
) : (
|
||||
<span className="admin-badge admin-badge-inactive">
|
||||
Neaktivní
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</motion.div>
|
||||
</AnimatePresence>
|
||||
|
||||
<Pagination pagination={pagination} onPageChange={setPage} />
|
||||
</div>
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user