Skip to content

Commit

Permalink
Refactor Gateway and Results components to streamline vote data retri…
Browse files Browse the repository at this point in the history
…eval and improve variable usage
  • Loading branch information
JohanGrims committed Dec 16, 2024
1 parent 6ca774b commit 18d583a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/Gateway.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default function Gateway() {
}

Gateway.loader = async function loader({ params }) {
const vote = await getDoc(doc(db, `/votes/${params.id}`));
const { id } = params;
const vote = await getDoc(doc(db, `/votes/${id}`));
if (!vote.exists()) {
new Response("Not Found", {
status: 404,
Expand All @@ -25,17 +26,17 @@ Gateway.loader = async function loader({ params }) {
moment.unix(voteData.endTime.seconds).tz("Europe/Berlin")
)
) {
return redirect(`/r/${voteData.id}`);
return redirect(`/r/${id}`);
}
if (
berlinTime.isBefore(
moment.unix(voteData.startTime.seconds).tz("Europe/Berlin")
)
) {
return redirect(`/s/${voteData.id}`);
return redirect(`/s/${id}`);
}
if (localStorage.getItem(voteData.id)) {
return redirect(`/x/${voteData.id}`);
if (localStorage.getItem(id)) {
return redirect(`/x/${id}`);
}
return redirect(`/v/${voteData.id}`);
return redirect(`/v/${id}`);
};
5 changes: 3 additions & 2 deletions src/admin/vote/Results.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ export default function Results() {

Results.loader = async function loader({ params }) {
const { id } = params;
const vote = (await getDoc(doc(db, `/votes/${id}`))).data();
const vote = await getDoc(doc(db, `/votes/${id}`));
const voteData = { id: vote.id, ...vote.data() };
const options = (
await getDocs(collection(db, `/votes/${id}/options`))
).docs.map((doc) => {
Expand All @@ -523,7 +524,7 @@ Results.loader = async function loader({ params }) {
});

return {
vote,
vote: voteData,
options,
results,
choices,
Expand Down

0 comments on commit 18d583a

Please sign in to comment.