mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 21:42:06 +00:00
* chore: upgrade frontend application dependencies * chore: upgrade frontend infrastructure dependencies * fix: align lockfiles with CI npm * fix: preserve ky error semantics * fix: sanitize timeout error message
242 lines
7.6 KiB
TypeScript
242 lines
7.6 KiB
TypeScript
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<LocationListItem | null>(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 (
|
|
<Box className="flex flex-col gap-4 p-4">
|
|
<Stack
|
|
direction={{ xs: "column", md: "row" }}
|
|
spacing={2}
|
|
sx={{
|
|
alignItems: { xs: "stretch", md: "center" },
|
|
justifyContent: "space-between",
|
|
}}
|
|
>
|
|
<Stack
|
|
direction="row"
|
|
spacing={2}
|
|
sx={{
|
|
alignItems: "baseline",
|
|
}}
|
|
>
|
|
<Typography variant="h5" component="h1">
|
|
Locations
|
|
</Typography>
|
|
<Typography
|
|
variant="body2"
|
|
sx={{
|
|
color: "text.secondary",
|
|
}}
|
|
>
|
|
{totalCount} total
|
|
</Typography>
|
|
{isFetching && !isLoading && <CircularProgress size={16} />}
|
|
</Stack>
|
|
|
|
<Stack
|
|
direction={{ xs: "column", sm: "row" }}
|
|
spacing={2}
|
|
sx={{
|
|
alignItems: "center",
|
|
}}
|
|
>
|
|
<TextField
|
|
size="small"
|
|
value={search}
|
|
onChange={(event) => {
|
|
setSearch(event.target.value);
|
|
setPage(0);
|
|
}}
|
|
placeholder="Search locations..."
|
|
className="min-w-[220px]"
|
|
/>
|
|
<Button variant="contained" onClick={() => navigate("/locations/new")}>
|
|
+ New Location
|
|
</Button>
|
|
</Stack>
|
|
</Stack>
|
|
{error && (
|
|
<Alert severity="error">
|
|
{error instanceof Error ? error.message : "Failed to load locations"}
|
|
</Alert>
|
|
)}
|
|
<TableContainer component={Paper} variant="outlined">
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell className="font-semibold">Location Name</TableCell>
|
|
<TableCell className="font-semibold">Address</TableCell>
|
|
<TableCell className="font-semibold">City</TableCell>
|
|
<TableCell className="font-semibold">State</TableCell>
|
|
<TableCell className="font-semibold">Phone</TableCell>
|
|
<TableCell className="font-semibold">Contact</TableCell>
|
|
<TableCell className="font-semibold">Status</TableCell>
|
|
<TableCell align="right" className="font-semibold">
|
|
Actions
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{isLoading ? (
|
|
<TableRow>
|
|
<TableCell colSpan={8} align="center" className="py-10">
|
|
<CircularProgress size={28} />
|
|
</TableCell>
|
|
</TableRow>
|
|
) : rows.length === 0 ? (
|
|
<TableRow>
|
|
<TableCell colSpan={8} align="center" className="py-10">
|
|
<Typography
|
|
sx={{
|
|
color: "text.secondary",
|
|
}}
|
|
>
|
|
No locations found
|
|
</Typography>
|
|
</TableCell>
|
|
</TableRow>
|
|
) : (
|
|
rows.map((row) => (
|
|
<TableRow key={String(row.id)} hover>
|
|
<TableCell>
|
|
<Button
|
|
variant="text"
|
|
size="small"
|
|
className="font-semibold"
|
|
onClick={() => navigate(`/locations/edit/${row.id}`)}
|
|
>
|
|
{row.name || "—"}
|
|
</Button>
|
|
</TableCell>
|
|
<TableCell>{row.address || "—"}</TableCell>
|
|
<TableCell>{row.city || "—"}</TableCell>
|
|
<TableCell>{row.state || "—"}</TableCell>
|
|
<TableCell>{row.phone || "—"}</TableCell>
|
|
<TableCell>{row.contact || "—"}</TableCell>
|
|
<TableCell>{row.status || "—"}</TableCell>
|
|
<TableCell align="right">
|
|
<Tooltip title="Edit">
|
|
<IconButton
|
|
size="small"
|
|
aria-label="Edit location"
|
|
onClick={() => navigate(`/locations/edit/${row.id}`)}
|
|
>
|
|
<EditOutlinedIcon fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title="Delete">
|
|
<IconButton
|
|
size="small"
|
|
aria-label="Delete location"
|
|
onClick={() => setDeleteTarget(row)}
|
|
>
|
|
<DeleteOutlineIcon fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</TableCell>
|
|
</TableRow>
|
|
))
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
<TablePagination
|
|
component="div"
|
|
count={totalCount}
|
|
page={page}
|
|
onPageChange={(_event, nextPage) => setPage(nextPage)}
|
|
rowsPerPage={pageSize}
|
|
onRowsPerPageChange={(event) => {
|
|
setPageSize(Number(event.target.value));
|
|
setPage(0);
|
|
}}
|
|
rowsPerPageOptions={[12, 24, 48]}
|
|
/>
|
|
<Dialog open={Boolean(deleteTarget)} onClose={() => setDeleteTarget(null)}>
|
|
<DialogTitle>Delete Location</DialogTitle>
|
|
<DialogContent>
|
|
<DialogContentText>
|
|
Are you sure you want to delete "{deleteTarget?.name}"? This action cannot be
|
|
undone.
|
|
</DialogContentText>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={() => setDeleteTarget(null)} disabled={deleteLocation.isPending}>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
color="error"
|
|
variant="contained"
|
|
onClick={handleDeleteConfirm}
|
|
disabled={deleteLocation.isPending}
|
|
>
|
|
{deleteLocation.isPending ? "Deleting..." : "Delete"}
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
</Box>
|
|
);
|
|
}
|