src/contexts/SpeedLimitStoreContext.tsx

src/contexts/SpeedLimitStoreContext.tsx
mail@pastecode.io avatar
unknown
typescript
22 days ago
4.1 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 { fetchSpeedLimitsDataByRouteId } from '@Redux/slices/portalDataSlice'
import {
  mutationAddNewTSOSpeedLimit,
  mutationDeleteTSOSpeedLimitById,
  mutationEditTSOSpeedLimitById,
  resetMutationAddNewTSOSpeedLimit,
  resetMutationEditTSOSpeedLimitById,
  resetMutationDeleteTSOSpeedLimitById
} 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>
  addNewTSOSpeedLimit: (payload: any) => void
  editTSOSpeedLimit: (payload: any) => void
  deleteTSOSpeedLimit: (payload: any) => void
  // sortSpeedLimit: () => 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(
        fetchSpeedLimitsDataByRouteId({
          routeId: routeId,
          railroadId: railroadId,
          startDestinationId: startDestinationId,
          endDestinationId: endDestinationId
        })
      )
      console.log('🚀 ~ res:', res)
    },
    addNewTSOSpeedLimit: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        mutationAddNewTSOSpeedLimit({
          subdivision: payload?.subdivision,
          speedLimitType: payload?.type,
          speedLimitMph: payload?.limit,
          routeId: payload?.routeId,
          startMilepost: payload?.startMilepost,
          endMilepost: payload?.endMilepost,
          direction: payload?.direction
        })
      )
      if (!res?.error) {
        payload.onSuccess()
        dispatch(resetMutationAddNewTSOSpeedLimit())
      } else {
        payload.onFailure(res?.payload?.message)
      }
    },
    editTSOSpeedLimit: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        mutationEditTSOSpeedLimitById({
          temporarySpeedLimitId: payload.speedLimitId,
          subdivision: payload?.subdivision,
          speedLimitType: payload?.type,
          speedLimitMph: payload?.limit,
          routeId: payload?.routeId,
          startMilepost: payload?.startMilepost,
          endMilepost: payload?.endMilepost,
          direction: payload?.direction
        })
      )
      if (!res?.error) {
        payload.onSuccess()
        dispatch(resetMutationEditTSOSpeedLimitById())
      } else {
        payload.onFailure(res?.payload?.message)
      }
    },
    deleteTSOSpeedLimit: async payload => {
      const dispatch = store.dispatch
      const res: any = await dispatch(
        mutationDeleteTSOSpeedLimitById({
          temporarySpeedLimitId: payload?.temporarySpeedLimitIdDelete
        })
      )
      if (!res?.error) {
        payload.onSuccess()
        dispatch(resetMutationDeleteTSOSpeedLimitById())
      } else {
        payload.onFailure(res?.payload?.message)
      }
    },
    // sortSpeedLimit: () =>
    //   set(
    //     produce((state: TSpeedLimitState) => {
    //       state.speedLimits?.sort(
    //         (a, b) => a?.start_distance_miles - b?.start_distance_miles
    //       )
    //     })
    //   ),

    removeFirstSpeedLimit: () =>
      set(
        produce((state: TSpeedLimitState) => {
          state.speedLimits?.shift()
        })
      )
  }))
)
Leave a Comment