Skip to content

Commit

Permalink
feat: improve app performance
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Jul 15, 2022
1 parent a128375 commit 1a09dcc
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 153 deletions.
58 changes: 32 additions & 26 deletions src/components/forms/ProjectCreateModal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Stepper, Step, StepLabel } from '@mui/material';
import { useIntl } from 'react-intl';
Expand Down Expand Up @@ -103,36 +103,42 @@ const ProjectCreateModal = ({ onClose, modalSizeAndPosition }) => {
const { notification, showProgressOverlay: apiResponseIsPending } =
useSelector(state => state.app);

const stepperStepsTranslationIds = [
'project',
'issuances',
'project-locations',
'estimations',
'labels',
'ratings',
'co-benefits',
'related-projects',
];
const stepperStepsTranslationIds = useMemo(
() => [
'project',
'issuances',
'project-locations',
'estimations',
'labels',
'ratings',
'co-benefits',
'related-projects',
],
[],
);

const onChangeStepTo = async ({ formik, desiredStep = null }) => {
const errors = await formik.validateForm();
const onChangeStepTo = useCallback(
async ({ formik, desiredStep = null }) => {
const errors = await formik.validateForm();

// manually setting touched for error fields so errors are displayed
formik.setTouched(setNestedObjectValues(errors, true));
// manually setting touched for error fields so errors are displayed
formik.setTouched(setNestedObjectValues(errors, true));

const isProjectValid = _.isEmpty(errors);
const isProjectValid = _.isEmpty(errors);

if (isProjectValid) {
if (
desiredStep >= stepperStepsTranslationIds.length &&
!apiResponseIsPending
) {
formik.submitForm();
} else {
setTabValue(desiredStep);
if (isProjectValid) {
if (
desiredStep >= stepperStepsTranslationIds.length &&
!apiResponseIsPending
) {
formik.submitForm();
} else {
setTabValue(desiredStep);
}
}
}
};
},
[setTabValue, apiResponseIsPending],
);

// if project was successfully created, close modal
const projectWasSuccessfullyCreated =
Expand Down
58 changes: 32 additions & 26 deletions src/components/forms/ProjectEditModal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Stepper, Step, StepLabel } from '@mui/material';
import { useIntl } from 'react-intl';
Expand Down Expand Up @@ -57,36 +57,42 @@ const ProjectEditModal = ({
setProject(formattedProjectData);
}, [projectToBeEdited]);

const stepperStepsTranslationIds = [
'project',
'issuances',
'project-locations',
'estimations',
'labels',
'ratings',
'co-benefits',
'related-projects',
];
const stepperStepsTranslationIds = useMemo(
() => [
'project',
'issuances',
'project-locations',
'estimations',
'labels',
'ratings',
'co-benefits',
'related-projects',
],
[],
);

const onChangeStepTo = async ({ formik, desiredStep = null }) => {
const errors = await formik.validateForm();
const onChangeStepTo = useCallback(
async ({ formik, desiredStep = null }) => {
const errors = await formik.validateForm();

// manually setting touched for error fields so errors are displayed
formik.setTouched(setNestedObjectValues(errors, true));
// manually setting touched for error fields so errors are displayed
formik.setTouched(setNestedObjectValues(errors, true));

const isProjectValid = _.isEmpty(errors);
const isProjectValid = _.isEmpty(errors);

if (isProjectValid) {
if (
desiredStep >= stepperStepsTranslationIds.length &&
!apiResponseIsPending
) {
formik.submitForm();
} else {
setTabValue(desiredStep);
if (isProjectValid) {
if (
desiredStep >= stepperStepsTranslationIds.length &&
!apiResponseIsPending
) {
formik.submitForm();
} else {
setTabValue(desiredStep);
}
}
}
};
},
[setTabValue, apiResponseIsPending],
);

// if project was successfully edited, close modal
const projectWasSuccessfullyEdited =
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/ProjectEstimationForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';

import {
Expand Down Expand Up @@ -32,7 +32,10 @@ const ProjectEstimationForm = ({
handleBlur,
}) => {
const intl = useIntl();
const getFieldName = fieldName => `${name}[${index}].${fieldName}`;
const getFieldName = useCallback(
fieldName => `${name}[${index}].${fieldName}`,
[name, index],
);

return (
<ModalFormContainerStyle>
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/ProjectIssuanceForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, useCallback } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';

import {
Expand Down Expand Up @@ -26,7 +26,10 @@ import {
const ProjectIssuanceForm = memo(
({ index, name, errors, touched, value, setFieldValue, handleBlur }) => {
const intl = useIntl();
const getFieldName = fieldName => `${name}[${index}].${fieldName}`;
const getFieldName = useCallback(
fieldName => `${name}[${index}].${fieldName}`,
[name, index],
);
const areFieldsDisabled = Boolean(value.id);

return (
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/ProjectLabelForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, useCallback } from 'react';
import { useSelector } from 'react-redux';
import { useIntl, FormattedMessage } from 'react-intl';

Expand Down Expand Up @@ -35,7 +35,10 @@ const ProjectLabelForm = memo(
({ index, name, errors, touched, value, setFieldValue, handleBlur }) => {
const intl = useIntl();
const { pickLists } = useSelector(store => store.climateWarehouse);
const getFieldName = fieldName => `${name}[${index}].${fieldName}`;
const getFieldName = useCallback(
fieldName => `${name}[${index}].${fieldName}`,
[name, index],
);
const areFieldsDisabled = Boolean(value.id);

return (
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/ProjectLocationForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, useCallback } from 'react';
import { useSelector } from 'react-redux';
import { FormattedMessage, useIntl } from 'react-intl';

Expand Down Expand Up @@ -30,7 +30,10 @@ import {
const ProjectLocationForm = memo(
({ index, name, errors, touched, value, setFieldValue, handleBlur }) => {
const intl = useIntl();
const getFieldName = fieldName => `${name}[${index}].${fieldName}`;
const getFieldName = useCallback(
fieldName => `${name}[${index}].${fieldName}`,
[index, name],
);
const { pickLists } = useSelector(store => store.climateWarehouse);

return (
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/ProjectRatingForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, useCallback } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';
import { FormikError } from '../form/FormikError';
Expand Down Expand Up @@ -32,7 +32,10 @@ const ProjectRatingForm = memo(
({ index, name, errors, touched, value, setFieldValue, handleBlur }) => {
const intl = useIntl();
const { pickLists } = useSelector(store => store.climateWarehouse);
const getFieldName = fieldName => `${name}[${index}].${fieldName}`;
const getFieldName = useCallback(
fieldName => `${name}[${index}].${fieldName}`,
[index, name],
);

return (
<ModalFormContainerStyle>
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/ProjectRelatedProjectForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, useCallback } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';
import { FormikError } from '../form/FormikError';

Expand All @@ -23,7 +23,10 @@ import {
const ProjectRelatedProjectForm = memo(
({ index, name, errors, touched, value, setFieldValue, handleBlur }) => {
const intl = useIntl();
const getFieldName = fieldName => `${name}[${index}].${fieldName}`;
const getFieldName = useCallback(
fieldName => `${name}[${index}].${fieldName}`,
[name, index],
);

return (
<ModalFormContainerStyle>
Expand Down
19 changes: 16 additions & 3 deletions src/components/forms/SubscriptionModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useIntl, FormattedMessage } from 'react-intl';
import { useDispatch, useSelector } from 'react-redux';
import styled from 'styled-components';
Expand Down Expand Up @@ -81,7 +81,7 @@ const SubscriptionModal = ({ onClose }) => {
return false;
}, [orgUid]);

const submitCustomOrganization = () => {
const submitCustomOrganization = useCallback(() => {
if (!isValidationOn) {
setIsValidation(true);
}
Expand All @@ -100,7 +100,20 @@ const SubscriptionModal = ({ onClose }) => {
} else {
setTimeout(() => setIsValidation(false), 3000);
}
};
}, [
isValidationOn,
setIsValidation,
isOrgUidValid,
isIpValid,
isPortValid,
isUserAlreadySubscribedToOrgUid,
orgUid,
setOrgUid,
ip,
setIp,
port,
setPort,
]);

return (
<>
Expand Down
43 changes: 23 additions & 20 deletions src/components/forms/UnitCreateModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,35 @@ const UnitCreateModal = ({ onClose, modalSizeAndPosition }) => {
}
}, []);

const onChangeStepTo = useCallback(async ({ formik, desiredStep = null }) => {
const errors = await formik.validateForm();
const onChangeStepTo = useCallback(
async ({ formik, desiredStep = null }) => {
const errors = await formik.validateForm();

// manually setting touched for error fields so errors are displayed
formik.setTouched(setNestedObjectValues(errors, true));
// manually setting touched for error fields so errors are displayed
formik.setTouched(setNestedObjectValues(errors, true));

const isUnitValid = _.isEmpty(errors);
const isUnitValid = _.isEmpty(errors);

const isIssuanceSelected =
desiredStep > 1 ? !_.isEmpty(formik.values?.issuance) : true;
const isIssuanceSelected =
desiredStep > 1 ? !_.isEmpty(formik.values?.issuance) : true;

const isProjectSelected = Boolean(
localStorage.getItem('unitSelectedWarehouseProjectId'),
);
const isProjectSelected = Boolean(
localStorage.getItem('unitSelectedWarehouseProjectId'),
);

if (isUnitValid && isProjectSelected && isIssuanceSelected) {
if (
desiredStep >= stepperStepsTranslationIds.length &&
!apiResponseIsPending
) {
formik.submitForm();
} else {
setTabValue(desiredStep);
if (isUnitValid && isProjectSelected && isIssuanceSelected) {
if (
desiredStep >= stepperStepsTranslationIds.length &&
!apiResponseIsPending
) {
formik.submitForm();
} else {
setTabValue(desiredStep);
}
}
}
}, []);
},
[setTabValue, apiResponseIsPending],
);

// if unit was successfully created, close modal
const unitWasSuccessfullyCreated =
Expand Down

0 comments on commit 1a09dcc

Please sign in to comment.