Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch custom validators before deserializing #7580

Merged
merged 7 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

- Internally we were storing the `family` name field as a required property which was limiting what how you could capture the name of a person in the forms. Now we are storing it as an optional property which would make more flexible.

## Bug fixes

- Custom form field validators from country config will work offline. [#7478](https://github.com/opencrvs/opencrvs-core/issues/7478)

### Breaking changes

- **Gateways searchEvents API updated** `operationHistories` only returns `operationType` & `operatedOn` due to the other fields being unused in OpenCRVS
Expand Down
17 changes: 15 additions & 2 deletions packages/client/src/forms/register/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { LoopReducer, Loop } from 'redux-loop'
import { LoopReducer, Loop, Cmd, loop } from 'redux-loop'
import { IForm } from '@client/forms'
import * as offlineActions from '@client/offline/actions'
import { deserializeForm } from '@client/forms/deserializer/deserializer'
import { validators } from '@client/forms/validators'
import { initValidators, validators } from '@client/forms/validators'

export type IRegisterFormState =
| {
Expand Down Expand Up @@ -46,6 +46,19 @@ export const registerFormReducer: LoopReducer<IRegisterFormState, Action> = (
switch (action.type) {
case offlineActions.READY:
case offlineActions.FORMS_LOADED:
return loop(
state,
Cmd.run(
async () => {
await initValidators()
return action.payload
},
{
successActionCreator: offlineActions.CustomValidatorsSuccess
}
)
)
case offlineActions.CUSTOM_VALIDATORS_LOADED:
const { forms } = action.payload

const birth = deserializeForm(forms.birth, validators)
Expand Down
17 changes: 15 additions & 2 deletions packages/client/src/forms/register/reviewReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { LoopReducer, Loop } from 'redux-loop'
import { LoopReducer, Loop, loop, Cmd } from 'redux-loop'
import { IForm } from '@client/forms'
import * as offlineActions from '@client/offline/actions'
import { deserializeForm } from '@client/forms/deserializer/deserializer'
import { validators } from '@client/forms/validators'
import { initValidators, validators } from '@client/forms/validators'

export type IReviewFormState =
| {
Expand Down Expand Up @@ -46,6 +46,19 @@ export const reviewReducer: LoopReducer<IReviewFormState, Action> = (
switch (action.type) {
case offlineActions.READY:
case offlineActions.FORMS_LOADED:
return loop(
state,
Cmd.run(
async () => {
await initValidators()
return action.payload
},
{
successActionCreator: offlineActions.CustomValidatorsSuccess
}
)
)
case offlineActions.CUSTOM_VALIDATORS_LOADED:
const { forms } = action.payload

const birth = deserializeForm(forms.birth, validators)
Expand Down
15 changes: 15 additions & 0 deletions packages/client/src/offline/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export type FormsLoadedAction = {
payload: LoadFormsResponse
}

export const CUSTOM_VALIDATORS_LOADED = 'OFFLINE/CUSTOM_VALIDATORS_LOADED'
export type CustomValidatorsLoadedLoadedAction = {
type: typeof CUSTOM_VALIDATORS_LOADED
payload: LoadFormsResponse
}

export const FORMS_FAILED = 'OFFLINE/FORMS_FAILED'
export type FormsFailedAction = {
type: typeof FORMS_FAILED
Expand Down Expand Up @@ -188,6 +194,14 @@ export const formsLoaded = (payload: LoadFormsResponse): FormsLoadedAction => ({
payload: payload
})

export const CustomValidatorsSuccess = (
forms: LoadFormsResponse
): CustomValidatorsLoadedLoadedAction => {
return {
type: CUSTOM_VALIDATORS_LOADED,
payload: forms
}
}
export const formsFailed = (error: Error): FormsFailedAction => ({
type: FORMS_FAILED,
payload: error
Expand Down Expand Up @@ -333,6 +347,7 @@ export type Action =
| LocationsLoadedAction
| FormsFailedAction
| FormsLoadedAction
| CustomValidatorsLoadedLoadedAction
| SetOfflineData
| IGetOfflineDataSuccessAction
| IGetOfflineDataFailedAction
Expand Down
Loading