Skip to content

Commit

Permalink
feat: add validation for project forms
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Feb 14, 2022
1 parent a573faf commit 3cd19f0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/components/forms/CreateLocationsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const CreateLocationsForm = ({ value, onChange }) => {
<StyledLabelContainer>
<Body>
<LabelContainer>
<FormattedMessage id="in-country-region" />
*<FormattedMessage id="in-country-region" />
</LabelContainer>
<ToolTipContainer
tooltip={intl.formatMessage({
Expand Down
23 changes: 9 additions & 14 deletions src/components/forms/CreateProjectForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import React, { useState, useEffect, useRef } from 'react';
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import { useDispatch, useSelector } from 'react-redux';
import { withRouter } from 'react-router-dom';
Expand Down Expand Up @@ -31,7 +31,7 @@ import {
locationsSchema,
estimationsSchema,
ratingsSchema,
// projectSchema,
projectSchema,
} from '../../store/validations';

const StyledFormContainer = styled('div')`
Expand All @@ -54,8 +54,6 @@ const CreateProjectForm = withRouter(
const dispatch = useDispatch();
const intl = useIntl();
const { notification } = useSelector(state => state.app);
const labelRef = useRef();
const issuanceRef = useRef();

const [newProject, setNewProject] = useState({
registryOfOrigin: '',
Expand Down Expand Up @@ -146,7 +144,7 @@ const CreateProjectForm = withRouter(
}

if (!_.isEmpty(newIssuance)) {
dataToSend.issuance = newIssuance;
dataToSend.issuances = newIssuance;
}

if (!_.isEmpty(newCoBenefits)) {
Expand All @@ -169,14 +167,13 @@ const CreateProjectForm = withRouter(
dataToSend.projectRatings = ratingsState;
}

// const projectIsValid = await projectSchema.isValid(dataToSend);
const projectIsValid = await projectSchema.isValid(dataToSend);

// if (projectIsValid) {
// console.log('project is valid');
dispatch(postNewProject(dataToSend));
// } else {
// console.log('project is invalid');
// }
if (projectIsValid) {
dispatch(postNewProject(dataToSend));
} else {
console.log('project is invalid');
}
};

const projectWasSuccessfullyCreated =
Expand Down Expand Up @@ -243,14 +240,12 @@ const CreateProjectForm = withRouter(
<LabelsRepeater
labelsState={newLabels}
newLabelsState={setNewLabels}
labelRef={labelRef}
/>
</TabPanel>
<TabPanel value={tabValue} index={2}>
<IssuanceRepeater
issuanceState={newIssuance}
newIssuanceState={setNewIssuance}
issuanceRef={issuanceRef}
/>
</TabPanel>
<TabPanel value={tabValue} index={3}>
Expand Down
6 changes: 3 additions & 3 deletions src/components/forms/CreateRelatedProjectsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const CreateRelatedProjectsForm = ({ value, onChange }) => {
<StyledLabelContainer>
<Body>
<LabelContainer>
<FormattedMessage id="related-project-id" />
*<FormattedMessage id="related-project-id" />
</LabelContainer>
<ToolTipContainer
tooltip={intl.formatMessage({
Expand Down Expand Up @@ -82,7 +82,7 @@ const CreateRelatedProjectsForm = ({ value, onChange }) => {
<StyledLabelContainer>
<Body>
<LabelContainer>
<FormattedMessage id="relationship-type" />
*<FormattedMessage id="relationship-type" />
</LabelContainer>
<ToolTipContainer
tooltip={intl.formatMessage({
Expand Down Expand Up @@ -116,7 +116,7 @@ const CreateRelatedProjectsForm = ({ value, onChange }) => {
<StyledLabelContainer>
<Body>
<LabelContainer>
<FormattedMessage id="registry" />
*<FormattedMessage id="registry" />
</LabelContainer>
<ToolTipContainer
tooltip={intl.formatMessage({
Expand Down
2 changes: 2 additions & 0 deletions src/store/validations/labels.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const labelSchema = yup.object().shape({
.required('Required Field'),
creditingPeriodEndDate: yup
.date()
.min(yup.ref('creditingPeriodStartDate'))
.typeError('Invalid Date')
.required('Required Field'),
validityPeriodStartDate: yup
Expand All @@ -17,6 +18,7 @@ export const labelSchema = yup.object().shape({
.required('Required Field'),
validityPeriodEndDate: yup
.date()
.min(yup.ref('validityPeriodStartDate'))
.typeError('Invalid Date')
.required('Required Field'),
unitQuantity: yup
Expand Down
2 changes: 1 addition & 1 deletion src/store/validations/locations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as yup from 'yup';
export const locationSchema = yup.object({
id: yup.string().optional(),
country: yup.string().required('Required Field'),
inCountryRegion: yup.string().optional(),
inCountryRegion: yup.string().required('Required Field'),
geographicIdentifier: yup.string().required('Required Field'),
updatedAt: yup.date().optional(),
createdAt: yup.date().optional(),
Expand Down
21 changes: 7 additions & 14 deletions src/store/validations/project.validations.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import * as yup from 'yup';

/*
import {
labelSchema,
issuanceSchema,
coBenefitSchema,
locationSchema,
relatedProjectSchema,
estimationSchema,
ratingSchema,
} from './';
*/
import { coBenefitSchema } from './co-benefits.validation';
import { estimationSchema } from './estimations.validation';
import { issuanceSchema } from './Issuances.validations';
import { labelSchema } from './labels.validation';
import { locationSchema } from './locations.validations';
import { ratingSchema } from './ratings.validation';
import { relatedProjectSchema } from './relatedProjects.validation';

export const projectDetailsSchema = yup.object().shape({
registryOfOrigin: yup.string().required('Required Field'),
Expand All @@ -37,7 +32,6 @@ export const projectDetailsSchema = yup.object().shape({
validationDate: yup.date().optional(),
});

/*
export const projectSchema = yup.object().shape({
registryOfOrigin: yup.string().required('Required Field'),
originProjectId: yup.string().required('Required Field'),
Expand Down Expand Up @@ -69,4 +63,3 @@ export const projectSchema = yup.object().shape({
estimations: yup.array().of(estimationSchema).optional(),
projectRatings: yup.array().of(ratingSchema).optional(),
});
*/
6 changes: 3 additions & 3 deletions src/store/validations/relatedProjects.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as yup from 'yup';

export const relatedProjectSchema = yup.object({
id: yup.string().optional(),
relatedProjectId: yup.string().optional(),
relationshipType: yup.string().optional(),
registry: yup.string().optional(),
relatedProjectId: yup.string().required('Required Field'),
relationshipType: yup.string().required('Required Field'),
registry: yup.string().required('Required Field'),
updatedAt: yup.date().optional(),
createdAt: yup.date().optional(),
});
Expand Down

0 comments on commit 3cd19f0

Please sign in to comment.