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

chore: [DHIS2-17883] revert validate values assigned from the rules engine #3758

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
/* eslint-disable complexity */
import i18n from '@dhis2/d2-i18n';
import { isEqual } from 'lodash';
import log from 'loglevel';
import { v4 as uuid } from 'uuid';
import * as React from 'react';
Expand Down Expand Up @@ -41,7 +40,6 @@ type FieldUI = {
errorType?: ?string,
errorData?: ErrorData,
validatingMessage?: ?string,
pendingValidation?: ?boolean,
};

type RenderDividerFn = (index: number, fieldsCount: number, field: FieldConfig) => React.Node;
Expand Down Expand Up @@ -110,7 +108,6 @@ export class FormBuilder extends React.Component<Props> {
if (!validators || validators.length === 0) {
return {
valid: true,
pendingValidation: false,
};
}

Expand Down Expand Up @@ -142,16 +139,11 @@ export class FormBuilder extends React.Component<Props> {
errorMessage: validatorResult.message,
errorType: validatorResult.type,
errorData: validatorResult.data,
pendingValidation: false,
};
}

return {
valid: true,
errorMessage: null,
errorType: null,
errorData: null,
pendingValidation: false,
};
}

Expand Down Expand Up @@ -188,19 +180,17 @@ export class FormBuilder extends React.Component<Props> {
}, asyncUIState);
}

static executeValidateFields(
static executeValidateAllFields(
props: Props,
fieldsValidatingPromiseContainer: FieldsValidatingPromiseContainer,
customFields?: Array<FieldConfig>,
) {
const {
id,
fields: allFields,
fields,
values,
onGetValidationContext,
onIsValidating,
} = props;
const fields = customFields || allFields || [];
const validationContext = onGetValidationContext && onGetValidationContext();
const validationPromises = fields
.map(async (field) => {
Expand Down Expand Up @@ -243,7 +233,6 @@ export class FormBuilder extends React.Component<Props> {
valid: false,
errorMessage: [i18n.t('error encountered during field validation')],
errorType: i18n.t('error'),
pendingValidation: false,
};
log.error({ reason, field });
}
Expand Down Expand Up @@ -279,7 +268,7 @@ export class FormBuilder extends React.Component<Props> {
this.commitUpdateTriggeredForFields = {};

if (this.props.validateIfNoUIData) {
this.validateFields(this.props);
this.validateAllFields(this.props);
}
}

Expand All @@ -288,27 +277,14 @@ export class FormBuilder extends React.Component<Props> {
this.asyncUIState = FormBuilder.getAsyncUIState(this.props.fieldsUI);
this.commitUpdateTriggeredForFields = {};
if (this.props.validateIfNoUIData) {
this.validateFields(newProps);
this.validateAllFields(newProps);
}
} else {
this.asyncUIState =
FormBuilder.updateAsyncUIState(this.props.fieldsUI, newProps.fieldsUI, this.asyncUIState);
}
}

componentDidUpdate(prevProps: Props) {
const { fieldsUI, fields } = this.props;

if (!isEqual(prevProps.fieldsUI, fieldsUI)) {
const pendingValidationFields = Object.keys(fieldsUI).filter(key => fieldsUI[key].pendingValidation);

if (pendingValidationFields.length !== 0 && !this.validateAllCancelablePromise) {
const fieldsToValidate = fields.filter(field => pendingValidationFields.includes(field.id));
this.validateFields(this.props, fieldsToValidate);
}
}
}

getCleanUpData() {
const remainingCompleteUids: Array<string> = Object
.keys(this.fieldsValidatingPromiseContainer)
Expand All @@ -324,13 +300,12 @@ export class FormBuilder extends React.Component<Props> {
return remainingCompleteUids;
}

validateFields(
validateAllFields(
props: Props,
customFields?: Array<FieldConfig>,
) {
this.validateAllCancelablePromise && this.validateAllCancelablePromise.cancel();
this.validateAllCancelablePromise = makeCancelablePromise(
FormBuilder.executeValidateFields(props, this.fieldsValidatingPromiseContainer, customFields),
FormBuilder.executeValidateAllFields(props, this.fieldsValidatingPromiseContainer),
);

this.validateAllCancelablePromise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ export const formsSectionsFieldsUIDesc = createReducerDescription({
const updatedFields = Object.keys(assignEffects).reduce((acc, id) => {
if (formSectionFields[id]) {
acc[id] = {
...state[formId][id],
valid: true,
errorData: undefined,
errorMessage: undefined,
errorType: undefined,
modified: true,
touched: true,
validatingMessage: null,
pendingValidation: true,
};
}
return acc;
Expand Down
Loading