SpeedLimitSection/reducer/index.tsx

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

const speedLimitReducer = (
  state: speedLimitStates,
  action: reducerActionProps
) => {
  switch (action.type) {
    case speedLimitActions.INITIALIZE_STATES:
      return {
        ...state
      }
    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 directionList = action.payload.routeId?.split('_')

      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_SELECTED_DELETE_ITEM:
      return {
        ...state,
        dataDeleteItemSelected: action.payload.selectedDeleteItemData
      }
    // case speedLimitActions.ON_EDIT:
    //   return {
    //     ...state,
    //     edit: action.payload.isEdit
    //   }

    default:
      break
  }
  return state
}

const actionCreators: (
  dispatch: (obj: reducerActionProps) => void
) => actionCreatorsProps = dispatch => {
  return {
    onInitStates: () => {
      dispatch({
        type: speedLimitActions.INITIALIZE_STATES,
        payload: {}
      })
    },
    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 }
      })
    },
    onSelectedDeleteItem: selectedDeleteItemData => {
      dispatch({
        type: speedLimitActions.ON_SELECTED_DELETE_ITEM,
        payload: { selectedDeleteItemData }
      })
    }

    // onEdit: isEdit => {
    //   dispatch({
    //     type: speedLimitActions.ON_EDIT,
    //     payload: { isEdit }
    //   })
    // }
  }
}

const initialState: speedLimitStates = {
  type: '',
  subdivision: '',
  limit: 0,
  mp: 0,
  toMp: 0,
  heavy: false,
  hazardous: false,
  page: '',
  actionType: ActionTypes.View,
  indexItemSelected: null,
  dataItemSelected: null,
  dataDeleteItemSelected: null,
  routeId: '',
  direction: '',
  directionList: [],
  isDeleteModal: false
  // 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