mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 16:19:01 +00:00
221 lines
7.1 KiB
TypeScript
221 lines
7.1 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 { ContactListItem } from "@/domain/contacts/types/contact";
|
|
import { useContactsList } from "@/domain/contacts/use-cases/use-contacts-list";
|
|
import { useDeleteContact } from "@/domain/contacts/use-cases/use-delete-contact";
|
|
import { useDebounce } from "@/hooks/use-debounce";
|
|
|
|
export default function ContactsListPage() {
|
|
const navigate = useNavigate();
|
|
const [search, setSearch] = useState("");
|
|
const [page, setPage] = useState(0);
|
|
const [pageSize, setPageSize] = useState(12);
|
|
const [deleteTarget, setDeleteTarget] = useState<ContactListItem | null>(null);
|
|
|
|
const debouncedSearch = useDebounce(search, 300);
|
|
const deleteContact = useDeleteContact();
|
|
|
|
const listParams = useMemo(
|
|
() => ({ search: debouncedSearch, page: page + 1, pageSize }),
|
|
[debouncedSearch, page, pageSize],
|
|
);
|
|
|
|
const { data, isLoading, isFetching, error } = useContactsList(listParams);
|
|
const rows = data?.items ?? [];
|
|
const totalCount = data?.totalCount ?? 0;
|
|
|
|
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">
|
|
Contacts
|
|
</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={(e) => {
|
|
setSearch(e.target.value);
|
|
setPage(0);
|
|
}}
|
|
placeholder="Search contacts..."
|
|
className="min-w-sidebar"
|
|
/>
|
|
<Button variant="contained" onClick={() => navigate("/contacts/new")}>
|
|
+ New Contact
|
|
</Button>
|
|
</Stack>
|
|
</Stack>
|
|
{Boolean(error) && (
|
|
<Alert severity="error">
|
|
{error instanceof Error ? error.message : "Failed to load contacts"}
|
|
</Alert>
|
|
)}
|
|
<TableContainer component={Paper} variant="outlined">
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell className="font-semibold">Name</TableCell>
|
|
<TableCell className="font-semibold">Account</TableCell>
|
|
<TableCell className="font-semibold">Title</TableCell>
|
|
<TableCell className="font-semibold">Email</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={7} align="center" className="py-10">
|
|
<CircularProgress size={28} />
|
|
</TableCell>
|
|
</TableRow>
|
|
) : rows.length === 0 ? (
|
|
<TableRow>
|
|
<TableCell colSpan={7} align="center" className="py-10">
|
|
<Typography
|
|
sx={{
|
|
color: "text.secondary",
|
|
}}
|
|
>
|
|
No contacts found
|
|
</Typography>
|
|
</TableCell>
|
|
</TableRow>
|
|
) : (
|
|
rows.map((row) => (
|
|
<TableRow key={String(row.id)} hover>
|
|
<TableCell>
|
|
<Button
|
|
variant="text"
|
|
size="small"
|
|
className="font-semibold"
|
|
onClick={() => navigate(`/contacts/edit/${row.id}`)}
|
|
>
|
|
{row.name || "—"}
|
|
</Button>
|
|
</TableCell>
|
|
<TableCell>{row.account || "—"}</TableCell>
|
|
<TableCell>{row.title || "—"}</TableCell>
|
|
<TableCell>{row.email || "—"}</TableCell>
|
|
<TableCell>{row.phoneNumber || "—"}</TableCell>
|
|
<TableCell>{row.owner || "—"}</TableCell>
|
|
<TableCell align="right">
|
|
<Tooltip title="Edit">
|
|
<IconButton size="small" onClick={() => navigate(`/contacts/edit/${row.id}`)}>
|
|
<EditOutlinedIcon fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title="Delete">
|
|
<IconButton size="small" onClick={() => setDeleteTarget(row)}>
|
|
<DeleteOutlineIcon fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</TableCell>
|
|
</TableRow>
|
|
))
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
<TablePagination
|
|
component="div"
|
|
count={totalCount}
|
|
page={page}
|
|
onPageChange={(_e, nextPage) => setPage(nextPage)}
|
|
rowsPerPage={pageSize}
|
|
onRowsPerPageChange={(e) => {
|
|
setPageSize(Number(e.target.value));
|
|
setPage(0);
|
|
}}
|
|
rowsPerPageOptions={[12, 24, 48]}
|
|
/>
|
|
<Dialog open={Boolean(deleteTarget)} onClose={() => setDeleteTarget(null)}>
|
|
<DialogTitle>Delete Contact</DialogTitle>
|
|
<DialogContent>
|
|
<DialogContentText>
|
|
Are you sure you want to delete "{deleteTarget?.name}"?
|
|
</DialogContentText>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={() => setDeleteTarget(null)} disabled={deleteContact.isPending}>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
color="error"
|
|
variant="contained"
|
|
disabled={deleteContact.isPending}
|
|
onClick={() => {
|
|
if (!deleteTarget) return;
|
|
deleteContact.mutate(deleteTarget.id, {
|
|
onSuccess: () => setDeleteTarget(null),
|
|
});
|
|
}}
|
|
>
|
|
Delete
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
</Box>
|
|
);
|
|
}
|