Newer
Older
import React, { useState, useEffect } from 'react'
import { translate } from 'cozy-ui/react/I18n'
import { DateTime } from 'luxon'
import {
IConsumptionDataManager,
IDataload,
TimeStep,
ITimePeriod,
TimePeriod,
} from 'services/dataConsumptionContracts'
import {
defineLastStepDate,
defineDetailedTimeStep,
formatDetailedDate,
incrementDate,
} from 'services/dateChartService'
import { UserChallenge } from 'services/dataChallengeContracts'
import { FluidType } from 'enum/fluid.enum'
import Switch from 'components/CommonKit/Switch/StyledSwitch'
import FluidChartSwipe from 'components/ContentComponents/FluidChart/FluidChartSwipe'
import ConsumptionVisualizer from 'components/ContentComponents/ConsumptionVisualizer/ConsumptionVisualizer'
import StyledIcon from 'components/CommonKit/Icon/StyledIcon'
import captionIcon from 'assets/icons/visu/challenge-caption-chart.svg'
import ActivateHalfHourLoad from '../ConsumptionNavigator/ActivateHalfHourLoad'
interface FluidChartContentProps {
fluidTypes: FluidType[]
timeStep: TimeStep
referenceDate: DateTime
lastDataDate: DateTime
lastDateWithAllData: DateTime
multiFluid: boolean
currentChallenge: UserChallenge | null
consumptionDataManager: IConsumptionDataManager
isDataLoaded: boolean
handleDetailedDate(date: DateTime, timeStep: TimeStep): void
t: Function
}
const FluidChartContent: React.FC<FluidChartContentProps> = ({
fluidTypes,
timeStep,
referenceDate,
lastDataDate,
lastDateWithAllData,
multiFluid,
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
currentChallenge,
consumptionDataManager,
isDataLoaded,
handleDetailedDate,
t,
}: FluidChartContentProps) => {
const [indexDisplayed, setIndexDisplayed] = useState<number>(0)
const [selectedDate, setSelectedDate] = useState<DateTime>(referenceDate)
const [selectedDataload, setSelectedDataload] = useState<IDataload>()
const [selectedCompareDataload, setSelectedComparedataload] = useState<
IDataload
>()
const [showCompare, setShowCompare] = useState<boolean>(false)
const [isLoaded, setIsLoaded] = useState<boolean>(true)
const challengePeriod: ITimePeriod | null =
currentChallenge &&
currentChallenge.startingDate &&
currentChallenge.endingDate &&
new TimePeriod(currentChallenge.startingDate, currentChallenge.endingDate)
const handleChangeIndex = (index: number) => {
const date =
index === 0
? referenceDate
: defineLastStepDate(referenceDate, timeStep, index)
setSelectedDate(date)
setIndexDisplayed(index)
setIsLoaded(false)
}
const handleClickData = (
dataload: IDataload,
compareDataload: IDataload | null
) => {
setSelectedDate(dataload.date)
setSelectedDataload(dataload)
if (compareDataload) {
setSelectedComparedataload(compareDataload)
}
setIsLoaded(true)
}
const handleClickDetails = () => {
const detailedTimeStep = defineDetailedTimeStep(timeStep, fluidTypes)
const detailedDate = formatDetailedDate(selectedDate, detailedTimeStep)
handleDetailedDate(detailedDate, detailedTimeStep)
setIsLoaded(false)
}
const handleClickMove = (increment: number) => {
const { incrementIndex, incrementedDate } = incrementDate(
increment,
selectedDate,
timeStep
)
setSelectedDate(incrementedDate)
if (incrementIndex != 0) {
setIndexDisplayed(indexDisplayed + incrementIndex)
setIsLoaded(false)
}
}
const handleChangeSwitch = () => {
setShowCompare(!showCompare)
}
useEffect(() => {
setSelectedDate(referenceDate)
setIndexDisplayed(0)
setIsLoaded(false)
}, [referenceDate, timeStep])
return (
<div className="fv-root">
<div className="fv-header">
<ConsumptionVisualizer
fluidTypes={fluidTypes}
timeStep={timeStep}
date={selectedDate}
dataload={selectedDataload}
compareDataload={selectedCompareDataload}
showCompare={showCompare}
multiFluid={multiFluid}
lastDataDate={lastDataDate}
lastDateWithAllData={lastDateWithAllData}
isLoaded={isLoaded}
indexDisplayed={indexDisplayed}
setSelectedDate={setSelectedDate}
setIndexDisplayed={setIndexDisplayed}
handleClickMove={handleClickMove}
handleClickDetails={handleClickDetails}
handleChangeIndex={handleChangeIndex}
/>
</div>
{!halfHourLoad && timeStep === 10 && (
<ActivateHalfHourLoad timeStep={timeStep} />
)}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{isDataLoaded && (
<FluidChartSwipe
fluidTypes={fluidTypes}
timeStep={timeStep}
multiFluid={multiFluid}
referenceDate={referenceDate}
selectedDate={selectedDate}
indexDisplayed={indexDisplayed}
showCompare={showCompare}
challengePeriod={challengePeriod}
handleChangeIndex={handleChangeIndex}
handleClickData={handleClickData}
consumptionDataManager={consumptionDataManager}
/>
)}
{!multiFluid ? (
<div className="fv-footer" onClick={handleChangeSwitch}>
<div className="fv-footer-compare text-15-normal">
<Switch fluidTypes={fluidTypes} checked={showCompare} />
<span
className={
showCompare
? `fv-footer-label graph-switch-text selected`
: `fv-footer-label graph-switch-text`
}
>
{t(`TIMESTEP.${TimeStep[timeStep]}.COMPARELABEL`)}
</span>
</div>
</div>
) : challengePeriod ? (
<div className="fv-footer">
<div className="fv-footer-challenge text-15-normal">
<StyledIcon icon={captionIcon} size={34} />
<span className="fv-footer-label-padding">
{t('CHALLENGE.PERIOD')}
</span>
</div>
</div>
) : null}
</div>
)
}
export default translate()(FluidChartContent)