-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from dapetcu21/fixes-0.1.2
fixes-0.1.2
- Loading branch information
Showing
31 changed files
with
151 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/ElectionObservationSection/ElectionObservationSection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/ElectionResultsProcess/ElectionResultsProcess.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/ElectionResultsStackedBar/ElectionResultsStackedBar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/ElectionResultsSummaryTable/ElectionResultsSummaryTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/ElectionTurnoutBreakdownChart/ElectionTurnoutBreakdownChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useEffect, useState } from "react"; | ||
import { ElectionBallot, ElectionScope } from "../types/Election"; | ||
import { APIRequestState } from "../util/api"; | ||
import { ElectionAPI } from "../util/electionApi"; | ||
import { useApiResponse } from "./useApiResponse"; | ||
|
||
export const useBallotData = ( | ||
api: ElectionAPI, | ||
ballotId: number | null, | ||
scope: ElectionScope | null, | ||
autoRefreshInterval: number = 60 * 1000, | ||
): APIRequestState<ElectionBallot> => { | ||
const [timerToken, setTimerToken] = useState<boolean>(false); | ||
|
||
const state = useApiResponse(() => (ballotId != null && scope ? api.getBallot(ballotId, scope) : null), [ | ||
scope, | ||
ballotId, | ||
timerToken, | ||
]); | ||
|
||
// Changing the timerToken will re-trigger useApiResponse | ||
useEffect(() => { | ||
if (state.loading || !state.data?.meta.live) { | ||
return undefined; | ||
} | ||
|
||
const timerId = setTimeout(() => { | ||
setTimerToken((x) => !x); | ||
}, autoRefreshInterval); | ||
|
||
return () => { | ||
clearTimeout(timerId); | ||
}; | ||
}, [state.data, state.loading, timerToken, autoRefreshInterval]); | ||
return state; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useEffect, useState } from "react"; | ||
import { APIInvocation, APIRequestState } from "../util/api"; | ||
|
||
export type UseAPIResponseOptions<ResponseType> = { | ||
invocation?: APIInvocation<ResponseType> | null; | ||
discardPreviousData?: boolean; // Defaults to false | ||
discardDataOnError?: boolean; // Defaults to false | ||
abortPreviousRequest?: boolean; // Defaults to true | ||
}; | ||
|
||
export function useApiResponse<ResponseType>( | ||
makeInvocation: () => APIInvocation<ResponseType> | UseAPIResponseOptions<ResponseType> | undefined | void | null, | ||
dependencies: unknown[], | ||
): APIRequestState<ResponseType> { | ||
const [state, setState] = useState<APIRequestState<ResponseType>>({ | ||
data: null, | ||
hasData: false, | ||
loading: false, | ||
error: null, | ||
}); | ||
|
||
useEffect(() => { | ||
const inv = makeInvocation(); | ||
const options: UseAPIResponseOptions<ResponseType> = (typeof inv === "function" ? { invocation: inv } : inv) || {}; | ||
const { | ||
invocation, | ||
discardPreviousData = false, | ||
discardDataOnError = false, | ||
abortPreviousRequest = true, | ||
} = options; | ||
|
||
if (!invocation && !discardPreviousData) return; | ||
|
||
setState((prevState) => ({ | ||
data: discardPreviousData ? null : prevState.data, | ||
hasData: discardPreviousData ? false : prevState.hasData, | ||
loading: invocation != null, | ||
error: invocation != null || discardPreviousData ? null : prevState.error, | ||
})); | ||
|
||
if (!invocation) return; | ||
|
||
const abortController = new AbortController(); | ||
|
||
let mounted = true; | ||
invocation(abortController.signal).then( | ||
(data) => { | ||
if (!mounted) return; | ||
setState({ | ||
data, | ||
hasData: true, | ||
loading: false, | ||
error: null, | ||
}); | ||
}, | ||
(error) => { | ||
if (!mounted) return; | ||
setState((prevState) => ({ | ||
data: discardDataOnError ? null : prevState.data, | ||
hasData: discardDataOnError ? false : prevState.hasData, | ||
loading: false, | ||
error, | ||
})); | ||
}, | ||
); | ||
|
||
return () => { | ||
mounted = false; | ||
if (abortPreviousRequest) { | ||
abortController.abort(); | ||
} | ||
}; | ||
}, dependencies); | ||
|
||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7eaed1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: