diff --git a/src/donations/DonationsPage.tsx b/src/donations/DonationsPage.tsx index da885287..263fe67c 100644 --- a/src/donations/DonationsPage.tsx +++ b/src/donations/DonationsPage.tsx @@ -8,6 +8,8 @@ import { Grid, Icon, Table, TableBody, TableCell, TableRow, TableHead, Paper } f export const DonationsPage = () => { const [editBatchId, setEditBatchId] = React.useState("notset"); const [batches, setBatches] = React.useState(null); + const [sortDirection, setSortDirection] = React.useState(null); + const [currentSortedCol, setCurrentSortedCol] = React.useState(""); const isMounted = useMountedState(); const batchUpdated = () => { setEditBatchId("notset"); loadData(); } @@ -33,6 +35,45 @@ export const DonationsPage = () => { return result; } + const sortTable = (key: string, asc: boolean | null) => { + let sortedBatches; + if (asc === null) asc = false; + setCurrentSortedCol(key); + + sortedBatches = batches.sort(function(a: any, b: any) { + if (a[key] === null) return Infinity; + + if (key === "batchDate") { + if (typeof new Date(a[key]).getMonth === "function") { + return asc ? (new Date(a[key])?.getTime() - new Date(b[key])?.getTime()) : (new Date(b[key])?.getTime() - new Date(a[key])?.getTime()); + } + } + + const parsedNum = parseInt(a[key]); + if (!isNaN(parsedNum)) { return asc ? (a[key] - b[key]) : (b[key] - a[key]); } + + const valA = a[key].toUpperCase(); + const valB = b[key].toUpperCase(); + if (valA < valB) { + return asc ? 1 : -1; + } + if (valA > valB) { + return asc ? -1 : 1; + } + + return 0; + }); + setBatches(sortedBatches); + setSortDirection(!asc); + } + + const getSortArrows = (key: string) => ( +
+
+
+
+ ) + const getRows = () => { const result: JSX.Element[] = []; @@ -65,7 +106,21 @@ export const DonationsPage = () => { return rows; } - rows.push(NameDateDonationsTotalEdit); + rows.push( + + sortTable("name", sortDirection)}> + Name + {getSortArrows("name")} + + sortTable("batchDate", sortDirection)}> + Date + {getSortArrows("batchDate")} + + Donations + Total + Edit + + ); return rows; } diff --git a/src/people/components/PeopleSearchResults.tsx b/src/people/components/PeopleSearchResults.tsx index 63f56c6d..60d1dd41 100644 --- a/src/people/components/PeopleSearchResults.tsx +++ b/src/people/components/PeopleSearchResults.tsx @@ -70,7 +70,7 @@ export function PeopleSearchResults(props: Props) { if (a[key] === null) return Infinity; // if value is null push to the end of array if (typeof a[key].getMonth === "function") { - return asc ? (a[key].getTime() - b[key].getTime()) : (b[key].getTime() - a[key].getTime()); + return asc ? (a[key]?.getTime() - b[key]?.getTime()) : (b[key]?.getTime() - a[key]?.getTime()); } const parsedNum = parseInt(a[key]);