mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 12:22:03 +00:00
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 { AccountListItem } from "@/domain/accounts/types/account";
|
|
import { useAccountsList } from "@/domain/accounts/use-cases/use-accounts-list";
|
|
import { useDeleteAccount } from "@/domain/accounts/use-cases/use-delete-account";
|
|
import { useDebounce } from "@/hooks/use-debounce";
|
|
|
|
export default function AccountsListPage() {
|
|
const navigate = useNavigate();
|
|
const [search, setSearch] = useState("");
|
|
const [page, setPage] = useState(0);
|
|
const [pageSize, setPageSize] = useState(12);
|
|
const [deleteTarget, setDeleteTarget] = useState<AccountListItem | null>(null);
|
|
|
|
const debouncedSearch = useDebounce(search, 300);
|
|
const deleteAccount = useDeleteAccount();
|
|
|
|
const listParams = useMemo(
|
|
() => ({
|
|
search: debouncedSearch,
|
|
page: page + 1,
|
|
pageSize,
|
|
}),
|
|
[debouncedSearch, page, pageSize],
|
|
);
|
|
|
|
const { data, isLoading, isFetching, error } = useAccountsList(listParams);
|
|
|
|
const rows = data?.items ?? [];
|
|
const totalCount = data?.totalCount ?? 0;
|
|
|
|
const handleDeleteConfirm = () => {
|
|
if (!deleteTarget) {
|
|
return;
|
|
}
|
|
|
|
deleteAccount.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">
|
|
Accounts
|
|
</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 accounts..."
|
|
className="min-w-[220px]"
|
|
/>
|
|
<Button variant="contained" onClick={() => navigate("/accounts/new")}>
|
|
+ New Account
|
|
</Button>
|
|
</Stack>
|
|
</Stack>
|
|
{Boolean(error) && (
|
|
<Alert severity="error">
|
|
{error instanceof Error ? error.message : "Failed to load accounts"}
|
|
</Alert>
|
|
)}
|
|
<TableContainer component={Paper} variant="outlined">
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell className="font-semibold">Name</TableCell>
|
|
<TableCell className="font-semibold">Number</TableCell>
|
|
<TableCell className="font-semibold">Industry</TableCell>
|
|
<TableCell className="font-semibold">Website</TableCell>
|
|
<TableCell className="font-semibold">Primary Contact</TableCell>
|
|
<TableCell className="font-semibold">Phone</TableCell>
|
|
<TableCell className="font-semibold">Owner</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 accounts found
|
|
</Typography>
|
|
</TableCell>
|
|
</TableRow>
|
|
) : (
|
|
rows.map((row) => (
|
|
<TableRow key={String(row.id)} hover>
|
|
<TableCell>
|
|
<Button
|
|
variant="text"
|
|
size="small"
|
|
className="font-semibold"
|
|
onClick={() => navigate(`/accounts/edit/${row.id}`)}
|
|
>
|
|
{row.name || "—"}
|
|
</Button>
|
|
</TableCell>
|
|
<TableCell>{row.number || "—"}</TableCell>
|
|
<TableCell>{row.industry || "—"}</TableCell>
|
|
<TableCell>{row.website || "—"}</TableCell>
|
|
<TableCell>{row.primaryContact || "—"}</TableCell>
|
|
<TableCell>{row.phoneNumber || "—"}</TableCell>
|
|
<TableCell>{row.owner || "—"}</TableCell>
|
|
<TableCell align="right">
|
|
<Tooltip title="Edit">
|
|
<IconButton
|
|
size="small"
|
|
aria-label="Edit account"
|
|
onClick={() => navigate(`/accounts/edit/${row.id}`)}
|
|
>
|
|
<EditOutlinedIcon fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title="Delete">
|
|
<IconButton
|
|
size="small"
|
|
aria-label="Delete account"
|
|
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 Account</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={deleteAccount.isPending}>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
color="error"
|
|
variant="contained"
|
|
onClick={handleDeleteConfirm}
|
|
disabled={deleteAccount.isPending}
|
|
>
|
|
{deleteAccount.isPending ? "Deleting..." : "Delete"}
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
</Box>
|
|
);
|
|
}
|