import { useMemo, useState } from "react"; import { useNavigate } from "react-router"; import DeleteOutlineIcon from "@mui/icons-material/DeleteOutlined"; import EditOutlinedIcon from "@mui/icons-material/EditOutlined"; import { Alert, Box, Button, CircularProgress, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, IconButton, Paper, Stack, Table, TableBody, TableCell, TableContainer, TableHead, TablePagination, TableRow, TextField, Tooltip, Typography, } from "@mui/material"; import type { LocationListItem } from "@/domain/locations/types/location"; import { useDeleteLocation } from "@/domain/locations/use-cases/use-delete-location"; import { useLocationsList } from "@/domain/locations/use-cases/use-locations-list"; import { useDebounce } from "@/hooks/use-debounce"; export default function LocationsListPage() { const navigate = useNavigate(); const [search, setSearch] = useState(""); const [page, setPage] = useState(0); const [pageSize, setPageSize] = useState(12); const [deleteTarget, setDeleteTarget] = useState(null); const debouncedSearch = useDebounce(search, 300); const deleteLocation = useDeleteLocation(); const listParams = useMemo( () => ({ search: debouncedSearch, page: page + 1, pageSize, }), [debouncedSearch, page, pageSize], ); const { data, isLoading, isFetching, error } = useLocationsList(listParams); const rows = data?.items ?? []; const totalCount = data?.totalCount ?? 0; const handleDeleteConfirm = () => { if (!deleteTarget) { return; } deleteLocation.mutate(deleteTarget.id, { onSuccess: () => setDeleteTarget(null), }); }; return ( Locations {totalCount} total {isFetching && !isLoading && } { setSearch(event.target.value); setPage(0); }} placeholder="Search locations..." className="min-w-[220px]" /> {error && ( {error instanceof Error ? error.message : "Failed to load locations"} )} Location Name Address City State Phone Contact Status Actions {isLoading ? ( ) : rows.length === 0 ? ( No locations found ) : ( rows.map((row) => ( {row.address || "—"} {row.city || "—"} {row.state || "—"} {row.phone || "—"} {row.contact || "—"} {row.status || "—"} navigate(`/locations/edit/${row.id}`)} > setDeleteTarget(row)} > )) )}
setPage(nextPage)} rowsPerPage={pageSize} onRowsPerPageChange={(event) => { setPageSize(Number(event.target.value)); setPage(0); }} rowsPerPageOptions={[12, 24, 48]} /> setDeleteTarget(null)}> Delete Location Are you sure you want to delete "{deleteTarget?.name}"? This action cannot be undone.
); }