src/redux/slices/portalActionSlice.ts

mail@pastecode.io avatar
unknown
typescript
a month ago
9.0 kB
3
Indexable
Never
import {
  addNewUser,
  editUserPermission,
  endSimpleTrainConsistTrip,
  reprocessTrip,
  softDeleteTrip
} from '@GraphQL/Api'
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'

interface MutationEndTripProps {
  createdBy: string
  endDateUtc: string
  simpleTrainConsistId: string
}

export const mutationEndTrip = createAsyncThunk(
  'portalAction/mutationEndTrip',
  async (
    { createdBy, endDateUtc, simpleTrainConsistId }: MutationEndTripProps,
    thunkAPI
  ) => {
    try {
      const data = await endSimpleTrainConsistTrip({
        createdBy,
        endDateUtc,
        simpleTrainConsistId
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

interface MutationReprocessTripProps {
  requestedBy: string
  simpleTrainConsistId: string
}

export const mutationReprocessTrip = createAsyncThunk(
  'portalAction/mutationReprocessTrip',
  async (
    { requestedBy, simpleTrainConsistId }: MutationReprocessTripProps,
    thunkAPI
  ) => {
    try {
      const data = await reprocessTrip({ requestedBy, simpleTrainConsistId })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

interface MutationEditUserPermissionProps {
  userId: string
  railroadId: string
  newRoleBitmask: number
  newIsTest: boolean
}

export const mutationEditUserPermission = createAsyncThunk(
  'portalAction/mutationEditUserPermission',
  async (
    {
      userId,
      railroadId,
      newRoleBitmask,
      newIsTest
    }: MutationEditUserPermissionProps,
    thunkAPI
  ) => {
    try {
      const data = await editUserPermission({
        userId,
        railroadId,
        newRoleBitmask,
        newIsTest
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

interface MutationAddNewUserProps {
  email: string
  password: string
  railroad_id: string
  role_bitmask: number
  is_test: boolean
}

export const mutationAddNewUser = createAsyncThunk(
  'portalAction/mutationAddNewUser',
  async (
    {
      email,
      password,
      railroad_id,
      role_bitmask,
      is_test
    }: MutationAddNewUserProps,
    thunkAPI
  ) => {
    try {
      const data = await addNewUser({
        email: email,
        password: password,
        railroad_id: railroad_id,
        role_bitmask: role_bitmask,
        is_test: is_test
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

interface MutationSoftDeleteTripProps {
  context: 'ecorail' | 'analytics'
  tripId: string
  softDelete: boolean
}

export const mutationSoftDeleteTrip = createAsyncThunk(
  'portalAction/mutationSoftDeleteTrip',
  async (
    { tripId, softDelete, context }: MutationSoftDeleteTripProps,
    thunkAPI
  ) => {
    try {
      const data = await softDeleteTrip({
        context,
        tripId,
        softDelete
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const mutationAddNewZoneSpeedLimit = createAsyncThunk(
  'portalAction/mutationAddNewZoneSpeedLimit',
  async () =>
    // { tripId, softDelete, context }: MutationSoftDeleteTripProps,
    // thunkAPI
    {
      try {
        return { status: 'ok' }
      } catch (error) {
        return error
      }
    }
)
export const mutationEditZoneSpeedLimit = createAsyncThunk(
  'portalAction/mutationEditZoneSpeedLimit',
  async () =>
    // { tripId, softDelete, context }: MutationSoftDeleteTripProps,
    // thunkAPI
    {
      try {
        return { status: 'ok' }
      } catch (error) {
        return error
      }
    }
)
export const mutationDeleteZoneSpeedLimit = createAsyncThunk(
  'portalAction/mutationDeleteZoneSpeedLimit',
  async () =>
    // { tripId, softDelete, context }: MutationSoftDeleteTripProps,
    // thunkAPI
    {
      try {
        return { status: 'ok' }
      } catch (error) {
        return error
      }
    }
)

const generateAsyncReducers = (currentFunction: any, dataKey: any) => ({
  [currentFunction.pending]: (state: any, action: any) => {
    state[dataKey].isSuccess = undefined
    state[dataKey].isError = undefined
    state[dataKey].loading = true
  },
  [currentFunction.rejected]: (state: any, action: any) => {
    state[dataKey].data = null
    state[dataKey].loading = false
    state[dataKey].isSuccess = false
    state[dataKey].isError = true
    state[dataKey].error.details = action.payload.errors
  },
  [currentFunction.fulfilled]: (state: any, action: any) => {
    state[dataKey].loading = false
    state[dataKey].isSuccess = true
    state[dataKey].isError = false
    state[dataKey].data = action.payload
    state[dataKey].error = {}
  }
})

const portalActionSlice = createSlice({
  name: 'portalAction',
  initialState: {
    endTrip: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    reprocessTrip: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    editUserPermission: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    addNewUser: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    softDeleteTrip: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    addNewZoneSpeedLimit: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    editZoneSpeedLimit: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    },
    deleteZoneSpeedLimit: {
      data: null,
      loading: false,
      isSuccess: undefined,
      isError: undefined,
      error: {}
    }
  },
  reducers: {
    resetMutationEndTrip: state => {
      state.endTrip = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationReprocessTrip: state => {
      state.reprocessTrip = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationEditUserPermission: state => {
      state.editUserPermission = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationAddNewUser: state => {
      state.addNewUser = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationSoftDeleteTrip: state => {
      state.softDeleteTrip = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationAddNewZoneSpeedLimit: state => {
      state.addNewZoneSpeedLimit = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationEditZoneSpeedLimit: state => {
      state.editZoneSpeedLimit = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    },
    resetMutationDeleteZoneSpeedLimit: state => {
      state.deleteZoneSpeedLimit = {
        data: null,
        loading: false,
        isSuccess: undefined,
        isError: undefined,
        error: {}
      }
    }
  },
  extraReducers: {
    ...generateAsyncReducers(mutationEndTrip, 'endTrip'),
    ...generateAsyncReducers(mutationReprocessTrip, 'reprocessTrip'),
    ...generateAsyncReducers(mutationEditUserPermission, 'editUserPermission'),
    ...generateAsyncReducers(mutationAddNewUser, 'addNewUser'),
    ...generateAsyncReducers(mutationSoftDeleteTrip, 'softDeleteTrip'),
    ...generateAsyncReducers(
      mutationAddNewZoneSpeedLimit,
      'addNewZoneSpeedLimit'
    ),
    ...generateAsyncReducers(mutationEditZoneSpeedLimit, 'editZoneSpeedLimit'),
    ...generateAsyncReducers(
      mutationDeleteZoneSpeedLimit,
      'deleteZoneSpeedLimit'
    )
  }
})

export const {
  resetMutationEndTrip,
  resetMutationReprocessTrip,
  resetMutationEditUserPermission,
  resetMutationAddNewUser,
  resetMutationSoftDeleteTrip,
  resetMutationAddNewZoneSpeedLimit,
  resetMutationEditZoneSpeedLimit,
  resetMutationDeleteZoneSpeedLimit
} = portalActionSlice.actions
export default portalActionSlice.reducer
Leave a Comment