reducer/index.tsx

src/ui/components/widgets/SpeedLimitSection/reducer/index.tsx
mail@pastecode.io avatar
unknown
typescript
4 days ago
10 kB
1
Indexable
Never
import { Reducer, useReducer } from 'react'
import {
  ActionTypes,
  actionCreatorsProps,
  reducerActionProps,
  speedLimitActions,
  speedLimitStates
} from '../types'

const speedLimitReducer = (
  state: speedLimitStates,
  action: reducerActionProps
) => {
  switch (action.type) {
    case speedLimitActions.INITIALIZE_STATES: {
      const directionList = action?.payload?.initData?.routeId
        ?.split('_')
        .map((value: any[], index: number) => ({
          value,
          text: action.payload.initData.routeName.split('-')[index].trim()
        }))

      return {
        ...state,
        type: action?.payload?.initData?.type,
        routeId: action?.payload?.initData?.routeId,
        direction: directionList[1]?.value,
        directionList
      }
    }
    case speedLimitActions.INITIALIZE_ROUTES: {
      return {
        ...state,
        routes: action?.payload?.initRoutes
      }
    }
    case speedLimitActions.ON_CHANGE_TYPE: {
      return {
        ...state,
        type: action.payload.type
      }
    }
    case speedLimitActions.ON_CHANGE_ACTION_TYPE: {
      // const filteredSpeedLimitList = [
      //   ...new Set(
      //     action.payload.speedLimits?.filter(
      //       (speedLimit: any) => speedLimit.type === action.payload.selectedType ||
      //       action.payload.selectedType===''
      //     )
      //      .map((speedLimit: any) => speedLimit.)
      //   )
      // ]
      return {
        ...state,
        actionType: action.payload.actionType
      }
    }
    case speedLimitActions.ON_CHANGE_PAGE:
      return {
        ...state,
        page: action.payload.selectedPage
      }

    case speedLimitActions.ON_SELECTED_ITEM:
      return {
        ...state,
        indexItemSelected: action.payload.selectedIndex,
        dataItemSelected: action.payload.selectedItemData
      }

    case speedLimitActions.ON_CHANGE_ROUTE_ID: {
      const route = state?.routes?.find(
        item => item?.route_id === action.payload?.routeId
      )

      const directionList = route?.route_id
        ?.split('_')
        .map((value: any[], index: number) => ({
          value,
          text: route?.route_name?.split('-')[index].trim()
        }))
      return {
        ...state,
        routeId: action.payload?.routeId,
        directionList
      }
    }
    case speedLimitActions.ON_CHANGE_DIRECTION: {
      return {
        ...state,
        direction: action.payload?.direction
      }
    }
    case speedLimitActions.ON_DELETE_SPEED_LIMIT:
      return {
        ...state,
        isDeleteModal: action.payload.isOpen
      }
    case speedLimitActions.ON_ADD_SPEED_LIMIT:
      return {
        ...state,
        isAddModal: action.payload.isOpen
      }
    case speedLimitActions.ON_EDIT_SPEED_LIMIT:
      return {
        ...state,
        isEditModal: action.payload.isOpen
      }
    case speedLimitActions.ON_SELECTED_DELETE_ITEM:
      return {
        ...state,
        temporarySpeedLimitIdDelete: action.payload.temporarySpeedLimitId
      }
    // case speedLimitActions.ON_EDIT:
    //   return {
    //     ...state,
    //     edit: action.payload.isEdit
    //   }
    case speedLimitActions.ON_CHANGE_SUBDIVISION_LIST: {
      const subdivisionList = action.payload?.subdivisionData?.map(
        (item: any) => {
          return {
            subdivision: item?.subdivision,
            startMile: item?.start_mile,
            endMile: item?.end_mile
          }
        }
      )
      return {
        ...state,
        subdivisionList,
        subdivision: subdivisionList?.[0]?.subdivision,
        startLimit: subdivisionList?.[0]?.startMile,
        endLimit: subdivisionList?.[0]?.endMile
      }
    }
    case speedLimitActions.ERROR_ADD_OR_EDIT: {
      return {
        ...state,
        errorModal: action.payload.error
      }
    }
    case speedLimitActions.ON_CHANGE_START_MP: {
      return {
        ...state,
        startMile: action.payload?.startMp
      }
    }
    case speedLimitActions.ON_CHANGE_END_MP: {
      return {
        ...state,
        endMile: action.payload?.endMp
      }
    }
    case speedLimitActions.ON_CHANGE_SUBDIVISION: {
      const subdivisionSelectedData = state?.subdivisionList?.find(
        (item: any) => {
          return item?.subdivision === action.payload?.subdivision
        }
      )

      // const { startMile, endMile } = subdivisionSelectedData
      const startMile = subdivisionSelectedData?.startMile
      const endMile = subdivisionSelectedData?.endMile

      return {
        ...state,
        subdivision: action.payload?.subdivision,
        startLimit: startMile,
        endLimit: endMile
      }
    }
    case speedLimitActions.ON_ADD_SPEED_LIMIT_DATA: {
      return {
        ...state,
        addSpeedLimitData: action.payload.addSpeedLimitData
      }
    }
    case speedLimitActions.ON_EDIT_SPEED_LIMIT_DATA: {
      return {
        ...state,
        editSpeedLimitData: action.payload.editSpeedLimitData
      }
    }
    case speedLimitActions.ON_TSO_SPEED_LIMIT_SELECTED: {
      return {
        ...state,
        TSOSpeedLimitSelected: action.payload.TSOSpeedLimit
      }
    }
    default:
      break
  }
  return state
}

const actionCreators: (
  dispatch: (obj: reducerActionProps) => void
) => actionCreatorsProps = dispatch => {
  return {
    onInitStates: initData => {
      dispatch({
        type: speedLimitActions.INITIALIZE_STATES,
        payload: { initData }
      })
    },
    onInitRoutes: initRoutes => {
      dispatch({
        type: speedLimitActions.INITIALIZE_ROUTES,
        payload: { initRoutes }
      })
    },
    onChangeType: type => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_TYPE,
        payload: { type }
      })
    },

    onChangeActionType: actionType => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_ACTION_TYPE,
        payload: {
          actionType
        }
      })
    },
    onChangePage: selectedPage => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_PAGE,
        payload: { selectedPage }
      })
    },
    onSelectedItem: ({ selectedItemData, selectedIndex }) => {
      dispatch({
        type: speedLimitActions.ON_SELECTED_ITEM,
        payload: { selectedItemData, selectedIndex }
      })
    },
    onChangeRouteId: routeId => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_ROUTE_ID,
        payload: { routeId }
      })
    },
    onChangeDirection: direction => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_DIRECTION,
        payload: { direction }
      })
    },
    onChangeIsOpenModalDelete: isOpen => {
      dispatch({
        type: speedLimitActions.ON_DELETE_SPEED_LIMIT,
        payload: { isOpen }
      })
    },
    onChangeIsOpenModalAdd: isOpen => {
      dispatch({
        type: speedLimitActions.ON_ADD_SPEED_LIMIT,
        payload: { isOpen }
      })
    },
    onChangeIsOpenModalEdit: isOpen => {
      dispatch({
        type: speedLimitActions.ON_EDIT_SPEED_LIMIT,
        payload: { isOpen }
      })
    },
    onSelectedDeleteItem: temporarySpeedLimitId => {
      dispatch({
        type: speedLimitActions.ON_SELECTED_DELETE_ITEM,
        payload: { temporarySpeedLimitId }
      })
    },
    onChangeSubdivisionList: subdivisionData => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_SUBDIVISION_LIST,
        payload: { subdivisionData }
      })
    },
    onIsOpenError: error => {
      dispatch({
        type: speedLimitActions.ERROR_ADD_OR_EDIT,
        payload: { error }
      })
    },
    onChangeStartMP: startMp => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_START_MP,
        payload: { startMp }
      })
    },
    onChangeEndMP: endMp => {
      dispatch({
        type: speedLimitActions.ON_CHANGE_END_MP,
        payload: { endMp }
      })
    },
    onChangeSubdivision(subdivision) {
      dispatch({
        type: speedLimitActions.ON_CHANGE_SUBDIVISION,
        payload: { subdivision }
      })
    },
    onChangeAddSpeedLimitData(addSpeedLimitData) {
      dispatch({
        type: speedLimitActions.ON_ADD_SPEED_LIMIT_DATA,
        payload: { addSpeedLimitData }
      })
    },
    onChangeEditSpeedLimitData(editSpeedLimitData) {
      dispatch({
        type: speedLimitActions.ON_EDIT_SPEED_LIMIT_DATA,
        payload: { editSpeedLimitData }
      })
    },
    onChangeTSOSpeedLimitSelected(TSOSpeedLimit) {
      dispatch({
        type: speedLimitActions.ON_TSO_SPEED_LIMIT_SELECTED,
        payload: { TSOSpeedLimit }
      })
    }
    // onEdit: isEdit => {
    //   dispatch({
    //     type: speedLimitActions.ON_EDIT,
    //     payload: { isEdit }
    //   })
    // }
  }
}

const initialState: speedLimitStates = {
  type: '',
  subdivision: '',
  subdivisionList: [],
  limit: 0,
  startMile: '',
  endMile: '',
  startLimit: '',
  endLimit: '',
  heavy: false,
  hazardous: false,
  page: '',
  actionType: ActionTypes.View,
  indexItemSelected: null,
  dataItemSelected: null,
  addSpeedLimitData: null,
  editSpeedLimitData: null,
  temporarySpeedLimitIdDelete: null,
  routes: [],
  routeId: '',
  direction: '',
  directionList: [],
  isDeleteModal: false,
  isAddModal: false,
  isEditModal: false,
  errorModal: '',
  TSOSpeedLimitSelected: null
  // edit: false
}

const useSpeedLimitReducer: () => [
  state: speedLimitStates,
  actions: actionCreatorsProps
] = () => {
  const [state, dispatch] = useReducer<Reducer<any, any>>(
    speedLimitReducer,
    initialState
  )

  const actions = actionCreators(dispatch)

  return [state, actions]
}

export { useSpeedLimitReducer }
Leave a Comment