Skip to content

Commit

Permalink
Merge pull request #518 from Chia-Network/update-error
Browse files Browse the repository at this point in the history
Validate on next
  • Loading branch information
MichaelTaylor3D committed Mar 9, 2022
2 parents 6867a25 + 9778b54 commit 716a42d
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 77 deletions.
7 changes: 7 additions & 0 deletions src/components/blocks/APIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
deleteProject,
deleteUnit,
} from '../../store/actions/climateWarehouseActions';
import { setForm, setValidateForm } from '../../store/actions/app';

const Table = styled('table')`
box-sizing: border-box;
Expand Down Expand Up @@ -249,6 +250,8 @@ const APIDataTable = withTheme(
}),
action: () => {
setEditRecord(record);
dispatch(setForm('project'));
dispatch(setValidateForm(false));
},
},
{
Expand Down Expand Up @@ -285,6 +288,8 @@ const APIDataTable = withTheme(
<EditUnitsForm
onClose={() => {
setEditRecord(null);
dispatch(setForm(null));
dispatch(setValidateForm(false));
}}
record={editRecord}
modalSizeAndPosition={modalSizeAndPosition}
Expand All @@ -294,6 +299,8 @@ const APIDataTable = withTheme(
<EditProjectsForm
onClose={() => {
setEditRecord(null);
dispatch(setForm(null));
dispatch(setValidateForm(false));
}}
record={editRecord}
modalSizeAndPosition={modalSizeAndPosition}
Expand Down
8 changes: 6 additions & 2 deletions src/components/forms/CreateCoBenefitsForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import u from 'updeep';
import React, { useState, useEffect } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';

import { coBenefitSchema } from '../../store/validations';
import { setValidationErrors } from '../../utils/validationUtils';
Expand All @@ -24,15 +25,18 @@ import {

const CreateCoBenefitsForm = ({ value, onChange }) => {
const [errorCoBenefitsMessage, setErrorCoBenefitsMessage] = useState({});
const { validateForm, formType } = useSelector(state => state.app);
const intl = useIntl();

const onInputChange = (field, changeValue) => {
onChange(u({ [field]: changeValue }, value));
};

useEffect(() => {
setValidationErrors(coBenefitSchema, value, setErrorCoBenefitsMessage);
}, [value]);
if (validateForm && formType === 'co-benefits') {
setValidationErrors(coBenefitSchema, value, setErrorCoBenefitsMessage);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
8 changes: 6 additions & 2 deletions src/components/forms/CreateEstimationsForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import u from 'updeep';
import React, { useEffect, useState } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';

import {
StandardInput,
Expand All @@ -27,15 +28,18 @@ import { setValidationErrors } from '../../utils/validationUtils';

const CreateEstimationsForm = ({ value, onChange }) => {
const intl = useIntl();
const { validateForm, formType } = useSelector(state => state.app);
const [errorEstimationMessage, setErrorEstimationMessage] = useState({});

const onInputChange = (field, changeValue) => {
onChange(u({ [field]: changeValue }, value));
};

useEffect(() => {
setValidationErrors(estimationSchema, value, setErrorEstimationMessage);
}, [value]);
if (validateForm && formType === 'estimations') {
setValidationErrors(estimationSchema, value, setErrorEstimationMessage);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/CreateLocationsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ const CreateLocationsForm = ({ value, onChange }) => {
const [errorLocationMessage, setErrorLocationMessage] = useState({});
const intl = useIntl();
const { pickLists } = useSelector(store => store.climateWarehouse);
const { validateForm, formType } = useSelector(state => state.app);

const onInputChange = (field, changeValue) => {
onChange(u({ [field]: changeValue }, value));
};

useEffect(() => {
setValidationErrors(locationSchema, value, setErrorLocationMessage);
}, [value]);
if (validateForm && formType === 'project-locations') {
setValidationErrors(locationSchema, value, setErrorLocationMessage);
}
}, [value, validateForm, formType]);

const selectCountriesOptions = useMemo(
() =>
Expand Down
8 changes: 8 additions & 0 deletions src/components/forms/CreateProjectForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { postNewProject } from '../../store/actions/climateWarehouseActions';
import { useIntl } from 'react-intl';

import { projectSchema } from '../../store/validations';
import { setValidateForm, setForm } from '../../store/actions/app';
import { cleanObjectFromEmptyFieldsOrArrays } from '../../utils/formatData';

const CreateProjectForm = withRouter(({ onClose, modalSizeAndPosition }) => {
Expand Down Expand Up @@ -63,13 +64,20 @@ const CreateProjectForm = withRouter(({ onClose, modalSizeAndPosition }) => {
'related-projects',
];

useEffect(() => {
dispatch(setForm(stepperStepsTranslationIds[tabValue]));
}, [tabValue]);

const onChangeStep = async (desiredStep = null) => {
const isValid = await projectSchema.isValid(project);
dispatch(setValidateForm(true));
if (isValid) {
dispatch(setValidateForm(false));
if (desiredStep >= stepperStepsTranslationIds.length) {
handleSubmitProject();
} else {
setTabValue(desiredStep);
dispatch(setValidateForm(false));
}
}
};
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/CreateProjectIssuancesForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {

const CreateProjectIssuancesForm = ({ value, onChange }) => {
const { issuances } = useSelector(store => store.climateWarehouse);
const { validateForm, formType } = useSelector(state => state.app);
const [errorIssuanceMessage, setErrorIssuanceMessage] = useState({});
const intl = useIntl();
const { location } = useHistory();
Expand Down Expand Up @@ -102,8 +103,10 @@ const CreateProjectIssuancesForm = ({ value, onChange }) => {
};

useEffect(() => {
setValidationErrors(issuanceSchema, value, setErrorIssuanceMessage);
}, [value]);
if (validateForm && formType === 'issuances') {
setValidationErrors(issuanceSchema, value, setErrorIssuanceMessage);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
5 changes: 4 additions & 1 deletion src/components/forms/CreateProjectLabelsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { setValidationErrors } from '../../utils/validationUtils';

const CreateProjectLabelsForm = ({ value, onChange }) => {
const { labels } = useSelector(store => store.climateWarehouse);
const { validateForm, formType } = useSelector(state => state.app);
const [errorLabelMessage, setErrorLabelMessage] = useState({});
const intl = useIntl();
const { pickLists } = useSelector(store => store.climateWarehouse);
Expand Down Expand Up @@ -106,8 +107,10 @@ const CreateProjectLabelsForm = ({ value, onChange }) => {
);

useEffect(() => {
if(validateForm && formType === 'labels'){
setValidationErrors(labelSchema, value, setErrorLabelMessage);
}, [value]);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
5 changes: 4 additions & 1 deletion src/components/forms/CreateRatingsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { setValidationErrors } from '../../utils/validationUtils';

const CreateRatingsForm = ({ value, onChange }) => {
const [errorRatingMessage, setErrorRatingMessage] = useState({});
const { validateForm, formType } = useSelector(state => state.app);
const intl = useIntl();
const { pickLists } = useSelector(store => store.climateWarehouse);

Expand All @@ -49,8 +50,10 @@ const CreateRatingsForm = ({ value, onChange }) => {
);

useEffect(() => {
if(validateForm && formType === 'ratings'){
setValidationErrors(ratingSchema, value, setErrorRatingMessage);
}, [value]);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
16 changes: 10 additions & 6 deletions src/components/forms/CreateRelatedProjectsForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import u from 'updeep';
import React, { useEffect, useState } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';

import { relatedProjectSchema } from '../../store/validations';
import { setValidationErrors } from '../../utils/validationUtils';
Expand All @@ -27,19 +28,22 @@ const CreateRelatedProjectsForm = ({ value, onChange }) => {
const [errorRelatedProjectMessage, setErrorRelatedProjectMessage] = useState(
{},
);
const { validateForm, formType } = useSelector(state => state.app);
const intl = useIntl();

const onInputChange = (field, changeValue) => {
onChange(u({ [field]: changeValue }, value));
};

useEffect(() => {
setValidationErrors(
relatedProjectSchema,
value,
setErrorRelatedProjectMessage,
);
}, [value]);
if (validateForm && formType === 'related-projects') {
setValidationErrors(
relatedProjectSchema,
value,
setErrorRelatedProjectMessage,
);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
15 changes: 10 additions & 5 deletions src/components/forms/CreateUnitIssuanceForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ import {

const CreateUnitIssuanceForm = ({ value, onChange }) => {
const { issuances } = useSelector(store => store.climateWarehouse);
const { validateForm, formType } = useSelector(state => state.app);
const [errorIssuanceMessage, setErrorIssuanceMessage] = useState({});
const intl = useIntl();
const { location } = useHistory();

const isUserOnUnitsPage = location.pathname.includes('projects') ? true : false;
const isUserOnUnitsPage = location.pathname.includes('projects')
? true
: false;

const areFieldsDisabled = useMemo(() => {
if (!isUserOnUnitsPage) {
Expand All @@ -45,9 +48,9 @@ const CreateUnitIssuanceForm = ({ value, onChange }) => {
}
return false;
}
if (isUserOnUnitsPage) {
return true;
}
if (isUserOnUnitsPage) {
return true;
}
}, [isUserOnUnitsPage, value, value.id]);

const getIssuanceLabel = issuance => {
Expand Down Expand Up @@ -102,8 +105,10 @@ const CreateUnitIssuanceForm = ({ value, onChange }) => {
};

useEffect(() => {
if(validateForm && formType === 'issuances'){
setValidationErrors(issuanceSchema, value, setErrorIssuanceMessage);
}, [value]);
}
}, [value, validateForm, formType]);

return (
<ModalFormContainerStyle>
Expand Down
11 changes: 8 additions & 3 deletions src/components/forms/CreateUnitLabelsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ import { setValidationErrors } from '../../utils/validationUtils';

const CreateUnitLabelsForm = ({ value, onChange }) => {
const { labels } = useSelector(store => store.climateWarehouse);
const { validateForm, formType } = useSelector(state => state.app);
const [errorLabelMessage, setErrorLabelMessage] = useState({});
const intl = useIntl();
const { pickLists } = useSelector(store => store.climateWarehouse);
const { location } = useHistory();

const isUserOnUnitsPage = location.pathname.includes('projects') ? true : false;
const isUserOnUnitsPage = location.pathname.includes('projects')
? true
: false;

const areFieldsDisabled = useMemo(() => {
if (!isUserOnUnitsPage) {
Expand Down Expand Up @@ -102,8 +105,10 @@ const CreateUnitLabelsForm = ({ value, onChange }) => {
);

useEffect(() => {
setValidationErrors(labelSchema, value, setErrorLabelMessage);
}, [value]);
if (validateForm && formType === 'labels') {
setValidationErrors(labelSchema, value, setErrorLabelMessage);
}
}, [value, validateForm,formType]);

return (
<ModalFormContainerStyle>
Expand Down
6 changes: 5 additions & 1 deletion src/components/forms/CreateUnitsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useIntl } from 'react-intl';
import { unitsSchema } from '../../store/validations';
import { UnitDetailsForm } from '.';
import { cleanObjectFromEmptyFieldsOrArrays } from '../../utils/formatData';
import { setValidateForm, setForm } from '../../store/actions/app';

const StyledFormContainer = styled('div')`
display: flex;
Expand Down Expand Up @@ -50,14 +51,17 @@ const CreateUnitsForm = withRouter(({ onClose, modalSizeAndPosition }) => {
});

const stepperStepsTranslationIds = ['unit', 'issuances', 'labels'];

const onChangeStep = async (desiredStep = null) => {
const isUnitValid = await unitsSchema.isValid(unit);
dispatch(setValidateForm(true));
if (isUnitValid) {
dispatch(setValidateForm(false));
if (desiredStep >= stepperStepsTranslationIds.length) {
handleSubmitUnit();
} else {
dispatch(setValidateForm(false));
setTabValue(desiredStep);
dispatch(setForm(stepperStepsTranslationIds[desiredStep]))
}
}
};
Expand Down
Loading

0 comments on commit 716a42d

Please sign in to comment.