generated from chingu-voyages/voyage-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break down Grid into smaller components
- Loading branch information
Showing
5 changed files
with
128 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
client/components/admin_dashboard/grid-components/VisitedChip.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
client/components/admin_dashboard/grid-components/columns.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
client/components/admin_dashboard/grid-components/formatters.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |