Skip to content

Commit

Permalink
Implement merge election (#177)
Browse files Browse the repository at this point in the history
It implements an inner state that if election data is provided along election id, it will fetch the election to update the election data.

Useful to give initial information to an election meanwhile awaiting is updated.
  • Loading branch information
selankon authored Jun 11, 2024
1 parent c12688a commit bc29fa3
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/react-providers/src/election/use-election-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const useElectionProvider = ({
}: ElectionProviderProps) => {
const { client: c, localize } = useClient()
const { state, actions } = useElectionReducer(c, data)

// If id and election data are both provided, it will fetch election info with the updated election.
const [mergeElection, setMergeElection] = useState<boolean>(!!id && !!data)

const {
client,
csp,
Expand Down Expand Up @@ -168,6 +172,14 @@ export const useElectionProvider = ({
fetchElection(id)
}, [state.id, id, client, loading.election, loaded.election, fetchElection])

// fetch election whenever the election id changes. This is useful if election data is provided,
// and you want to fetch the latest data anyway
useEffect(() => {
if (!id || !mergeElection || !client || loading.election) return

fetchElection(id).finally(() => setMergeElection(false))
}, [client, data, election, fetchElection, id, loading.election, mergeElection])

// check census information
useEffect(() => {
if (!fetchCensus || !election || !loaded.election || loading.census || !client.wallet) return
Expand Down

0 comments on commit bc29fa3

Please sign in to comment.