Skip to content
Snippets Groups Projects
FluidChartSlide.tsx 4.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useState, useEffect, useContext } from 'react'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    import { DateTime } from 'luxon'
    import {
      IConsumptionDataManager,
      IDataload,
      IChartData,
      ChartData,
      TimeStep,
      ITimePeriod,
    } from 'services/dataConsumptionContracts'
    import { defineTimePeriod } from 'services/dateChartService'
    import { FluidType } from 'enum/fluid.enum'
    
    import BarChart from 'components/ContentComponents/Charts/BarChart'
    
    import { AppContext } from 'components/Contexts/AppContextProvider'
    
    Romain CREY's avatar
    Romain CREY committed
    import StyledSpinner from 'components/CommonKit/Spinner/StyledSpinner'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    interface FluidChartSlideProps {
      index: number
      fluidTypes: FluidType[]
      timeStep: TimeStep
      multiFluid: boolean
      referenceDate: DateTime
      selectedDate: DateTime
      showCompare: boolean
      width: number
      height: number
      challengePeriod: ITimePeriod | null
      handleClickData: (
        dataload: IDataload,
        compareDataload: IDataload | null
      ) => void
      consumptionDataManager: IConsumptionDataManager
    
      isSwitching: boolean
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    const FluidChartSlide: React.FC<FluidChartSlideProps> = ({
      index,
      fluidTypes,
      timeStep,
      multiFluid,
      referenceDate,
      selectedDate,
      showCompare,
      width,
      height,
      challengePeriod,
      handleClickData,
      consumptionDataManager,
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }: FluidChartSlideProps) => {
      const [chartData, setChartData] = useState<IChartData>(new ChartData([]))
      const [isLoaded, setIsLoaded] = useState<boolean>(false)
    
    
    Romain CREY's avatar
    Romain CREY committed
      const { setChartIsLoaded, maxLoads, chartIsLoaded } = useContext(AppContext)
    
      const isHome: boolean = !window.location.hash.split('/')[2] ? true : false
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      useEffect(() => {
        let subscribed = true
        async function loadData() {
          const [timePeriod, compareTimePeriod] = await Promise.all([
            defineTimePeriod(referenceDate, timeStep, index),
            defineTimePeriod(referenceDate, timeStep, index + 1),
          ])
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
          const graphData = await consumptionDataManager.getGraphData(
            timePeriod,
            timeStep,
            fluidTypes,
    
            compareTimePeriod,
            isHome
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
          )
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
          if (subscribed && graphData && graphData.actualData.length > 0) {
            setChartData(graphData)
            setIsLoaded(true)
          }
        }
        setIsLoaded(false)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
        loadData()
        return () => {
          subscribed = false
        }
      }, [timeStep, fluidTypes])
    
    
    Romain CREY's avatar
    Romain CREY committed
      useEffect(() => {
        let subscribed = true
    
        const actualMonth = selectedDate.startOf('week').month
        const actualYear = selectedDate.startOf('week').year
    
        const maxTimePeriod = {
          startDate:
    
            timeStep === TimeStep.HALF_AN_HOUR
              ? selectedDate.startOf('month')
    
              : selectedDate.startOf('week').startOf('month').weekdayLong ===
                'lundi'
              ? selectedDate.startOf('week').startOf('month')
              : selectedDate
    
    Romain CREY's avatar
    Romain CREY committed
                  .startOf('week')
                  .startOf('month')
                  .plus({ days: +7 })
    
                  .startOf('week'),
    
    Romain CREY's avatar
    Romain CREY committed
          endDate:
    
            timeStep === TimeStep.HALF_AN_HOUR
              ? selectedDate.endOf('month')
              : selectedDate.startOf('week').month !==
                selectedDate.endOf('week').month
    
    Romain CREY's avatar
    Romain CREY committed
              ? selectedDate.endOf('week')
              : selectedDate.endOf('month').endOf('week'),
        }
    
        const compareMaxTimePeriod = maxTimePeriod
    
        const key = `${actualMonth}/${actualYear}-${isHome}-${fluidTypes
          .sort()
    
          .join('-')}-${timeStep}`
    
    Romain CREY's avatar
    Romain CREY committed
        async function loadData() {
          setChartIsLoaded(false)
          const graphMaxLoad = await consumptionDataManager.getMaxLoad(
            maxTimePeriod,
            timeStep,
            fluidTypes,
            compareMaxTimePeriod,
            isHome
          )
    
    Romain CREY's avatar
    Romain CREY committed
          maxLoads[key] = graphMaxLoad
    
          if (subscribed && graphMaxLoad) {
            setIsLoaded(true)
    
            setChartIsLoaded(true)
    
        if (
          key in maxLoads === false &&
          (timeStep === TimeStep.DAY || timeStep === TimeStep.HALF_AN_HOUR)
        ) {
    
    Romain CREY's avatar
    Romain CREY committed
          loadData()
        }
        return () => {
          subscribed = false
        }
      }, [selectedDate, fluidTypes])
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      return (
    
    Romain CREY's avatar
    Romain CREY committed
        <>
          <div className="fs-slide">
    
            {!chartIsLoaded || !isLoaded ? (
    
    Romain CREY's avatar
    Romain CREY committed
              <div className="chart-loading">
    
                <StyledSpinner size="5em" fluidTypes={fluidTypes} isHome={isHome} />
    
    Romain CREY's avatar
    Romain CREY committed
              </div>
    
    Romain CREY's avatar
    Romain CREY committed
              <BarChart
                chartData={chartData}
                fluidTypes={fluidTypes}
                timeStep={timeStep}
                multiFluid={multiFluid}
                selectedDate={selectedDate}
                showCompare={showCompare}
                handleClickData={handleClickData}
                height={height}
                width={width}
                challengePeriod={challengePeriod}
                isSwitching={isSwitching}
                isHome={isHome}
              />
    
    Romain CREY's avatar
    Romain CREY committed
          </div>
        </>
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      )
    }
    
    export default FluidChartSlide