-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add submission limit error handling and checks
- Loading branch information
Showing
3 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
frontend/src/features/admin-form/responses/AdminSubmissionsService.ts
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,26 @@ | ||
import { SubmissionCountQueryDto } from '~shared/types/submission' | ||
|
||
import { ApiService } from '~services/ApiService' | ||
|
||
import { ADMIN_FORM_ENDPOINT } from '../common/AdminViewFormService' | ||
|
||
/** | ||
* Counts the number of submissions for a given form | ||
* @param urlParameters Mapping of the url parameters to values | ||
* @returns The number of form submissions | ||
*/ | ||
export const countFormSubmissions = async ({ | ||
formId, | ||
dates, | ||
}: { | ||
formId: string | ||
dates?: SubmissionCountQueryDto | ||
}): Promise<number> => { | ||
const queryUrl = `${ADMIN_FORM_ENDPOINT}/${formId}/submissions/count` | ||
if (dates) { | ||
return ApiService.get(queryUrl, { | ||
params: { ...dates }, | ||
}).then(({ data }) => data) | ||
} | ||
return ApiService.get(queryUrl).then(({ data }) => data) | ||
} |
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,23 @@ | ||
import { useQuery, UseQueryResult } from 'react-query' | ||
import { useParams } from 'react-router-dom' | ||
|
||
import { adminFormKeys } from '../common/queries' | ||
|
||
import { countFormSubmissions } from './AdminSubmissionsService' | ||
|
||
export const adminFormResponsesKeys = { | ||
base: [...adminFormKeys.base, 'responses'] as const, | ||
id: (id: string) => [...adminFormResponsesKeys.base, id] as const, | ||
} | ||
|
||
/** | ||
* @precondition Must be wrapped in a Router as `useParam` is used. | ||
*/ | ||
export const useFormResponsesCount = (): UseQueryResult<number> => { | ||
const { formId } = useParams<{ formId: string }>() | ||
return useQuery( | ||
adminFormResponsesKeys.id(formId), | ||
() => countFormSubmissions({ formId }), | ||
{ staleTime: 10 * 60 * 1000 }, | ||
) | ||
} |
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