Skip to content

Commit

Permalink
add possibility of clearing the api error message (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
devgioele authored Aug 10, 2023
1 parent 875c18f commit 2c0abad
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/components/react-hook-form/useHandleSubmit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FieldValues, Path, UseFormReturn } from 'react-hook-form'
import { useCallback } from 'react'
import {
joinFieldErrorMessages,
useHandleRequest,
Expand All @@ -11,7 +12,7 @@ const DEFAULT_ERROR_FIELD_PATH = 'apiError'

export type UseHandleSubmitReturn<V, R> = Pick<
UseHandleRequestReturn<V, R>,
'apiErrorMessage'
'apiErrorMessage' | 'clearApiErrorMessage'
> & {
isSubmitting: boolean
triggerSubmit: UseHandleRequestTrigger<V, R>
Expand Down Expand Up @@ -39,11 +40,12 @@ export function useHandleSubmit<

options?: UseHandleRequestOptions<TActualFieldValues, Response, Error>,
): UseHandleSubmitReturn<TActualFieldValues, Response> {
const { apiErrorMessage, isRequesting, triggerRequest } = useHandleRequest<
TActualFieldValues,
Response,
Error
>(
const {
apiErrorMessage,
clearApiErrorMessage,
isRequesting,
triggerRequest,
} = useHandleRequest<TActualFieldValues, Response, Error>(
async (values) => {
clearErrors(DEFAULT_ERROR_FIELD_PATH as Path<TFieldValues>)
return await submitAction(values)
Expand All @@ -69,8 +71,14 @@ export function useHandleSubmit<
},
)

const clearError = useCallback(() => {
clearErrors(DEFAULT_ERROR_FIELD_PATH as Path<TFieldValues>)
clearApiErrorMessage()
}, [clearApiErrorMessage, clearErrors])

return {
apiErrorMessage,
clearApiErrorMessage: clearError,
isSubmitting: isRequesting,
triggerSubmit: triggerRequest,
}
Expand Down
17 changes: 16 additions & 1 deletion src/components/util/__test__/useHandleRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('useHandleRequest', () => {

await act(() => result.current.triggerRequest())

expect(result.current.apiErrorMessage).toBe('Server Error')
expect(result.current.apiErrorMessage).toBe(expectedErrorMessage)

act(() => {
void result.current.triggerRequest()
Expand Down Expand Up @@ -115,4 +115,19 @@ describe('useHandleRequest', () => {

expect(result.current.apiErrorMessage).toBe(defaultMessages['error.api'])
})

test('should correctly clear the api error message', async () => {
const { result } = renderHook(() =>
useHandleRequest(onRequestWithErrorResponse, { onSuccess }),
)
expect(result.current.apiErrorMessage).toBeUndefined()

await act(() => result.current.triggerRequest())
expect(result.current.apiErrorMessage).toBe(expectedErrorMessage)

act(() => {
result.current.clearApiErrorMessage()
})
expect(result.current.apiErrorMessage).toBeUndefined()
})
})
6 changes: 6 additions & 0 deletions src/components/util/useHandleRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type UseHandleRequestReturn<V, R> = {
apiErrorMessage: string | undefined
isRequesting: boolean
triggerRequest: UseHandleRequestTrigger<V, R>
clearApiErrorMessage: () => void
}

export function useHandleRequest<
Expand All @@ -52,6 +53,10 @@ export function useHandleRequest<
const { messages } = useInternationalization()
const isMounted = useIsMounted()

const clearApiErrorMessage = useCallback(() => {
setApiErrorMessage(undefined)
}, [])

const triggerRequest: UseHandleRequestTrigger<Values, Response> = useCallback(
async (values) => {
try {
Expand Down Expand Up @@ -92,6 +97,7 @@ export function useHandleRequest<

return {
apiErrorMessage,
clearApiErrorMessage,
isRequesting,
triggerRequest,
}
Expand Down

0 comments on commit 2c0abad

Please sign in to comment.