import { useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { useAlert } from "../context/AlertContext"; import { useAuth } from "../context/AuthContext"; import Forbidden from "../components/Forbidden"; import { motion, AnimatePresence } from "framer-motion"; import ConfirmModal from "../components/ConfirmModal"; import useModalLock from "../hooks/useModalLock"; import apiFetch from "../utils/api"; import FormField from "../components/FormField"; import { warehouseLocationListOptions, type WarehouseLocation, } from "../lib/queries/warehouse"; const API_BASE = "/api/admin/warehouse/locations"; interface LocationForm { code: string; name: string; description: string; } export default function WarehouseLocations() { const alert = useAlert(); const { hasPermission } = useAuth(); const queryClient = useQueryClient(); const { data: locations = [], isPending } = useQuery( warehouseLocationListOptions(), ); const [showModal, setShowModal] = useState(false); const [editingLocation, setEditingLocation] = useState(null); const [form, setForm] = useState({ code: "", name: "", description: "", }); const [errors, setErrors] = useState>({}); const [deleteConfirm, setDeleteConfirm] = useState<{ show: boolean; location: WarehouseLocation | null; }>({ show: false, location: null }); useModalLock(showModal); if (!hasPermission("warehouse.manage")) return ; const openCreateModal = () => { setEditingLocation(null); setForm({ code: "", name: "", description: "", }); setErrors({}); setShowModal(true); }; const openEditModal = (location: WarehouseLocation) => { setEditingLocation(location); setForm({ code: location.code, name: location.name, description: location.description || "", }); setErrors({}); setShowModal(true); }; const handleSubmit = async () => { const newErrors: Record = {}; if (!form.code.trim()) newErrors.code = "Zadejte kód umístění"; if (!form.name.trim()) newErrors.name = "Zadejte název umístění"; setErrors(newErrors); if (Object.keys(newErrors).length > 0) return; try { const url = editingLocation ? `${API_BASE}/${editingLocation.id}` : API_BASE; const method = editingLocation ? "PUT" : "POST"; const response = await apiFetch(url, { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }); const result = await response.json(); if (result.success) { setShowModal(false); queryClient.invalidateQueries({ queryKey: ["warehouse", "locations"] }); queryClient.invalidateQueries({ queryKey: ["warehouse"] }); alert.success(result.message); } else { alert.error(result.error); } } catch { alert.error("Chyba připojení"); } }; const handleDelete = async () => { if (!deleteConfirm.location) return; try { const response = await apiFetch( `${API_BASE}/${deleteConfirm.location.id}`, { method: "DELETE", }, ); const result = await response.json(); if (result.success) { setDeleteConfirm({ show: false, location: null }); queryClient.invalidateQueries({ queryKey: ["warehouse", "locations"] }); queryClient.invalidateQueries({ queryKey: ["warehouse"] }); alert.success(result.message); } else { alert.error(result.error); } } catch { alert.error("Chyba připojení"); } }; const toggleActive = async (location: WarehouseLocation) => { try { const response = await apiFetch(`${API_BASE}/${location.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ is_active: !location.is_active }), }); const result = await response.json(); if (result.success) { queryClient.invalidateQueries({ queryKey: ["warehouse", "locations"] }); queryClient.invalidateQueries({ queryKey: ["warehouse"] }); alert.success( location.is_active ? "Umístění bylo deaktivováno" : "Umístění bylo aktivováno", ); } else { alert.error(result.error); } } catch { alert.error("Chyba připojení"); } }; if (isPending) { return (
); } return (

Umístění skladu

{locations.length === 0 && (

Zatím nejsou žádná umístění.

)} {locations.length > 0 && (
{locations.map((location) => ( ))}
Kód Název Popis Stav Akce
{location.code} {location.name} {location.description || "—"}
)}
{/* Add/Edit Modal */} {showModal && (
setShowModal(false)} />

{editingLocation ? "Upravit umístění" : "Přidat umístění"}

{ setForm({ ...form, code: e.target.value.toUpperCase(), }); setErrors((prev) => ({ ...prev, code: "", })); }} className="admin-form-input" placeholder="A-01" aria-invalid={!!errors.code} /> { setForm({ ...form, name: e.target.value }); setErrors((prev) => ({ ...prev, name: "", })); }} className="admin-form-input" placeholder="Regál A, polička 1" aria-invalid={!!errors.name} />