Skip to content
Snippets Groups Projects
FluidChartSlide.tsx 3.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    import BarChart from 'components/Charts/BarChart'
    import ConsumptionVisualizer from 'components/ConsumptionVisualizer/ConsumptionVisualizer'
    import Loader from 'components/Loader/Loader'
    
    import { useClient } from 'cozy-client'
    
    import { FluidType, TimeStep } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { DateTime } from 'luxon'
    
    import { Datachart } from 'models'
    
    import React, { useEffect, useState } from 'react'
    
    import ConsumptionService from 'services/consumption.service'
    
    import DateChartService from 'services/dateChart.service'
    
    import { setCurrentDataChart } from 'store/chart/chart.slice'
    
    import { useAppDispatch, useAppSelector } from 'store/hooks'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import './fluidChartSlide.scss'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    interface FluidChartSlideProps {
      index: number
    
      fluidType: FluidType
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      width: number
      height: number
    
      isSwitching: boolean
    
      setActive: React.Dispatch<React.SetStateAction<boolean>>
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const FluidChartSlide = ({
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      index,
    
      fluidType,
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      width,
      height,
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }: FluidChartSlideProps) => {
    
      const client = useClient()
    
      const dispatch = useAppDispatch()
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const {
    
        chart: { currentTimeStep, currentIndex, showCompare },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        global: { fluidStatus, fluidTypes },
    
      } = useAppSelector(state => state.ecolyo)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      const [chartData, setChartData] = useState<Datachart>({
        actualData: [],
        comparisonData: null,
      })
    
      const [isDataLoaded, setIsDataLoaded] = useState<boolean>(false)
    
      const [timeStep, setTimeStep] = useState<TimeStep>(TimeStep.WEEK)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
      useEffect(() => {
        let subscribed = true
        async function loadData() {
    
          if (currentTimeStep != timeStep && subscribed) {
            setIsDataLoaded(false)
            setTimeStep(currentTimeStep)
          }
          if (
            !isDataLoaded &&
            index >= currentIndex - 1 &&
            index <= currentIndex + 1
          ) {
    
            const dateChartService = new DateChartService()
            const referenceDate = DateTime.local().setZone('utc', {
              keepLocalTime: true,
            })
            const [timePeriod, compareTimePeriod] = await Promise.all([
              dateChartService.defineTimePeriod(
                referenceDate,
                currentTimeStep,
                index
              ),
              dateChartService.defineTimePeriod(
                referenceDate,
                currentTimeStep,
                index + 1
              ),
            ])
            const consumptionService = new ConsumptionService(client)
    
            const fluidTypeArray: FluidType[] =
              fluidType === FluidType.MULTIFLUID ? fluidTypes : [fluidType]
    
            const graphData = await consumptionService.getGraphData(
              timePeriod,
    
              currentTimeStep,
    
              fluidTypeArray,
    
              compareTimePeriod,
    
              fluidType === FluidType.MULTIFLUID
    
            if (subscribed && graphData && graphData?.actualData.length > 0) {
    
              setChartData(graphData)
    
              setIsDataLoaded(true)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
          }
        }
        loadData()
        return () => {
          subscribed = false
        }
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      }, [
        currentIndex,
        currentTimeStep,
    
        fluidType,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        fluidTypes,
        client,
        dispatch,
        index,
    
        isDataLoaded,
        timeStep,
    
    
      useEffect(() => {
        if (index === currentIndex) {
    
          dispatch(setCurrentDataChart(chartData))
    
        }
      }, [dispatch, index, currentIndex, chartData])
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
      return (
    
        <div className="fluidchartslide-root" aria-busy={!isDataLoaded}>
    
          {!isDataLoaded ? (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            <div className="data-spinner">
              <Loader fluidType={fluidType} />
            </div>
    
              <ConsumptionVisualizer fluidType={fluidType} setActive={setActive} />
    
              <BarChart
                chartData={chartData}
                fluidType={fluidType}
                timeStep={timeStep}
    
                showCompare={showCompare}
    
                height={height}
                width={width}
                isSwitching={isSwitching}
              />
            </>
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      )
    }
    
    
    export default FluidChartSlide