From 48c050b022fe0dbdf248378e879cca5411fd4886 Mon Sep 17 00:00:00 2001 From: mdulko Date: Thu, 4 Apr 2024 13:11:36 +0200 Subject: [PATCH] OLMIS-7903: fixed duplicate code --- .../MOHApproval/MOHApproveRegionBuq.jsx | 106 ++++++------------ .../MOHApproval/MOHFinalApprovalTable.jsx | 6 +- .../components/MOHApproval/MOHSearchBuq.jsx | 76 +++++++++++++ .../MOHApproval/MohForFinalApproval.jsx | 71 +++--------- 4 files changed, 124 insertions(+), 135 deletions(-) create mode 100644 src/buq/components/MOHApproval/MOHSearchBuq.jsx diff --git a/src/buq/components/MOHApproval/MOHApproveRegionBuq.jsx b/src/buq/components/MOHApproval/MOHApproveRegionBuq.jsx index 3deed96c..dc8a560d 100644 --- a/src/buq/components/MOHApproval/MOHApproveRegionBuq.jsx +++ b/src/buq/components/MOHApproval/MOHApproveRegionBuq.jsx @@ -17,16 +17,14 @@ import useLocalStorage from '../../../react-hooks/useLocalStorage'; import useCostCalculationRegion from '../../../react-hooks/useCostCalculationRegion'; import ModalErrorMessage from '../../../react-components/ModalErrorMessage'; import { addThousandsSeparatorsForStrings } from '../../utils/helpers'; +import MOHSearchBuq from "./MOHSearchBuq"; const MOHApproveRegionBuq = ({ loadingModalService }) => { const { forecastingPeriodsParams } = useBuqCommonFuncs(); const { geographicZoneParams } = useGeographicZoneGroup(); const [group, setGroup] = useState(); - const [noneSelectedGroup, setNoneSelectedGroup] = useState(false); const [forecastingPeriodId, setForecastingPeriodId] = useState(); - const [noneSelectedForecastingPeriod, setNoneSelectedForecastingPeriod] = - useState(false); const [displayFinalApproveModal, setDisplayFinalApproveModal] = useState(false); const [data, setData] = useState([]); @@ -49,41 +47,30 @@ const MOHApproveRegionBuq = ({ loadingModalService }) => { loadingModalService ); - const { handleSaveInLocalStorage } = useLocalStorage(); + const { handleSaveInLocalStorage } = useLocalStorage(); - useEffect(() => { - if (regionData) { - setData(regionData); - } - }, [regionData]); + useEffect(() => { + if (regionData) { + setData(regionData); + } + }, [regionData]); - useEffect(() => { - if (data.length) { - const allCheckboxesAreSelected = data.every( - (item) => item.checkbox === true - ); - setSelectedCheckbox(allCheckboxesAreSelected); - } else { - setSelectedCheckbox(false); + const checkAllCheckboxes = (checkData) => { + if (checkData.length) { + const allCheckboxesAreSelected = checkData.every( + (item) => item.checkbox === true + ); + setSelectedCheckbox(allCheckboxesAreSelected); + } else { + setSelectedCheckbox(false); + } } - }, [data]); - - const handleSearchButton = () => { - if (!group) { - setNoneSelectedGroup(true); - } - if (!forecastingPeriodId) { - setNoneSelectedForecastingPeriod(true); - } - if (group && forecastingPeriodId) { - fetchRegionData(); - } - }; const toggleAllCheckboxes = (value) => { setSelectedCheckbox(value); const updatedData = data.map((obj) => ({ ...obj, checkbox: value })); setData(updatedData); + checkAllCheckboxes(updatedData); }; const toggleRowCheckbox = (value, row) => { @@ -95,6 +82,7 @@ const MOHApproveRegionBuq = ({ loadingModalService }) => { }); setData(updatedData); + checkAllCheckboxes(updatedData); }; const handleFinalApproveAction = () => { @@ -219,56 +207,26 @@ const MOHApproveRegionBuq = ({ loadingModalService }) => { [data, selectedCheckbox] ); + const handleSetData = (payload) => setData(payload); + const handleSetGroup = (payload) => setGroup(payload); + const handleSetForecastingPeriodId = (payload) => setForecastingPeriodId(payload); + return ( <>

Consolidated Summary

-
-
-
-

Group

- { - setGroup(value); - setNoneSelectedGroup(false); - }} - isInvalid={noneSelectedGroup} - displayInformation={true} - /> -
-
-

Forecasting period

- { - setForecastingPeriodId(value); - setNoneSelectedForecastingPeriod(false); - }} - isInvalid={noneSelectedForecastingPeriod} - displayInformation={true} - /> -
-
-
- -
-
+ diff --git a/src/buq/components/MOHApproval/MOHFinalApprovalTable.jsx b/src/buq/components/MOHApproval/MOHFinalApprovalTable.jsx index 60cf8a0a..1ba4b1f1 100644 --- a/src/buq/components/MOHApproval/MOHFinalApprovalTable.jsx +++ b/src/buq/components/MOHApproval/MOHFinalApprovalTable.jsx @@ -45,8 +45,8 @@ const MOHApprovalTable = ({ data, redirectUrl, handleSetData }) => { Header: () => (
toggleAllCheckboxes(value)} />
@@ -55,8 +55,8 @@ const MOHApprovalTable = ({ data, redirectUrl, handleSetData }) => { Cell: ({ row }) => (
toggleRowCheckbox(value, row.original)} />
@@ -67,8 +67,8 @@ const MOHApprovalTable = ({ data, redirectUrl, handleSetData }) => {
Status
diff --git a/src/buq/components/MOHApproval/MOHSearchBuq.jsx b/src/buq/components/MOHApproval/MOHSearchBuq.jsx new file mode 100644 index 00000000..32a8414b --- /dev/null +++ b/src/buq/components/MOHApproval/MOHSearchBuq.jsx @@ -0,0 +1,76 @@ +import React, { useState } from "react"; +import InputWithSuggestionsAndValidation from '../../../react-components/inputs/input-with-suggestions-and-validation'; + +const MOHSearchBuq = ({ + geographicZoneParams, + forecastingPeriodsParams, + group, + handleSetGroup, + forecastingPeriodId, + handleSetForecastingPeriodId, + fetchBuqs + }) => { + + const [noneSelectedGroup, setNoneSelectedGroup] = useState(false); + const [noneSelectedForecastingPeriod, setNoneSelectedForecastingPeriod] = useState(false); + + const handleSearchButton = () => { + if (!group) { + setNoneSelectedGroup(true); + } + if (!forecastingPeriodId) { + setNoneSelectedForecastingPeriod(true); + } + if (group && forecastingPeriodId) { + fetchBuqs(); + } + }; + + return ( +
+
+
+

Group

+ { + handleSetGroup(value); + setNoneSelectedGroup(false); + }} + isInvalid={noneSelectedGroup} + displayInformation={true} + /> +
+
+

Forecasting period

+ { + handleSetForecastingPeriodId(value); + setNoneSelectedForecastingPeriod(false); + }} + isInvalid={noneSelectedForecastingPeriod} + displayInformation={true} + /> +
+
+
+ +
+
+ ) +} + +export default MOHSearchBuq \ No newline at end of file diff --git a/src/buq/components/MOHApproval/MohForFinalApproval.jsx b/src/buq/components/MOHApproval/MohForFinalApproval.jsx index 94eab93a..1bdbf188 100644 --- a/src/buq/components/MOHApproval/MohForFinalApproval.jsx +++ b/src/buq/components/MOHApproval/MohForFinalApproval.jsx @@ -12,13 +12,12 @@ import ModalErrorMessage from '../../../react-components/ModalErrorMessage'; import MOHFinalApprovalTable from "./MOHFinalApprovalTable"; import useCostCalculationsForFourLevels from "../../../react-hooks/useCostCalculationsForFourLevels"; import DetailsBlock from '../../../react-components/DetailsBlock'; +import MOHSearchBuq from "./MOHSearchBuq"; const MohForFinalApproval = ({ loadingModalService, facilityService }) => { const { forecastingPeriodsParams } = useBuqCommonFuncs(); const { geographicZoneParams } = useGeographicZoneGroup(); - const [noneSelectedGroup, setNoneSelectedGroup] = useState(false); - const [noneSelectedForecastingPeriod, setNoneSelectedForecastingPeriod] = useState(false); const [group, setGroup] = useState(); const [forecastingPeriodId, setForecastingPeriodId] = useState(); const [data, setData] = useState([]); @@ -107,8 +106,6 @@ const MohForFinalApproval = ({ loadingModalService, facilityService }) => { } }; - const handleSetData = (payload) => setData(payload); - const detailsData = data[0]?.calculatedGroupsCosts !== undefined ? [ [ { @@ -176,65 +173,23 @@ const MohForFinalApproval = ({ loadingModalService, facilityService }) => { ] ] : []; - const handleSearchButton = () => { - if (!group) { - setNoneSelectedGroup(true); - } - if (!forecastingPeriodId) { - setNoneSelectedForecastingPeriod(true); - } - if (group && forecastingPeriodId) { - fetchBuqForFinalApproval(); - } - }; + const handleSetData = (payload) => setData(payload); + const handleSetGroup = (payload) => setGroup(payload); + const handleSetForecastingPeriodId = (payload) => setForecastingPeriodId(payload); return ( <>

Consolidated Summary

-
-
-
-

Group

- { - setGroup(value); - setNoneSelectedGroup(false); - }} - isInvalid={noneSelectedGroup} - displayInformation={true} - /> -
-
-

Forecasting period

- { - setForecastingPeriodId(value); - setNoneSelectedForecastingPeriod(false); - }} - isInvalid={noneSelectedForecastingPeriod} - displayInformation={true} - /> -
-
-
- -
-
+
{data[0]?.calculatedGroupsCosts !== undefined ?