Skip to content

Commit

Permalink
fix: edit project - entries get duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Feb 16, 2022
1 parent 7e553fc commit 6b31ab4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 72 deletions.
135 changes: 66 additions & 69 deletions src/components/forms/CreateProjectForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,15 @@ import {
ratingsSchema,
projectSchema,
} from '../../store/validations';
import { cleanObjectFromEmptyFieldsOrArrays } from '../../utils/formatData';

const CreateProjectForm = withRouter(({ onClose, modalSizeAndPosition }) => {
const [newLabels, setNewLabels] = useState([]);
const [newRelatedProjects, setNewRelatedProjects] = useState([]);
const [newIssuance, setNewIssuance] = useState([]);
const [newProjectLocations, setNewProjectLocations] = useState([]);
const [newCoBenefits, setNewCoBenefits] = useState([]);
const [estimationsState, setEstimationsState] = useState([]);
const [ratingsState, setRatingsState] = useState([]);
const [tabValue, setTabValue] = useState(0);
const dispatch = useDispatch();
const intl = useIntl();
const { notification } = useSelector(state => state.app);

const [newProject, setNewProject] = useState({
const [project, setProject] = useState({
registryOfOrigin: '',
originProjectId: '',
program: '',
Expand Down Expand Up @@ -92,71 +86,39 @@ const CreateProjectForm = withRouter(({ onClose, modalSizeAndPosition }) => {
const onNextButtonPress = async () => {
switch (stepperStepsTranslationIds[tabValue]) {
case 'project':
switchToNextTabIfDataIsValid(newProject, projectSchema);
switchToNextTabIfDataIsValid(project, projectSchema);
break;
case 'labels':
switchToNextTabIfDataIsValid(newLabels, labelsSchema);
switchToNextTabIfDataIsValid(project.labels, labelsSchema);
break;
case 'issuances':
switchToNextTabIfDataIsValid(newIssuance, issuancesSchema);
switchToNextTabIfDataIsValid(project.issuances, issuancesSchema);
break;
case 'co-benefits':
switchToNextTabIfDataIsValid(newCoBenefits, coBenefitsSchema);
switchToNextTabIfDataIsValid(project.coBenefits, coBenefitsSchema);
break;
case 'project-locations':
switchToNextTabIfDataIsValid(newProjectLocations, locationsSchema);
switchToNextTabIfDataIsValid(project.projectLocations, locationsSchema);
break;
case 'related-projects':
switchToNextTabIfDataIsValid(newRelatedProjects, relatedProjectsSchema);
switchToNextTabIfDataIsValid(
project.relatedProjects,
relatedProjectsSchema,
);
break;
case 'estimations':
switchToNextTabIfDataIsValid(estimationsState, estimationsSchema);
switchToNextTabIfDataIsValid(project.estimations, estimationsSchema);
break;
case 'ratings':
switchToNextTabIfDataIsValid(ratingsState, ratingsSchema);
switchToNextTabIfDataIsValid(project.projectRatings, ratingsSchema);
break;
}
};

const handleSubmit = async () => {
const dataToSend = _.cloneDeep(newProject);

Object.keys(dataToSend).forEach(el => {
if (!dataToSend[el]) {
delete dataToSend[el];
}
});

if (!_.isEmpty(newLabels)) {
dataToSend.labels = newLabels;
}

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

if (!_.isEmpty(newCoBenefits)) {
dataToSend.coBenefits = newCoBenefits;
}

if (!_.isEmpty(newProjectLocations)) {
dataToSend.projectLocations = newProjectLocations;
}

if (!_.isEmpty(newRelatedProjects)) {
dataToSend.relatedProjects = newRelatedProjects;
}

if (!_.isEmpty(estimationsState)) {
dataToSend.estimations = estimationsState;
}

if (!_.isEmpty(ratingsState)) {
dataToSend.projectRatings = ratingsState;
}

const dataToSend = _.cloneDeep(project);
cleanObjectFromEmptyFieldsOrArrays(dataToSend);
const projectIsValid = await projectSchema.isValid(dataToSend);

if (projectIsValid) {
dispatch(postNewProject(dataToSend));
}
Expand Down Expand Up @@ -215,50 +177,85 @@ const CreateProjectForm = withRouter(({ onClose, modalSizeAndPosition }) => {
index={0}
>
<ProjectDetailsForm
projectDetails={newProject}
setProjectDetails={setNewProject}
projectDetails={project}
setProjectDetails={setProject}
/>
</TabPanel>
<TabPanel value={tabValue} index={1}>
<LabelsRepeater
labelsState={newLabels}
newLabelsState={setNewLabels}
labelsState={project?.labels ?? []}
newLabelsState={value =>
setProject(prev => ({
...prev,
labels: value,
}))
}
/>
</TabPanel>
<TabPanel value={tabValue} index={2}>
<IssuanceRepeater
issuanceState={newIssuance}
newIssuanceState={setNewIssuance}
issuanceState={project?.issuances ?? []}
newIssuanceState={value =>
setProject(prev => ({
...prev,
issuances: value,
}))
}
/>
</TabPanel>
<TabPanel value={tabValue} index={3}>
<CoBenefitsRepeater
coBenefitsState={newCoBenefits}
setNewCoBenefitsState={setNewCoBenefits}
coBenefitsState={project?.coBenefits ?? []}
setNewCoBenefitsState={value =>
setProject(prev => ({
...prev,
coBenefits: value,
}))
}
/>
</TabPanel>
<TabPanel value={tabValue} index={4}>
<LocationsRepeater
locationsState={newProjectLocations}
setLocationsState={setNewProjectLocations}
locationsState={project?.projectLocations ?? []}
setLocationsState={value =>
setProject(prev => ({
...prev,
projectLocations: value,
}))
}
/>
</TabPanel>
<TabPanel value={tabValue} index={5}>
<RelatedProjectsRepeater
relatedProjectsState={newRelatedProjects}
setRelatedProjectsState={setNewRelatedProjects}
relatedProjectsState={project?.relatedProjects ?? []}
setRelatedProjectsState={value =>
setProject(prev => ({
...prev,
relatedProjects: value,
}))
}
/>
</TabPanel>
<TabPanel value={tabValue} index={6}>
<EstimationsRepeater
estimationsState={estimationsState}
setEstimationsState={setEstimationsState}
estimationsState={project?.estimations ?? []}
setEstimationsState={value =>
setProject(prev => ({
...prev,
estimations: value,
}))
}
/>
</TabPanel>
<TabPanel value={tabValue} index={7}>
<RatingsRepeater
ratingsState={ratingsState}
setRatingsState={setRatingsState}
ratingsState={project?.projectRatings ?? []}
setRatingsState={value =>
setProject(prev => ({
...prev,
projectRatings: value,
}))
}
/>
</TabPanel>
</div>
Expand Down
3 changes: 0 additions & 3 deletions src/components/forms/EditProjectsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ const EditProjectsForm = ({

const handleSubmitProject = async () => {
const dataToSend = _.cloneDeep(project);

cleanObjectFromEmptyFieldsOrArrays(dataToSend);

const projectIsValid = await projectSchema.isValid(dataToSend);

if (projectIsValid) {
dispatch(updateProjectRecord(dataToSend));
}
Expand Down

0 comments on commit 6b31ab4

Please sign in to comment.