Skip to content

Commit

Permalink
break down Grid into smaller components
Browse files Browse the repository at this point in the history
  • Loading branch information
kristi-h committed Dec 20, 2024
1 parent 8761f07 commit 86504c9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 119 deletions.
125 changes: 6 additions & 119 deletions client/components/admin_dashboard/Grid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,10 @@ import {
GridToolbarContainer,
} from "@mui/x-data-grid";
import { Paper, Box, Tooltip } from "@mui/material";
import Button from "@mui/material/Button";
import SearchBar from "./SearchBar";
import StatusChange from "./StatusChange";
import { getColumns } from "./grid-components/columns";
import { updateStatusOnServer } from "@/actions/form";

const formatName = (name) => {
const [firstName, ...rest] = name.split(" ");
const lastName = rest.join(" ");
if (!lastName) {
return firstName;
}
return `${lastName}, ${firstName}`;
};

const formatTime = (hour) => {
return hour <= 12 ? `${hour}a` : `${hour - 12}p`;
};

const formatTimeRange = (timeRange) => {
return `${formatTime(timeRange?.preferredEarlyTime)} - ${formatTime(
timeRange?.preferredLateTime
)}`;
};

const formatDateCreated = (date) => {
return `${date.toLocaleDateString()} ${date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})}`;
};

const formatPhone = (phone) => {
const cleaned = ("" + phone).replace(/\D/g, "");
const match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
return match ? `(${match[1]}) ${match[2]}-${match[3]}` : phone;
};

const paginationModel = { page: 0, pageSize: 15 };

const Toolbar = () => (
Expand All @@ -55,96 +22,16 @@ const Toolbar = () => (
export default function Grid({ rows, refreshData }) {
const [searchText, setSearchText] = useState("");

const columns = [
{
field: "visitOrder",
headerName: "Visit Order",
width: 190,
valueGetter: (_, row) => row.schedule.order,
},
{
field: "markVisited",
headerName: "Visited?",
width: 190,
renderCell: (params) => {
const { id, status } = params.row;

return (
<Button
variant={
status === "Visited" || status === "Completed"
? "contained"
: "outlined"
}
color="primary"
onClick={() =>
toggleVisited(
id,
status === "Visited" || status === "Completed"
? "Scheduled"
: "Visited"
)
}
>
{status === "Visited" || status === "Completed"
? "Visited"
: "Not Visited"}
</Button>
);
},
},
{
field: "status",
headerName: "Status",
width: 190,
renderCell: (params) => {
const { id, status } = params.row;
return (
<StatusChange
id={id}
refreshData={refreshData}
currentStatus={status}
/>
);
},
},
{
valueFormatter: formatName,
field: "name",
headerName: "Name",
width: 190,
},
{
valueFormatter: formatDateCreated,
field: "dateCreated",
headerName: "Requested on",
width: 190,
},
{
valueFormatter: formatTimeRange,
field: "timeRange",
headerName: "Timeslot",
width: 190,
},
{
valueFormatter: formatPhone,
field: "phone",
headerName: "Phone",
width: 190,
},
{ field: "email", headerName: "Email", width: 190 },
{
field: "address",
headerName: "Address",
width: 190,
},
];

const toggleVisited = async (id, status) => {
await updateStatusOnServer(id, status);
await refreshData();
};

const columns = useMemo(
() => getColumns(refreshData, toggleVisited),
[refreshData]
);

const filteredRows = useMemo(() => {
if (!searchText) return rows;

Expand Down
17 changes: 17 additions & 0 deletions client/components/admin_dashboard/grid-components/VisitedChip.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Button } from "@mui/material";

const VisitedChip = ({ id, status, toggleVisited }) => {
const visited = status === "Visited" || status === "Completed";

return (
<Button
variant={visited ? "contained" : "outlined"}
color="primary"
onClick={() => toggleVisited(id, visited ? "Scheduled" : "Completed")}
>
{visited ? "Visited" : "Not Visited"}
</Button>
);
};

export default VisitedChip;
74 changes: 74 additions & 0 deletions client/components/admin_dashboard/grid-components/columns.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import VisitedChip from "./VisitedChip";
import StatusChange from "./StatusChange";
import {
formatName,
formatDateCreated,
formatTimeRange,
formatPhone,
} from "./formatters";

export const getColumns = (refreshData, toggleVisited) => [
{
field: "visitOrder",
headerName: "Visit Order",
width: 190,
valueGetter: (_, row) => row.schedule.order,
},
{
field: "markVisited",
headerName: "Visited?",
width: 190,
renderCell: (params) => (
<VisitedChip
id={params.row.id}
status={params.row.status}
toggleVisited={toggleVisited}
/>
),
},
{
field: "status",
headerName: "Status",
width: 190,
renderCell: (params) => {
const { id, status } = params.row;
return (
<StatusChange
id={id}
refreshData={refreshData}
currentStatus={status}
/>
);
},
},
{
valueFormatter: formatName,
field: "name",
headerName: "Name",
width: 190,
},
{
valueFormatter: formatDateCreated,
field: "dateCreated",
headerName: "Requested on",
width: 190,
},
{
valueFormatter: formatTimeRange,
field: "timeRange",
headerName: "Timeslot",
width: 190,
},
{
valueFormatter: formatPhone,
field: "phone",
headerName: "Phone",
width: 190,
},
{ field: "email", headerName: "Email", width: 190 },
{
field: "address",
headerName: "Address",
width: 190,
},
];
31 changes: 31 additions & 0 deletions client/components/admin_dashboard/grid-components/formatters.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const formatName = (name) => {
const [firstName, ...rest] = name.split(" ");
const lastName = rest.join(" ");
if (!lastName) {
return firstName;
}
return `${lastName}, ${firstName}`;
};

export const formatTime = (hour) => {
return hour <= 12 ? `${hour}a` : `${hour - 12}p`;
};

export const formatTimeRange = (timeRange) => {
return `${formatTime(timeRange?.preferredEarlyTime)} - ${formatTime(
timeRange?.preferredLateTime
)}`;
};

export const formatDateCreated = (date) => {
return `${date.toLocaleDateString()} ${date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})}`;
};

export const formatPhone = (phone) => {
const cleaned = ("" + phone).replace(/\D/g, "");
const match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
return match ? `(${match[1]}) ${match[2]}-${match[3]}` : phone;
};

0 comments on commit 86504c9

Please sign in to comment.