diff --git a/frontend/src/components/Election/Election.tsx b/frontend/src/components/Election/Election.tsx
index def70963..6027c0e4 100644
--- a/frontend/src/components/Election/Election.tsx
+++ b/frontend/src/components/Election/Election.tsx
@@ -1,4 +1,3 @@
-import { useEffect } from "react"
import { useParams } from "react-router";
import React from 'react'
import ElectionHome from "./ElectionHome";
@@ -11,43 +10,45 @@ import Sidebar from "./Sidebar";
import { Grid } from "@mui/material";
import Thanks from "./Voting/Thanks";
import ViewBallot from "./Admin/ViewBallot";
-import { IAuthSession } from "../../hooks/useAuthSession";
-import { useGetElection } from "../../hooks/useAPI";
+import { IAuthSession } from "../../hooks/useAuthSession"
+import useElection, { ElectionContextProvider } from "../ElectionContextProvider";
type Props = {
authSession: IAuthSession,
}
+const TempWrapper = ({ authSession }: Props) => {
+ //Temporary wrapper to get election component inputs
+ //Once components are converted to use election context this can be removed
+
+ const { data, election, refreshElection, permissions, updateElection } = useElection()
+
+ return (
+
+
+
+
+
+
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+
+
+ )
+}
+
const Election = ({ authSession }: Props) => {
const { id } = useParams();
- const { data, isPending, error, makeRequest: fetchData } = useGetElection(id)
- useEffect(() => {
- fetchData()
- }, [])
-
return (
- <>
- {isPending &&
Loading Election...
}
- {data != null &&
-
-
-
-
-
-
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
-
-
-
- }
- >
+
+
+
)
}
diff --git a/frontend/src/components/ElectionContextProvider.tsx b/frontend/src/components/ElectionContextProvider.tsx
new file mode 100644
index 00000000..9e124b5d
--- /dev/null
+++ b/frontend/src/components/ElectionContextProvider.tsx
@@ -0,0 +1,60 @@
+import React, { useContext, useEffect } from 'react'
+import { createContext, Dispatch, SetStateAction } from 'react'
+import { Election } from '../../../domain_model/Election';
+import { useEditElection, useGetElection } from '../hooks/useAPI';
+import { Election as IElection } from '../../../domain_model/Election';
+import { VoterAuth } from '../../../domain_model/VoterAuth';
+import structuredClone from '@ungap/structured-clone';
+
+
+export interface IElectionContext {
+ data: {election: Election, voterAuth: VoterAuth}
+ election: Election;
+ refreshElection: Function;
+ updateElection: Function;
+ permissions: string[]
+}
+
+
+export const ElectionContext = createContext({
+ data: null,
+ election: null,
+ refreshElection: () => false,
+ updateElection: () => false,
+ permissions: []
+}
+)
+
+export const ElectionContextProvider = ({ id, children }) => {
+
+ const { data, isPending, error, makeRequest: fetchData } = useGetElection(id)
+ const { makeRequest: editElection } = useEditElection(id)
+
+ useEffect(() => {
+ fetchData()
+ }, [id])
+
+ const applyElectionUpdate = async (updateFunc: (election: IElection) => any) => {
+ if (!data.election) return
+ const electionCopy: IElection = structuredClone(data.election)
+ updateFunc(electionCopy)
+ return await editElection({ Election: electionCopy })
+ };
+
+
+ return (
+ {data && children}
+
+ )
+}
+
+export default function useElection() {
+ return useContext(ElectionContext);
+}
\ No newline at end of file