Newer
Older
import ExpiredConsentModal from 'components/Connection/ExpiredConsentModal/ExpiredConsentModal'
import { GrdfWaitConsent } from 'components/Connection/GRDFConnect/GrdfWaitConsent'
import Content from 'components/Content/Content'
import CustomPopupModal from 'components/CustomPopup/CustomPopupModal'
import DateNavigator from 'components/DateNavigator/DateNavigator'
import FluidChart from 'components/FluidChart/FluidChart'
import CozyBar from 'components/Header/CozyBar'
import Header from 'components/Header/Header'
import KonnectorViewerCard from 'components/Konnector/KonnectorViewerCard'
import KonnectorViewerList from 'components/Konnector/KonnectorViewerList'
import PartnerIssueModal from 'components/PartnerIssue/PartnerIssueModal'
import ReleaseNotesModal from 'components/ReleaseNotesModal/ReleaseNotesModal'
import { DateTime } from 'luxon'
import React, { useCallback, useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import DateChartService from 'services/dateChart.service'
import {
setCurrentIndex,
setCurrentTimeStep,
setSelectedDate,
setShowOfflineData,
} from 'store/chart/chart.slice'
import { showReleaseNotes } from 'store/global/global.slice'
import { useAppDispatch, useAppSelector } from 'store/hooks'
import { openPartnersModal, setCustomPopup } from 'store/modal/modal.slice'
import { isLastDateReached } from 'utils/date'
import {
getKonnectorUpdateError,
getTodayDate,
isKonnectorActive,
} from 'utils/utils'
import ConsumptionDetails from './ConsumptionDetails/ConsumptionDetails'
import FluidButtons from './FluidButtons/FluidButtons'
const ConsumptionView = ({ fluidType }: { fluidType: FluidType }) => {
const navigate = useNavigate()
const isMulti = fluidType === FluidType.MULTIFLUID
chart: { currentTimeStep, showOfflineData, selectedDate, currentIndex },
global: { fluidStatus, releaseNotes },
modal: { partnersIssueModal, customPopupModal },

Hugo NOUTS
committed
const dateChartService = new DateChartService()

Hugo NOUTS
committed
const [openReleaseNoteModal, setOpenReleaseNoteModal] = useState<boolean>(
releaseNotes.show
)

Guilhem CARRON
committed
const [active, setActive] = useState<boolean>(false)
const [openExpiredConsentModal, setOpenExpiredConsentModal] =
const [consentExpiredFluids, setConsentExpiredFluids] = useState<FluidType[]>(

Guilhem CARRON
committed
[]
)

Guilhem CARRON
committed
fluidType !== FluidType.MULTIFLUID && currentFluidStatus.lastDataDate
? `${currentFluidStatus.lastDataDate.toLocaleString()} + ${
currentFluidStatus.status + fluidType
: ''
const lastDataDateKey =
fluidType !== FluidType.MULTIFLUID && currentFluidStatus.lastDataDate
? `${currentFluidStatus.lastDataDate.toLocaleString() + fluidType}`

Guilhem CARRON
committed
const handleCloseReleaseNoteModal = useCallback(() => {
setOpenReleaseNoteModal(false)
dispatch(
showReleaseNotes({
show: false,
notes: releaseNotes.notes,
redirectLink: releaseNotes.redirectLink,
})
)
if (releaseNotes.redirectLink) {
navigate(releaseNotes.redirectLink)
}, [dispatch, navigate, releaseNotes.notes, releaseNotes.redirectLink])

Hugo NOUTS
committed
const getPartnerKey = (fluidType: FluidType): 'enedis' | 'egl' | 'grdf' => {
switch (fluidType) {
case FluidType.ELECTRICITY:
return 'enedis'
case FluidType.WATER:
return 'egl'
case FluidType.GAS:
return 'grdf'
default:
throw new Error('unknown fluidtype')
}
const handleClosePartnerIssueModal = useCallback(
async (fluidType: FluidType) => {
const profileService = new ProfileService(client)
const profileValues = await profileService.getProfile()
if (profileValues) {
const updatedProfile = await profileService.updateProfile({
partnersIssueSeenDate: {
...profileValues.partnersIssueSeenDate,
[getPartnerKey(fluidType)]: getTodayDate(),
},
})
if (updatedProfile) {
dispatch(
openPartnersModal({
...partnersIssueModal,
[getPartnerKey(fluidType)]: false,
})
)
}
}
},
[client, dispatch, partnersIssueModal]
const handleCloseCustomPopupModal = async () => {
const profileService = new ProfileService(client)
const updatedProfile = await profileService.updateProfile({
customPopupDate: getTodayDate(),
})
if (updatedProfile) {
dispatch(
setCustomPopup({
...customPopupModal,
popupEnabled: false,
})
)
}
}
useEffect(
/** Reset half-hour timestep for water & gas */
function setDefaultTimeStep() {
if (
fluidType !== FluidType.ELECTRICITY &&
currentTimeStep == TimeStep.HALF_AN_HOUR
) {
dispatch(setCurrentTimeStep(TimeStep.WEEK))
}
},
[dispatch, fluidType, currentTimeStep]
)
/**
* If fluid is not connected, display Connect components
* If fluid is connected, dispatch FluidChart
*/
const isFluidConnected = isKonnectorActive(fluidStatus, fluidType)
dispatch(setShowOfflineData(isFluidConnected))
}, [dispatch, fluidStatus, fluidType])

Guilhem CARRON
committed
useEffect(() => {
let subscribed = true
const expiredConsents: FluidType[] = []
// Check if some fluids have expired consent error

Guilhem CARRON
committed
for (const fluid of fluidStatus) {
const error = fluid.connection.triggerState?.last_error
if (error && getKonnectorUpdateError(error) === 'error_update_oauth') {

Guilhem CARRON
committed
expiredConsents.push(fluid.fluidType)
}
}
if (subscribed) setConsentExpiredFluids(expiredConsents)

Guilhem CARRON
committed
return () => {
subscribed = false
}
}, [fluidStatus])
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const disablePrev =
selectedDate <
DateTime.local(0, 1, 1).setZone('utc', {
keepLocalTime: true,
}) && !isKonnectorActive(fluidStatus, FluidType.MULTIFLUID)
const getIncrement = (next: boolean) =>
next
? dateChartService.defineIncrementForNextIndex(
currentTimeStep,
selectedDate,
currentIndex
)
: dateChartService.defineIncrementForPreviousIndex(
currentTimeStep,
selectedDate,
currentIndex
)
const handleClickMove = (next: boolean) => {
const increment = getIncrement(next)
const updatedDate = dateChartService.incrementDate(
currentTimeStep,
selectedDate,
increment
)
const updatedIndex = dateChartService.defineDateIndex(
currentTimeStep,
updatedDate
)
dispatch(setSelectedDate(updatedDate))
dispatch(setCurrentIndex(updatedIndex))
}
<CozyBar titleKey="common.title_consumption" />
<Header desktopTitleKey="common.title_consumption">
<DateNavigator
disableNext={isLastDateReached(selectedDate, currentTimeStep)}
disablePrev={disablePrev}
handleNextDate={() => handleClickMove(true)}
handlePrevDate={() => handleClickMove(false)}
navigatorDate={selectedDate}
timeStep={currentTimeStep}
/>
<FluidButtons activeFluid={fluidType} key={updateKey} />

Guilhem CARRON
committed
{fluidType === FluidType.GAS &&
currentFluidStatus.status === FluidState.CHALLENGE_ASKED && (
<GrdfWaitConsent />
)}
<FluidChart
fluidType={fluidType}
setActive={setActive}
key={lastDataDateKey}
/>
<ConsumptionDetails fluidType={fluidType} />
{!isMulti && (
<KonnectorViewerCard
fluidType={fluidType}
showOfflineData={showOfflineData}
setActive={setActive}
active={active}
/>
{!showOfflineData && isMulti && <KonnectorViewerList />}
{/* MODALS */}
{openReleaseNoteModal && (
<ReleaseNotesModal
open={openReleaseNoteModal}
handleCloseClick={handleCloseReleaseNoteModal}
/>
)}
{/* Partner issue modals for individual fluids */}
{fluidStatus
.filter(fluid => fluid.maintenance)
.filter(fluid => fluid.fluidType === fluidType)
.map(issuedFluid => (
<PartnerIssueModal
key={issuedFluid.fluidType}
open={partnersIssueModal[getPartnerKey(issuedFluid.fluidType)]}
handleCloseClick={handleClosePartnerIssueModal}
/>
))}
<CustomPopupModal
customPopup={customPopupModal}
handleCloseClick={handleCloseCustomPopupModal}
/>
{Boolean(consentExpiredFluids.length) &&
consentExpiredFluids.map(fluid => (
<ExpiredConsentModal
key={fluid}
open={openExpiredConsentModal}
handleCloseClick={() => setOpenExpiredConsentModal(false)}
fluidType={fluid}
toggleModal={() => setOpenExpiredConsentModal(prev => !prev)}
/>
))}
</>
)
}
export default ConsumptionView