src\contexts\SpeedLimitStoreContext.tsx

mail@pastecode.io avatar
unknown
typescript
a month ago
2.8 kB
3
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
  // 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
      await dispatch(
        fetchZoneSpeedLimitsByRouteId({
          routeId: routeId,
          railroadId: railroadId,
          startDestinationId: startDestinationId,
          endDestinationId: endDestinationId
        })
      )
    },
    addNewZoneSpeedLimitItem: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(mutationAddNewZoneSpeedLimit())
      console.log('🚀 ~ res:', res)
      if (res?.payload?.status === 'ok') {
        payload.onSuccess()
        dispatch(resetMutationAddNewZoneSpeedLimit())
      }
    },
    editZoneSpeedLimitItem: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(mutationEditZoneSpeedLimit())
      console.log('🚀 ~ res:', res)
      if (res?.payload?.status === 'ok') {
        payload.onSuccess()
        dispatch(resetMutationEditZoneSpeedLimit())
      }
    },
    deleteZoneSpeedLimitItem: async payload => {
      console.log('🚀 ~ payload:', payload)
      const dispatch = store.dispatch
      const res: any = await dispatch(mutationDeleteZoneSpeedLimit())
      console.log('🚀 ~ res:', res)
      if (res?.payload?.status === 'ok') {
        payload.onSuccess()
        dispatch(resetMutationDeleteZoneSpeedLimit())
      }
    }
  }))
)
Leave a Comment