From 47539a1897c3f2db81c00db14a804e588cc9cdff Mon Sep 17 00:00:00 2001 From: JohanGrims Date: Fri, 8 Nov 2024 22:00:27 +0100 Subject: [PATCH] refactor(votes): sort votes by start time and include start/end times in state management --- src/App.jsx | 86 +++++++++++++-------- src/admin/Overview.jsx | 52 +++++++------ src/admin/navigation/DrawerList.jsx | 114 ++++++++++++++++++---------- 3 files changed, 155 insertions(+), 97 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index f288602..de41488 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -28,16 +28,20 @@ function App() { {activeVotes.length < 1 && ( Keine Wahlen )} - {activeVotes.map((vote) => ( - - {vote.title} - - ))} + {activeVotes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((vote) => ( + + {vote.title} + + ))} Keine Wahlen )} - {scheduledVotes.map((e) => ( - - {e.title} - - ))} + {scheduledVotes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((e) => ( + + {e.title} + + ))} @@ -80,15 +88,19 @@ function App() { {expiredVotes.length === 0 && ( Keine Wahlen )} - {expiredVotes.map((e) => ( - - {e.title} - - ))} + {expiredVotes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((e) => ( + + {e.title} + + ))} @@ -116,13 +128,25 @@ export async function loader() { if (now.isAfter(startTime)) { if (data.active && now.isBefore(endTime)) { - activeVotes.push({ id: e.id, title: data.title }); + activeVotes.push({ + id: e.id, + title: data.title, + startTime: data.startTime, + }); } else { - expiredVotes.push({ id: e.id, title: data.title }); + expiredVotes.push({ + id: e.id, + title: data.title, + startTime: data.startTime, + }); } return; } - scheduledVotes.push({ id: e.id, title: data.title }); + scheduledVotes.push({ + id: e.id, + title: data.title, + startTime: data.startTime, + }); }); return { activeVotes, expiredVotes, scheduledVotes }; diff --git a/src/admin/Overview.jsx b/src/admin/Overview.jsx index 234623e..5957fab 100644 --- a/src/admin/Overview.jsx +++ b/src/admin/Overview.jsx @@ -43,31 +43,35 @@ export default function Overview() { gap: "20px", }} > - {votes.map((vote) => { - const now = moment(); - const startTime = moment.unix(vote.startTime.seconds); - const endTime = moment.unix(vote.endTime.seconds); - const isActive = - vote.active && endTime.isAfter(now) && startTime.isBefore(now); + {votes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((vote) => { + const now = moment(); + const startTime = moment.unix(vote.startTime.seconds); + const endTime = moment.unix(vote.endTime.seconds); + const isActive = + vote.active && endTime.isAfter(now) && startTime.isBefore(now); - return ( - navigate(`/admin/${vote.id}`)} - > -

{vote.title}

-

- -

-
- ); - })} + return ( + navigate(`/admin/${vote.id}`)} + > +

{vote.title}

+

+ +

+
+ ); + })} Date.now()) { setScheduledVotes((scheduledVotes) => [ ...scheduledVotes, - { id: e.id, title: data.title, version: data.version }, + { + id: e.id, + title: data.title, + version: data.version, + startTime: data.startTime, + endTime: data.endTime, + }, ]); } else { setActiveVotes((activeVotes) => [ ...activeVotes, - { id: e.id, title: data.title, version: data.version }, + { + id: e.id, + title: data.title, + version: data.version, + startTime: data.startTime, + endTime: data.endTime, + }, ]); } } else { setExpiredVotes((expiredVotes) => [ ...expiredVotes, - { id: e.id, title: data.title, version: data.version }, + { + id: e.id, + title: data.title, + version: data.version, + startTime: data.startTime, + endTime: data.endTime, + }, ]); } }); @@ -124,19 +142,23 @@ export default function DrawerList() { {activeVotes.length === 0 && ( Keine Wahlen )} - {activeVotes.map((e) => ( - - navigate(`/admin/${e.id}`)} - /> - - ))} + {activeVotes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((e) => ( + + navigate(`/admin/${e.id}`)} + /> + + ))} )} @@ -159,19 +181,23 @@ export default function DrawerList() { {scheduledVotes.length === 0 && ( Keine Wahlen )} - {scheduledVotes.map((e) => ( - - navigate(`/admin/${e.id}`)} - /> - - ))} + {scheduledVotes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((e) => ( + + navigate(`/admin/${e.id}`)} + /> + + ))} )} @@ -194,19 +220,23 @@ export default function DrawerList() { {expiredVotes.length === 0 && ( Keine Wahlen )} - {expiredVotes.map((e) => ( - - navigate(`/admin/${e.id}`)} - /> - - ))} + {expiredVotes + .sort((a, b) => { + return b.startTime.seconds - a.startTime.seconds; + }) + .map((e) => ( + + navigate(`/admin/${e.id}`)} + /> + + ))} )}