-
Notifications
You must be signed in to change notification settings - Fork 77
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
feat: Reason for delayed registration in v2 birth #8533
Changes from 7 commits
e58cfe4
d1f52e6
dac8bbd
f97f752
17fd514
728f6fd
0ed8594
61f6d14
0d4db7e
5f9b904
2d10c6d
fc0cbba
587ae90
5cc3ce9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -462,6 +462,10 @@ class FormSectionComponent extends React.Component<AllProps> { | |
...field, | ||
id: field.id.replaceAll('.', FIELD_SEPARATOR) | ||
})) | ||
const valuesWithFormattedDate = getValuesWithFormattedDate( | ||
fieldsWithDotIds, | ||
values | ||
) | ||
|
||
return ( | ||
<section> | ||
|
@@ -477,7 +481,9 @@ class FormSectionComponent extends React.Component<AllProps> { | |
const conditionalActions: string[] = getConditionalActionsForField( | ||
field, | ||
{ | ||
$form: makeFormikFieldIdsOpenCRVSCompatible(values), | ||
$form: makeFormikFieldIdsOpenCRVSCompatible( | ||
valuesWithFormattedDate | ||
), | ||
$now: formatISO(new Date(), { representation: 'date' }) | ||
} | ||
) | ||
|
@@ -548,6 +554,36 @@ function makeFormikFieldIdsOpenCRVSCompatible<T>(data: Record<string, T>) { | |
]) | ||
) | ||
} | ||
/** | ||
* | ||
* @param fields field config in OpenCRVS format (separated with `.`) | ||
* @param values form values in formik format (separated with `FIELD_SEPARATOR`) | ||
* @returns adds 0 before single digit days and months to make them 2 digit | ||
* @because ajv's `formatMaximum` and `formatMinimum` does not allow single digit day or months | ||
*/ | ||
function getValuesWithFormattedDate( | ||
fields: FieldConfig[], | ||
values: Record<string, FieldValue> | ||
) { | ||
return fields.reduce( | ||
(acc, field) => { | ||
const fieldId = field.id.replaceAll('.', FIELD_SEPARATOR) | ||
|
||
if (field.type === 'DATE' && fieldId in values) { | ||
const value = values[fieldId as keyof typeof values] | ||
if (typeof value === 'string') { | ||
const formattedDate = value | ||
.split('-') | ||
.map((d: string) => d.padStart(2, '0')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens to years here? Could you move this under I think with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid years are always of four digits. So, if there is a year which is of less than two digits, it should be considered invalid. In that sense, we don't need to make it four digits. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, let me rephrase. Could we write few test cases for this function. If I understand correctly, it receives values from formik. Formik When we are building on top of that, I'm more worried that the functionality goes undocumented. Tests could help us there if we are not changing Pseudocode:
|
||
.join('-') | ||
acc[fieldId] = formattedDate | ||
} | ||
} | ||
return acc | ||
}, | ||
{ ...values } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose this does not need to be destructured. Let's aim that we don't mutate as a principle |
||
) | ||
} | ||
|
||
export const FormFieldGenerator: React.FC<ExposedProps> = (props) => { | ||
const intl = useIntl() | ||
|
@@ -569,7 +605,9 @@ export const FormFieldGenerator: React.FC<ExposedProps> = (props) => { | |
validate={(values) => | ||
getValidationErrorsForForm( | ||
props.fields, | ||
makeFormikFieldIdsOpenCRVSCompatible(values), | ||
makeFormikFieldIdsOpenCRVSCompatible( | ||
getValuesWithFormattedDate(props.fields, values) | ||
), | ||
props.requiredErrorMessage | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,9 +98,14 @@ export function eventHasAction(type: ActionDocument['type']) { | |
export type FieldAPI = { | ||
inArray: (values: string[]) => FieldAPI | ||
isBeforeNow: () => FieldAPI | ||
/** | ||
* Checks if the date is within `days` days in the past from now. | ||
*/ | ||
isAfter: (days: number) => FieldAPI | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could
|
||
isEqualTo: (value: string) => FieldAPI | ||
isUndefined: () => FieldAPI | ||
not: { | ||
isBeforeNow: () => FieldAPI | ||
inArray: (values: string[]) => FieldAPI | ||
equalTo: (value: string) => FieldAPI | ||
} | ||
|
@@ -158,6 +163,26 @@ export function field(fieldId: string) { | |
}, | ||
required: ['$form', '$now'] | ||
}), | ||
isAfter: (days: number) => | ||
addCondition({ | ||
type: 'object', | ||
properties: { | ||
$form: { | ||
type: 'object', | ||
properties: { | ||
[fieldId]: { | ||
type: 'string', | ||
format: 'date', | ||
formatMinimum: new Date(Date.now() - days * 24 * 60 * 60 * 1000) | ||
.toISOString() | ||
.split('T')[0] | ||
} | ||
}, | ||
required: [fieldId] | ||
} | ||
}, | ||
required: ['$form'] | ||
}), | ||
isEqualTo: (value: string) => | ||
addCondition({ | ||
type: 'object', | ||
|
@@ -205,6 +230,30 @@ export function field(fieldId: string) { | |
required: ['$form'] | ||
}), | ||
not: { | ||
isBeforeNow: () => | ||
addCondition({ | ||
type: 'object', | ||
properties: { | ||
$form: { | ||
type: 'object', | ||
properties: { | ||
[fieldId]: { | ||
type: 'string', | ||
not: { | ||
format: 'date', | ||
formatMaximum: { $data: '2/$now' } | ||
} | ||
} | ||
}, | ||
required: [fieldId] | ||
}, | ||
$now: { | ||
type: 'string', | ||
format: 'date' | ||
} | ||
}, | ||
required: ['$form', '$now'] | ||
}), | ||
inArray: (values: string[]) => | ||
addCondition({ | ||
type: 'object', | ||
|
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.
Appreciate the comment 🙏 @ because might not be supported. It seems that we are going through all the values but only touching one. Would there be more specific names to functions, or could the implementation be more generic?