diff --git a/src/components/Action/ActionList/ActionList.tsx b/src/components/Action/ActionList/ActionList.tsx index 4de937d4c78d4e6741f67eccfde57092f139ff63..c2bcdf72cb2bfeaf584aad52952b540f140e8fa5 100644 --- a/src/components/Action/ActionList/ActionList.tsx +++ b/src/components/Action/ActionList/ActionList.tsx @@ -29,11 +29,9 @@ const ActionList = ({ setSelectedAction, setShowList }: ActionListProps) => { } else { actionList = await actionService.getDefaultActions() } - if (subscribed) { - setActions(actionList) - } + setActions(actionList) } - getActions() + subscribed && getActions() return () => { subscribed = false } diff --git a/src/components/Analysis/Comparison/Comparison.tsx b/src/components/Analysis/Comparison/Comparison.tsx index c21697f59ad0d22b1132ec816b065956bf584b81..5aec14697c6935144bef48298fd2c64a603b189f 100644 --- a/src/components/Analysis/Comparison/Comparison.tsx +++ b/src/components/Analysis/Comparison/Comparison.tsx @@ -60,29 +60,27 @@ const Comparison = ({ useEffect(() => { let subscribed = true async function populateData() { - if (subscribed) { - const fetchedYearIndicators = - await consumptionService.getPerformanceIndicators( - periods.monthPeriod, - TimeStep.MONTH, - fluidsWithData, - periods.previousYearPeriod + const fetchedYearIndicators = + await consumptionService.getPerformanceIndicators( + periods.monthPeriod, + TimeStep.MONTH, + fluidsWithData, + periods.previousYearPeriod + ) + if (fetchedYearIndicators) { + setYearPerformanceIndicators(fetchedYearIndicators) + if ( + fetchedYearIndicators.some( + indicator => indicator.value !== 0 || indicator.compareValue !== 0 ) - if (fetchedYearIndicators) { - setYearPerformanceIndicators(fetchedYearIndicators) - if ( - fetchedYearIndicators.some( - indicator => indicator.value !== 0 || indicator.compareValue !== 0 - ) - ) { - dispatch(setPeriod('year')) - } + ) { + dispatch(setPeriod('year')) } - setIsLoading(false) } + setIsLoading(false) } - populateData() + subscribed && populateData() return () => { subscribed = false } diff --git a/src/components/Analysis/ElecHalfHourMonthlyAnalysis/ElecHalfHourMonthlyAnalysis.tsx b/src/components/Analysis/ElecHalfHourMonthlyAnalysis/ElecHalfHourMonthlyAnalysis.tsx index 9d4ede330879fbfef11f774f43fe5d5c4f974650..a7e0b78cc2868a54972459053d6dc5b4d7ad03b6 100644 --- a/src/components/Analysis/ElecHalfHourMonthlyAnalysis/ElecHalfHourMonthlyAnalysis.tsx +++ b/src/components/Analysis/ElecHalfHourMonthlyAnalysis/ElecHalfHourMonthlyAnalysis.tsx @@ -86,7 +86,6 @@ const ElecHalfHourMonthlyAnalysis = ({ FluidType.ELECTRICITY, TimeStep.HALF_AN_HOUR ) - if (!subscribed) return if (isHalfHourLoadActivated) { setIsHalfHourActivated(true) const aggregatedDate = analysisMonth.minus({ month: 1 }) @@ -107,7 +106,7 @@ const ElecHalfHourMonthlyAnalysis = ({ setIsLoading(false) } - fetchEnedisAnalysisData() + subscribed && fetchEnedisAnalysisData() return () => { subscribed = false @@ -122,11 +121,11 @@ const ElecHalfHourMonthlyAnalysis = ({ FluidType.ELECTRICITY, analysisMonth.minus({ month: 1 }) ) - if (subscribed && price) { + if (price) { setElecPrice(price) } } - getAllLastPrices() + subscribed && getAllLastPrices() return () => { subscribed = false @@ -136,14 +135,12 @@ const ElecHalfHourMonthlyAnalysis = ({ useEffect(() => { let subscribed = true async function getOffPeakHours() { - if (subscribed) { - const offPeakHours = await emas.getOffPeakHours() - if (offPeakHours) { - setOffPeakHours(offPeakHours) - } + const offPeakHours = await emas.getOffPeakHours() + if (offPeakHours) { + setOffPeakHours(offPeakHours) } } - getOffPeakHours() + subscribed && getOffPeakHours() return () => { subscribed = false diff --git a/src/components/Analysis/MaxConsumptionCard/MaxConsumptionCard.tsx b/src/components/Analysis/MaxConsumptionCard/MaxConsumptionCard.tsx index bfbc200d33af29a5f0a7b1f3e568cc8fc78bd342..cf0c5d421e795f908258775d1c8a5caa4b9be9c4 100644 --- a/src/components/Analysis/MaxConsumptionCard/MaxConsumptionCard.tsx +++ b/src/components/Analysis/MaxConsumptionCard/MaxConsumptionCard.tsx @@ -79,9 +79,8 @@ const MaxConsumptionCard = ({ } setIsLoading(false) } - if (subscribed) { - getMaxLoadData() - } + subscribed && getMaxLoadData() + return () => { subscribed = false } diff --git a/src/components/Analysis/MonthlyAnalysis.tsx b/src/components/Analysis/MonthlyAnalysis.tsx index 0b36bc41ba1b3cbc27a146527b9a4308497401ff..588b3bfff2338957002fe6e4bb1649ec4e99adef 100644 --- a/src/components/Analysis/MonthlyAnalysis.tsx +++ b/src/components/Analysis/MonthlyAnalysis.tsx @@ -111,9 +111,7 @@ const MonthlyAnalysis = ({ setLoadingAnalysis(false) } - if (subscribed) { - populateData() - } + subscribed && populateData() return () => { saveLastScrollPosition() diff --git a/src/components/Analysis/ProfileComparator/ProfileComparator.tsx b/src/components/Analysis/ProfileComparator/ProfileComparator.tsx index 288be905f3c2426cbed9dcc8960890165c41504e..04a1464abd60daf7b3f52058712bda4a542b4f88 100644 --- a/src/components/Analysis/ProfileComparator/ProfileComparator.tsx +++ b/src/components/Analysis/ProfileComparator/ProfileComparator.tsx @@ -106,16 +106,15 @@ const ProfileComparator = ({ analysisDate.year, analysisDate.month ) - if (subscribed) { - setForecast(monthlyForecast) - if (monthlyForecast) { - getTotalValueWithConnectedFluids(monthlyForecast) - } - setIsLoading(false) + + setForecast(monthlyForecast) + if (monthlyForecast) { + getTotalValueWithConnectedFluids(monthlyForecast) } + setIsLoading(false) } } - loadAverageConsumption() + subscribed && loadAverageConsumption() return () => { subscribed = false } diff --git a/src/components/Analysis/TotalAnalysisChart/TotalAnalysisChart.tsx b/src/components/Analysis/TotalAnalysisChart/TotalAnalysisChart.tsx index 806862bd4985d13ef36d17deda7441a1bdd11121..3ec01cb5f2fb7040beea5eef8a9bca61fa1ea58b 100644 --- a/src/components/Analysis/TotalAnalysisChart/TotalAnalysisChart.tsx +++ b/src/components/Analysis/TotalAnalysisChart/TotalAnalysisChart.tsx @@ -53,9 +53,7 @@ const TotalAnalysisChart = ({ } setIsLoading(false) } - if (subscribed) { - getTotalData() - } + subscribed && getTotalData() return () => { subscribed = false } diff --git a/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx b/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx index 0caf08f963987cb860bdb8e540bfb135e6c96f80..b1ea160cc50701868690e969df3217d5bfed7117 100644 --- a/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx +++ b/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx @@ -103,12 +103,10 @@ const ChallengeCardOnGoing = ({ let subscribed = true async function importIcon() { importIconById(userChallenge.id, 'challenge').then(icon => { - if (subscribed) { - icon ? setChallengeIcon(icon) : setChallengeIcon(defaultChallengeIcon) - } + icon ? setChallengeIcon(icon) : setChallengeIcon(defaultChallengeIcon) }) } - importIcon() + subscribed && importIcon() return () => { subscribed = false } @@ -121,9 +119,7 @@ const ChallengeCardOnGoing = ({ userChallenge, currentDataload ) - if (subscribed) { - setIsDone(isChallengeDone.isDone) - } + setIsDone(isChallengeDone.isDone) } const unlockDuel = async () => { if ( @@ -147,7 +143,7 @@ const ChallengeCardOnGoing = ({ } } unlockDuel() - setChallengeResult() + subscribed && setChallengeResult() return () => { subscribed = false } diff --git a/src/components/Consumption/ConsumptionView.tsx b/src/components/Consumption/ConsumptionView.tsx index ec1ed70911b2f9b3b8a220a66dcae14eef36fd53..3e3cbe47d6aca5fa9cdf8b6f21b424bd8ad4761e 100644 --- a/src/components/Consumption/ConsumptionView.tsx +++ b/src/components/Consumption/ConsumptionView.tsx @@ -149,7 +149,7 @@ const ConsumptionView = ({ fluidType }: { fluidType: FluidType }) => { } } - if (subscribed) setConsentExpiredFluids(expiredConsents) + subscribed && setConsentExpiredFluids(expiredConsents) return () => { subscribed = false } diff --git a/src/components/ConsumptionVisualizer/EstimatedConsumptionModal.tsx b/src/components/ConsumptionVisualizer/EstimatedConsumptionModal.tsx index faed95db0f0ed476fd96e458ebc1a77cb0eca9aa..8a985579d2682f2070c67d3436913f3a16199bc8 100644 --- a/src/components/ConsumptionVisualizer/EstimatedConsumptionModal.tsx +++ b/src/components/ConsumptionVisualizer/EstimatedConsumptionModal.tsx @@ -29,11 +29,11 @@ const EstimatedConsumptionModal = ({ const fluidsPricesService = new FluidPricesService(client) async function getAllLastPrices() { const prices = await fluidsPricesService.getAllLastPrices() - if (subscribed && prices) { + if (prices) { setPrices(prices) } } - getAllLastPrices() + subscribed && getAllLastPrices() return () => { subscribed = false } diff --git a/src/components/Duel/DuelOngoing/DuelOngoing.tsx b/src/components/Duel/DuelOngoing/DuelOngoing.tsx index 54cfc11db89d6ae3d9fe333d1d6b2772fdc7bbf2..cb9be0db1eed852bdc243cd238f94be6abeafaaf 100644 --- a/src/components/Duel/DuelOngoing/DuelOngoing.tsx +++ b/src/components/Duel/DuelOngoing/DuelOngoing.tsx @@ -85,12 +85,10 @@ const DuelOngoing = ({ userChallenge, isFinished }: DuelOngoingProps) => { if (isFinished) { const dataloads = await challengeService.getUserChallengeDataload(userChallenge) - if (subscribed) { - setFinishedDataLoad(dataloads) - } + setFinishedDataLoad(dataloads) } } - populateData() + subscribed && populateData() return () => { subscribed = false } diff --git a/src/components/Ecogesture/SingleEcogestureView.tsx b/src/components/Ecogesture/SingleEcogestureView.tsx index e35c2b387ea3f4155e3350adae410847db38f97b..f88683225bc1752f88a54e69b3a0fede05a203a4 100644 --- a/src/components/Ecogesture/SingleEcogestureView.tsx +++ b/src/components/Ecogesture/SingleEcogestureView.tsx @@ -74,25 +74,23 @@ const SingleEcogestureView = () => { const data = await ecogestureService.getEcogesturesByIds([ ecogestureID || '', ]) - if (subscribed) { - if (data?.[0]) { - setEcogesture(data[0]) - // Prevent case this key doesn't exist in doctype - const icon = await importIconById(data[0].id, 'ecogesture') - if (subscribed) { - setEcogestureIcon(icon || defaultIcon) - if (currentChallenge?.exploration.ecogesture_id === data[0]._id) { - setValidExploration(currentChallenge.exploration.id) - } - } - } else { - logApp.error(`Could not find ecogesture ${ecogestureID}`) - Sentry.captureException(new Error('Could not find ecogesture')) + + if (data?.[0]) { + setEcogesture(data[0]) + // Prevent case this key doesn't exist in doctype + const icon = await importIconById(data[0].id, 'ecogesture') + + setEcogestureIcon(icon || defaultIcon) + if (currentChallenge?.exploration.ecogesture_id === data[0]._id) { + setValidExploration(currentChallenge.exploration.id) } - setIsLoading(false) + } else { + logApp.error(`Could not find ecogesture ${ecogestureID}`) + Sentry.captureException(new Error('Could not find ecogesture')) } + setIsLoading(false) } - getSingleEcogesture() + subscribed && getSingleEcogesture() return () => { subscribed = false diff --git a/src/components/EcogestureForm/EquipmentIcon/EquipmentIcon.tsx b/src/components/EcogestureForm/EquipmentIcon/EquipmentIcon.tsx index 332a188242ed1ecea7f7110d33c3d38405b0ded2..c89d39d4f9084d6f2887d4e3c19bd0eb2ae53696 100644 --- a/src/components/EcogestureForm/EquipmentIcon/EquipmentIcon.tsx +++ b/src/components/EcogestureForm/EquipmentIcon/EquipmentIcon.tsx @@ -16,11 +16,11 @@ const EquipmentIcon = ({ equipment, isChecked }: EquipmentIconProps) => { let subscribed = true async function getIcon() { const svg = await importIconById(equipment, 'equipments') - if (subscribed && svg) { + if (svg) { setIcon(svg) } } - getIcon() + subscribed && getIcon() return () => { subscribed = false } diff --git a/src/components/EcogestureSelection/EcogestureSelectionDetail/EcogestureSelectionDetail.tsx b/src/components/EcogestureSelection/EcogestureSelectionDetail/EcogestureSelectionDetail.tsx index 055a71bf620592a1c7bcf3b4f5daff1a959c2b50..34457092b06aed22ef4559d05a93c7f641ccb27d 100644 --- a/src/components/EcogestureSelection/EcogestureSelectionDetail/EcogestureSelectionDetail.tsx +++ b/src/components/EcogestureSelection/EcogestureSelectionDetail/EcogestureSelectionDetail.tsx @@ -30,11 +30,9 @@ const EcogestureSelectionDetail = ({ let subscribed = true async function getIcon() { const icon = await importIconById(ecogesture.id, 'ecogesture') - if (subscribed) { - setEcogestureIcon(icon || defaultIcon) - } + setEcogestureIcon(icon || defaultIcon) } - getIcon() + subscribed && getIcon() setShowDetails(false) return () => { subscribed = false diff --git a/src/components/EcogestureSelection/EcogestureSelectionView.tsx b/src/components/EcogestureSelection/EcogestureSelectionView.tsx index dd47ee2243d5ecd4e306de86920d3f1359b527d6..172125dda36d5e43feacc39605e51fdaa188f588 100644 --- a/src/components/EcogestureSelection/EcogestureSelectionView.tsx +++ b/src/components/EcogestureSelection/EcogestureSelectionView.tsx @@ -97,22 +97,21 @@ const EcogestureSelectionView = () => { ecogesture => ecogesture.viewedInSelection === false ) const slicedFilteredList = filteredList.slice(0, 10) - if (subscribed) { - if ( - availableList.length === slicedFilteredList.length && - slicedFilteredList.length > 0 - ) { - setOpenEcSelectionModal(true) - } - setTotalAvailable(availableList.length) - setTotalViewed(availableList.length - filteredList.length) - setEcogestureList(slicedFilteredList) - setIndexEcogesture(0) - setIsLoading(false) + + if ( + availableList.length === slicedFilteredList.length && + slicedFilteredList.length > 0 + ) { + setOpenEcSelectionModal(true) } + setTotalAvailable(availableList.length) + setTotalViewed(availableList.length - filteredList.length) + setEcogestureList(slicedFilteredList) + setIndexEcogesture(0) + setIsLoading(false) } - getFilteredList() + subscribed && getFilteredList() return () => { subscribed = false } diff --git a/src/components/FluidChart/FluidChartSlide.tsx b/src/components/FluidChart/FluidChartSlide.tsx index a734dc2662104f9685466573e1f98df268398d4e..3a20b9dc600c7176077f1e12eaf9c606dd8c8bc3 100644 --- a/src/components/FluidChart/FluidChartSlide.tsx +++ b/src/components/FluidChart/FluidChartSlide.tsx @@ -47,7 +47,7 @@ const FluidChartSlide = ({ useEffect(() => { let subscribed = true async function loadData() { - if (currentTimeStep != timeStep && subscribed) { + if (currentTimeStep != timeStep) { setIsDataLoaded(false) setTimeStep(currentTimeStep) } @@ -83,13 +83,13 @@ const FluidChartSlide = ({ compareTimePeriod, isHome: isMulti, }) - if (subscribed && graphData && graphData?.actualData.length > 0) { + if (graphData && graphData?.actualData.length > 0) { setChartData(graphData) setIsDataLoaded(true) } } } - loadData() + subscribed && loadData() return () => { subscribed = false } diff --git a/src/components/Options/ExportData/ExportData.tsx b/src/components/Options/ExportData/ExportData.tsx index 488dc64b9c7fac27435d87b156719e42f73edfd7..51c761c7905c7aeb647f5cb13c99a07da42cf17a 100644 --- a/src/components/Options/ExportData/ExportData.tsx +++ b/src/components/Options/ExportData/ExportData.tsx @@ -56,9 +56,8 @@ const ExportData = () => { subscribed = false } - if (subscribed) { - getExportableFluids() - } + subscribed && getExportableFluids() + return () => { subscribed = false } diff --git a/src/components/Options/ReportOptions/ReportOptions.tsx b/src/components/Options/ReportOptions/ReportOptions.tsx index 112aa1f30b4dc8cfc357545eac0643c9e875a28a..bb0d8a6cbf621c4d4e8fb76f08c947f9ad2eee94 100644 --- a/src/components/Options/ReportOptions/ReportOptions.tsx +++ b/src/components/Options/ReportOptions/ReportOptions.tsx @@ -79,9 +79,8 @@ const ReportOptions = () => { setLastSemesterMaxDay(lastSemesterMax as Dataload) } } - if (subscribed) { - getMaxLoadData() - } + subscribed && getMaxLoadData() + return () => { subscribed = false } diff --git a/src/components/Quiz/QuizQuestion/QuizQuestion.tsx b/src/components/Quiz/QuizQuestion/QuizQuestion.tsx index 5e8b2fbca51afca221d5c8c725271152719b64da..8702fd98af7c98734905c5b30131198f6785af86 100644 --- a/src/components/Quiz/QuizQuestion/QuizQuestion.tsx +++ b/src/components/Quiz/QuizQuestion/QuizQuestion.tsx @@ -32,11 +32,9 @@ const QuizQuestion = ({ userChallenge }: { userChallenge: UserChallenge }) => { userChallenge.quiz.customQuestion, fluidTypes ) - if (subscribed) { - setCustomQuestion(customQuestion) - } + setCustomQuestion(customQuestion) } - if (isCustomQuest) { + if (isCustomQuest && subscribed) { loadCustomQuestion() } return () => { diff --git a/src/components/TotalConsumption/TotalConsumption.tsx b/src/components/TotalConsumption/TotalConsumption.tsx index eec9e9ab7730e222d0f969a3339c9e75f4d3548a..75959c1e99cef1fc63ed720fc6678189e8de628f 100644 --- a/src/components/TotalConsumption/TotalConsumption.tsx +++ b/src/components/TotalConsumption/TotalConsumption.tsx @@ -72,7 +72,7 @@ const TotalConsumption = ({ fluidType }: { fluidType: FluidType }) => { ) useEffect(() => { - let isMounted = true + let subscribed = true const fetchTotal = async () => { await computeTotal(currentDatachart.actualData, setTotalValue) if (currentDatachart.comparisonData) { @@ -82,9 +82,9 @@ const TotalConsumption = ({ fluidType }: { fluidType: FluidType }) => { ) } } - isMounted && fetchTotal() + subscribed && fetchTotal() return () => { - isMounted = false + subscribed = false } }, [currentDatachart, fluidType, currentTimeStep, client, computeTotal])