diff --git a/client/src/App.tsx b/client/src/App.tsx index 4ad07a2..837228d 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -25,6 +25,7 @@ import ResetPasswordPage from "./pages/ResetPasswordPage"; import ForgotPasswordPage from "./pages/ForgotPasswordPage"; import RestrictedLayout from "./layouts/restricted"; import Ranking from "./pages/Ranking"; +import ManageSchedulePage from "./pages/ManageSchedulePage"; function App() { return ( @@ -44,7 +45,7 @@ function App() { } /> - } path="event/:id"/> + } path="event/:id" /> {/* Karang Guni Schedules Route */} @@ -94,6 +95,10 @@ function App() { } /> + + + } /> + diff --git a/client/src/components/AdministratorNavigationPanel.tsx b/client/src/components/AdministratorNavigationPanel.tsx index 0d7123c..0dc8e9a 100644 --- a/client/src/components/AdministratorNavigationPanel.tsx +++ b/client/src/components/AdministratorNavigationPanel.tsx @@ -178,7 +178,7 @@ export default function AdministratorNavigationPanel() { } - onClickRef="#" + onClickRef="schedules" /> ([]); + const [scheduleIdToDelete, setScheduleIdToDelete] = useState(null); + const { isOpen, onOpen, onOpenChange } = useDisclosure(); + const [sortDescriptor, setSortDescriptor] = useState({ + column: 'dateTime', + direction: 'ascending', + }); + + useEffect(() => { + instance.get('/schedule') + .then((res) => { + const schedules = res.data.map((schedule: Schedule) => ({ + ...schedule, + dateTime: new Date(schedule.dateTime), // Convert to Date object + })); + setScheduleList(schedules); + }) + .catch((err) => { + console.error('Error fetching schedules:', err); + }); + }, []); + + const deleteSchedule = () => { + if (scheduleIdToDelete !== null) { + instance.delete(`/schedule/${scheduleIdToDelete}`) + .then((res) => { + console.log(res.data); + setScheduleList((prev) => prev.filter(schedule => schedule.id !== scheduleIdToDelete)); + onOpenChange(); + setScheduleIdToDelete(null); + }) + .catch((err) => { + console.error('Error deleting schedule:', err); + }); + } + }; + + const sortScheduleList = (list: Schedule[], descriptor: SortDescriptor) => { + const { column, direction } = descriptor; + + const sortedList = [...list].sort((a, b) => { + switch (column) { + case 'dateTime': + const dateA = new Date(a.dateTime); + const dateB = new Date(b.dateTime); + return direction === 'ascending' ? dateA.getTime() - dateB.getTime() : dateB.getTime() - dateA.getTime(); + case 'location': + return direction === 'ascending' ? a.location.localeCompare(b.location) : b.location.localeCompare(a.location); + case 'postalCode': + return direction === 'ascending' ? a.postalCode.localeCompare(b.postalCode) : b.postalCode.localeCompare(a.postalCode); + case 'status': + return direction === 'ascending' ? a.status.localeCompare(b.status) : b.status.localeCompare(a.status); + default: + throw new Error(`Unsupported column: ${column}`); + } + }); + + return sortedList; + }; + + const handleSort = () => { + const { column, direction } = sortDescriptor; + const newDirection = direction === 'ascending' ? 'descending' : 'ascending'; + + setSortDescriptor({ column, direction: newDirection }); + }; + + const renderSortIndicator = () => { + if (sortDescriptor.direction === 'ascending') { + return ↑; + } else { + return ↓; + } + }; + + const sortedScheduleList = sortScheduleList(scheduleList, sortDescriptor); + + return ( + + + Admin Karang Guni Schedule + + + + + + + + + + + Date{renderSortIndicator()} + + Time + Location + Postal Code + Status + Action + + + {sortedScheduleList.map((schedule) => ( + + {((schedule.dateTime as unknown) as Date).toLocaleDateString()} + {((schedule.dateTime as unknown) as Date).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} + {schedule.location} + {schedule.postalCode} + {schedule.status} + + + { setScheduleIdToDelete(schedule.id); onOpen(); }}> + + + ))} + + + + + + {(onClose) => ( + <> + Delete Schedule + + Are you sure you want to delete this schedule? + + + + Close + + { deleteSchedule(); onClose(); }}> + Delete + + + > + )} + + + + ) +} + diff --git a/client/src/pages/Ranking.tsx b/client/src/pages/Ranking.tsx index 7df7fef..79c2c2e 100644 --- a/client/src/pages/Ranking.tsx +++ b/client/src/pages/Ranking.tsx @@ -73,19 +73,18 @@ export default function Ranking() { return ( - Form Data + Form Data - - Average Bill {renderSortIndicator()} - - Form ID User ID Electrical Bill Water Bill Total Bill Number of Dependents + + Average Bill {renderSortIndicator()} + Electrical Bill Picture Water Bill Picture Actions @@ -93,13 +92,12 @@ export default function Ranking() { {sortedFormData.map((data) => ( - ${data.avgBill.toFixed(2)} - {data.id} {data.userId} ${data.electricalBill.toFixed(2)} ${data.waterBill.toFixed(2)} ${data.totalBill.toFixed(2)} {data.noOfDependents} + ${data.avgBill.toFixed(2)} {data.ebPicture && }
Admin Karang Guni Schedule
Are you sure you want to delete this schedule?
Form Data