Skip to content
Snippets Groups Projects
FluidChartSwipe.tsx 3.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    import FluidChartSlide from 'components/FluidChart/FluidChartSlide'
    
    import { useChartResize } from 'components/Hooks/useChartResize'
    
    import { FluidType } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { DateTime } from 'luxon'
    
    import React, { useEffect, useRef, useState } from 'react'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    import SwipeableViews from 'react-swipeable-views'
    import { virtualize } from 'react-swipeable-views-utils'
    
    import DateChartService from 'services/dateChart.service'
    
    import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.slice'
    
    import { useAppDispatch, useAppSelector } from 'store/hooks'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import './fluidChartSwipe.scss'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    const VirtualizeSwipeableViews = virtualize(SwipeableViews)
    
    interface FluidChartSwipeProps {
    
      fluidType: FluidType
    
      setActive: React.Dispatch<React.SetStateAction<boolean>>
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const FluidChartSwipe = ({ fluidType, setActive }: FluidChartSwipeProps) => {
    
      const dispatch = useAppDispatch()
    
      const { currentIndex, currentTimeStep, selectedDate } = useAppSelector(
        state => state.ecolyo.chart
      )
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      const swipe = useRef<HTMLDivElement>(null)
    
      const [isSwitching, setIsSwitching] = useState(false)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    
      const handleChangeIndex = (index: number) => {
        const dateChartService = new DateChartService()
        let increment = 0
        if (currentIndex < index) {
          increment = dateChartService.defineIncrementForPreviousIndex(
            currentTimeStep,
            selectedDate,
            currentIndex
          )
        } else {
          increment = dateChartService.defineIncrementForNextIndex(
            currentTimeStep,
            selectedDate,
            currentIndex
          )
        }
    
        const updatedDate: DateTime = dateChartService.incrementDate(
    
          currentTimeStep,
          selectedDate,
          increment
        )
        const updatedIndex: number = dateChartService.defineDateIndex(
          currentTimeStep,
          updatedDate
        )
        dispatch(setSelectedDate(updatedDate))
        dispatch(setCurrentIndex(updatedIndex))
      }
    
    
      const { height, width } = useChartResize(swipe, false)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    
      useEffect(() => {
        function initIndex() {
          const dateChartService = new DateChartService()
          const updatedIndex: number = dateChartService.defineDateIndex(
            currentTimeStep,
            selectedDate
          )
          dispatch(setCurrentIndex(updatedIndex))
        }
        initIndex()
      }, [dispatch, currentTimeStep, selectedDate])
    
    
      const slideRenderer = (key: number, index: number) => (
        <FluidChartSlide
          key={key}
          index={index}
          fluidType={fluidType}
          width={width}
          height={height}
          isSwitching={isSwitching}
          setActive={setActive}
        />
      )
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      return (
    
        <div className="fluidchartswipe-root" ref={swipe}>
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
          <VirtualizeSwipeableViews
    
            index={currentIndex}
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
            overscanSlideAfter={1}
            overscanSlideBefore={1}
            onChangeIndex={handleChangeIndex}
    
            slideRenderer={({ key, index }) => slideRenderer(key, index)}
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
            enableMouseEvents
    
    Romain CREY's avatar
    Romain CREY committed
            onSwitching={!isSwitching ? () => setIsSwitching(true) : null}
    
            onTransitionEnd={() => {
              setIsSwitching(false)
            }}
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
            axis="x-reverse"
          />
        </div>
      )
    }
    
    export default FluidChartSwipe