Newer
Older
import ExpiredConsentModal from 'components/Connection/ExpiredConsentModal/ExpiredConsentModal'
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 { FluidType, TimeStep } from 'enums'
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
)
const [headerHeight, setHeaderHeight] = useState<number>(0)

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 && fluidStatus[fluidType].lastDataDate
? `${fluidStatus[fluidType].lastDataDate!.toLocaleString()} + ${
fluidStatus[fluidType].status + fluidType
}`
: ''
const lastDataDateKey =
fluidType !== FluidType.MULTIFLUID && fluidStatus[fluidType].lastDataDate
? `${fluidStatus[fluidType].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,
})
)
}
}
/** Handle time change */
useEffect(() => {
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])
176
177
178
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
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" />
desktopTitleKey="common.title_consumption"
<DateNavigator
disableNext={isLastDateReached(selectedDate, currentTimeStep)}
disablePrev={disablePrev}
handleNextDate={() => handleClickMove(true)}
handlePrevDate={() => handleClickMove(false)}
navigatorDate={selectedDate}
timeStep={currentTimeStep}
/>
<Content heightOffset={headerHeight}>
<FluidButtons activeFluid={fluidType} key={updateKey} />

Guilhem CARRON
committed

Hugo NOUTS
committed
{openReleaseNoteModal && (
<ReleaseNotesModal
open={openReleaseNoteModal}
handleCloseClick={handleCloseReleaseNoteModal}

Hugo NOUTS
committed
)}
<FluidChart
fluidType={fluidType}
setActive={setActive}
key={lastDataDateKey}
/>
<ConsumptionDetails fluidType={fluidType} />
<div className="konnector-section">
<KonnectorViewerCard
fluidType={fluidType}

Hugo NOUTS
committed
showOfflineData={true}

Guilhem CARRON
committed
setActive={setActive}
active={active}

Guilhem CARRON
committed
key={fluidType}
)}
{!showOfflineData && (
<div className="konnector-section">
<KonnectorViewerList />
) : (
<KonnectorViewerCard
fluidType={fluidType}
isDisconnected={true}

Hugo NOUTS
committed
showOfflineData={false}
setActive={setActive}
active={active}
/>
)}
{/* 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) &&

Guilhem CARRON
committed
consentExpiredFluids.map(fluid => {
return (
<ExpiredConsentModal
key={fluid}
open={openExpiredConsentModal}
handleCloseClick={() => setOpenExpiredConsentModal(false)}

Guilhem CARRON
committed
fluidType={fluid}
toggleModal={() => setOpenExpiredConsentModal(prev => !prev)}

Guilhem CARRON
committed
/>
)
})}
</>
)
}
export default ConsumptionView