-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
324 additions
and
46 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as React from "react"; | ||
import { StrictMode } from "react"; | ||
import { ApiProvider } from "@kiesraad/api"; | ||
|
||
export const Providers = ({ children }: { children: React.ReactNode }) => { | ||
return ( | ||
<StrictMode> | ||
<ApiProvider host="http://testhost">{children}</ApiProvider> | ||
</StrictMode> | ||
); | ||
}; |
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,10 @@ | ||
import { ReactElement } from "react"; | ||
import { Providers } from "./Providers"; | ||
|
||
import { render, RenderOptions } from "@testing-library/react"; | ||
|
||
const customRender = (ui: ReactElement, options?: Omit<RenderOptions, "wrapper">) => | ||
render(ui, { wrapper: Providers, ...options }); | ||
|
||
export * from "@testing-library/react"; | ||
export { customRender as render }; |
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,20 @@ | ||
export class ApiClient { | ||
host: string; | ||
|
||
constructor(host: string) { | ||
this.host = host; | ||
} | ||
|
||
async postRequest(path: string, requestBody: object): Promise<Response> { | ||
const host = process.env.NODE_ENV === "test" ? "http://testhost" : ""; | ||
const response = await fetch(host + "/v1" + path, { | ||
method: "POST", | ||
body: JSON.stringify(requestBody), | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}); | ||
|
||
return response; | ||
} | ||
} |
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,18 @@ | ||
import * as React from "react"; | ||
import { ApiClient } from "./ApiClient"; | ||
|
||
export interface iApiProviderContext { | ||
client: ApiClient; | ||
} | ||
|
||
export const ApiProviderContext = React.createContext<iApiProviderContext | null>(null); | ||
|
||
export interface ApiProviderProps { | ||
host: string; | ||
children: React.ReactNode; | ||
} | ||
export function ApiProvider({ children, host }: ApiProviderProps) { | ||
const client = React.useMemo(() => new ApiClient(host), [host]); | ||
|
||
return <ApiProviderContext.Provider value={{ client }}>{children}</ApiProviderContext.Provider>; | ||
} |
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,18 @@ | ||
export interface ApiResponse<DATA = object> { | ||
status: string; | ||
code: number; | ||
message?: string; | ||
data?: DATA; | ||
} | ||
|
||
export interface ApiResponseSuccess<DATA = object> extends ApiResponse<DATA> { | ||
status: "20x"; | ||
} | ||
|
||
export interface ApiResponseClientError<DATA = object> extends ApiResponse<DATA> { | ||
status: "40x"; | ||
} | ||
|
||
export interface ApiResponseServerError<DATA = object> extends ApiResponse<DATA> { | ||
status: "50x"; | ||
} |
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,5 @@ | ||
export * from "./ApiProvider"; | ||
export * from "./ApiClient"; | ||
export * from "./useApiRequest"; | ||
export * from "./usePollingStationDataEntry"; | ||
export * from "./gen/openapi"; |
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,11 @@ | ||
import * as React from "react"; | ||
|
||
import { ApiProviderContext, iApiProviderContext } from "./ApiProvider"; | ||
|
||
export function useApi() { | ||
const context = React.useContext<iApiProviderContext | null>(ApiProviderContext); | ||
if (context === null) { | ||
throw new Error("useApi must be used within an ApiProvider"); | ||
} | ||
return context; | ||
} |
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,62 @@ | ||
import * as React from "react"; | ||
|
||
import { useApi } from "./useApi"; | ||
import { ApiResponse, ApiResponseSuccess } from "./api"; | ||
|
||
export type UseApiRequestReturn<REQUEST_BODY, DATA, ERROR> = [ | ||
(requestBody: REQUEST_BODY) => void, | ||
{ | ||
loading: boolean; | ||
error: ERROR | null; | ||
data: DATA | null; | ||
} | ||
]; | ||
|
||
export interface UseApiRequestParams<DATA, ERROR> { | ||
path: string; | ||
responseHandler: (response: Response) => Promise<DATA | ERROR>; | ||
} | ||
|
||
export function useApiRequest<REQUEST_BODY, DATA extends ApiResponseSuccess, ERROR extends ApiResponse>({ | ||
path, | ||
responseHandler | ||
}: UseApiRequestParams<DATA, ERROR>): UseApiRequestReturn<REQUEST_BODY, DATA, ERROR> { | ||
const { client } = useApi(); | ||
const [data, setData] = React.useState<DATA | null>(null); | ||
const [error, setError] = React.useState<ERROR | null>(null); | ||
const [apiRequest, setApiRequest] = React.useState<REQUEST_BODY | null>(null); | ||
|
||
React.useEffect(() => { | ||
const doRequest = async (b: REQUEST_BODY) => { | ||
const response = await client.postRequest(path, b as object); | ||
|
||
const result = await responseHandler(response); | ||
if (result.status === "20x") { | ||
setData(result as DATA); | ||
} else { | ||
setError(result as ERROR); | ||
} | ||
}; | ||
|
||
if (apiRequest) { | ||
doRequest(apiRequest).catch((e: unknown) => { | ||
console.error(e); | ||
}); | ||
} | ||
}, [apiRequest, client, path, responseHandler]); | ||
|
||
const makeRequest = React.useCallback((requestBody: REQUEST_BODY) => { | ||
setApiRequest(requestBody); | ||
}, []); | ||
|
||
const loading = false; | ||
|
||
return [ | ||
makeRequest, | ||
{ | ||
loading, | ||
error, | ||
data | ||
} | ||
]; | ||
} |
Oops, something went wrong.