src/contexts/SpeedLimitStoreContext.tsx

mail@pastecode.io avatar
unknown
typescript
a month ago
3.9 kB
1
Indexable
Never
import { speedLimitItemType } from 'ui/components/widgets/SpeedLimitSection/types'
import { createSelectors } from '@Utils'
import { create } from 'zustand'
import { produce } from 'immer'
import { store } from '@Redux/store'
import { fetchZoneSpeedLimitsByRouteId } from '@Redux/slices/portalDataSlice'
import {
  mutationAddNewZoneSpeedLimit,
  mutationDeleteZoneSpeedLimit,
  mutationEditZoneSpeedLimit,
  resetMutationDeleteZoneSpeedLimit,
  resetMutationAddNewZoneSpeedLimit,
  resetMutationEditZoneSpeedLimit
} from '@Redux/slices/portalActionSlice'
import { useLocation } from 'react-router-dom'

type TSpeedLimitState = {
  speedLimits: speedLimitItemType[] | null
  fetchData: ({
    routeId,
    railroadId,
    startDestinationId,
    endDestinationId
  }: {
    routeId?: string
    railroadId: string
    startDestinationId: string
    endDestinationId: string
  }) => Promise<void>
  addNewZoneSpeedLimitItem: (payload: any) => void
  editZoneSpeedLimitItem: (payload: any) => void
  deleteZoneSpeedLimitItem: (payload: any) => void
  cloneData: (payload: any) => void
  removeFirstSpeedLimit: () => void
  // editSpeedLimitItem: (item: speedLimitItemType, index: number) => void
}

export const useSpeedLimitStore = createSelectors(
  create<TSpeedLimitState>()(set => ({
    speedLimits: null,
    fetchData: async ({
      routeId,
      railroadId,
      startDestinationId,
      endDestinationId
    }) => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        fetchZoneSpeedLimitsByRouteId({
          routeId: routeId,
          railroadId: railroadId,
          startDestinationId: startDestinationId,
          endDestinationId: endDestinationId
        })
      )
      console.log('🚀 ~ res:', res)
    },
    addNewZoneSpeedLimitItem: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        mutationAddNewZoneSpeedLimit({
          subdivision: payload?.subdivision,
          speedLimitType: payload?.type,
          speedLimitMph: payload?.limit,
          routeId: payload?.routeId,
          startMilepost: payload?.startMilepost,
          endMilepost: payload?.endMilepost,
          direction: payload?.direction
        })
      )
      console.log('🚀 ~ res:', res)
      if (!res?.error) {
        payload.onSuccess()
        dispatch(resetMutationAddNewZoneSpeedLimit())
      }
    },
    editZoneSpeedLimitItem: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        mutationEditZoneSpeedLimit({
          temporarySpeedLimitId: payload.speedLimitId,
          subdivision: payload?.subdivision,
          speedLimitType: payload?.type,
          speedLimitMph: payload?.limit,
          routeId: payload?.routeId,
          startMilepost: payload?.startMilepost,
          endMilepost: payload?.endMilepost,
          direction: payload?.direction
        })
      )
      console.log('🚀 ~ res:', res)
      if (!res?.error) {
        payload.onSuccess()
        dispatch(resetMutationEditZoneSpeedLimit())
      }
    },
    deleteZoneSpeedLimitItem: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        mutationDeleteZoneSpeedLimit({
          temporarySpeedLimitId: payload?.temporarySpeedLimitIdDelete
        })
      )
      console.log('🚀 ~ res:', res)
      if (!res?.error) {
        payload.onSuccess()
        dispatch(resetMutationDeleteZoneSpeedLimit())
      }
    },
    cloneData: payload => {
      set(
        produce((state: TSpeedLimitState) => {
          state.speedLimits = payload
        })
      )
    },
    removeFirstSpeedLimit: () =>
      set(
        produce((state: TSpeedLimitState) => {
          state.speedLimits?.shift()
        })
      )
  }))
)
Leave a Comment