src/redux/slices/portalDataSlice.ts

mail@pastecode.io avatar
unknown
typescript
25 days ago
12 kB
2
Indexable
Never
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import {
  getLatestDate,
  getDailyDaysResults,
  getTrendingDaysResults,
  getFuelMileResults,
  getDailyFuelMileResults,
  getLocoInfractions,
  getTripFuelSaving,
  getTrips,
  getTripDetails,
  getFuelSavingHeroMetrics,
  getFuelSavingTrends,
  getRoutesByRailroad,
  getTrip,
  getUsersAndRolesbyEmail,
  getBroadcastDataByRailroadId,
  getTripByStcId,
  getStationsByRouteId,
  GetLocalizationUnits,
  getZoneSpeedLimitsByRouteId,
  getTemporaryZoneSpeedLimitsByRouteId
} from '@GraphQL/Api'
import { string } from 'prop-types'

interface paramProps {
  version?: string
  companyId?: string
  email?: string
  date?: string
  inputLocomotiveNum?: string
  selectedCompanyId?: string
  startDate?: Date | null | string
  endDate?: Date | null | string
  locoNum?: string
  crewId?: string
  trainId?: string
  railroadId?: string
  routeId?: string
  tripId?: string
  stcId?: string
  route_id?: string | null
  route_direction?: string | null
  startDestinationId?: string | null
  endDestinationId?: string | null
}

export const fetchLatestDate = createAsyncThunk(
  'portalData/fetchLatestDate',
  async (_, thunkAPI) => {
    try {
      const data = await getLatestDate()
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchDailyDayResults = createAsyncThunk(
  'portalData/fetchDailyDayResults',
  async ({ companyId, date }: paramProps, thunkAPI) => {
    try {
      const data = await getDailyDaysResults(companyId, date)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchTrendingDayResults = createAsyncThunk(
  'portalData/fetchTrendingDayResults',
  async ({ companyId, startDate, endDate }: paramProps, thunkAPI) => {
    try {
      const data = await getTrendingDaysResults(companyId, startDate, endDate)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchDailyFuelMileResults = createAsyncThunk(
  'portalData/fetchDailyFuelMileResults',
  async ({ companyId, date }: paramProps, thunkAPI) => {
    try {
      const data = await getDailyFuelMileResults(companyId, date)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchFuelMileResults = createAsyncThunk(
  'portalData/fetchFuelMileResults',
  async ({ companyId, startDate, endDate }: paramProps, thunkAPI) => {
    try {
      const data = await getFuelMileResults(companyId, startDate, endDate)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchLocoInfractions = createAsyncThunk(
  'portalData/fetchLocoInfractions',
  async ({ companyId, locoNum, startDate, endDate }: paramProps, thunkAPI) => {
    try {
      let data = await getLocoInfractions(
        companyId,
        locoNum,
        startDate,
        endDate
      )

      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchTripFuelSaving = createAsyncThunk(
  'portalData/fetchTripFuelSaving',
  async ({ companyId, locoNum, startDate, endDate }: paramProps, thunkAPI) => {
    try {
      const data = await getTripFuelSaving(
        companyId,
        locoNum,
        startDate,
        endDate
      )
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchTrips = createAsyncThunk(
  'portalData/fetchTrips',
  async ({ railroadId, startDate, endDate }: paramProps, thunkAPI) => {
    try {
      const data = await getTrips(railroadId, startDate, endDate)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchTripDetails = createAsyncThunk(
  'portalData/fetchTripDetails',
  async ({ tripId }: paramProps, thunkAPI) => {
    try {
      const data = await getTripDetails(tripId)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchFuelSavingHeroMetrics = createAsyncThunk(
  'portalData/fetchFuelSavingHeroMetrics',
  async (
    { railroadId, startDate, endDate, route_id, route_direction }: paramProps,
    thunkAPI
  ) => {
    try {
      const data = await getFuelSavingHeroMetrics(
        railroadId,
        startDate,
        endDate,
        route_id,
        route_direction
      )
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchFuelSavingTrends = createAsyncThunk(
  'portalData/fetchFuelSavingTrends',
  async (
    { railroadId, startDate, endDate, route_id, route_direction }: paramProps,
    thunkAPI
  ) => {
    try {
      const data = await getFuelSavingTrends(
        railroadId,
        startDate,
        endDate,
        route_id,
        route_direction
      )
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchRoutesByRailroad = createAsyncThunk(
  'portalData/fetchRoutesByRailroad',
  async ({ railroadId }: paramProps, thunkAPI) => {
    try {
      const data = await getRoutesByRailroad(railroadId)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchTrip = createAsyncThunk(
  'portalData/fetchTrip',
  async ({ tripId, stcId }: paramProps, thunkAPI) => {
    try {
      const callFuction = tripId
        ? () => getTrip(tripId)
        : () => getTripByStcId(stcId)
      const data = await callFuction()
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchUsersAndRolesByEmail = createAsyncThunk(
  'portalData/fetchUsersAndRolesByEmail',
  async ({ email }: paramProps, thunkAPI) => {
    try {
      const data = await getUsersAndRolesbyEmail(email)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchStationsByRouteId = createAsyncThunk(
  'portalData/fetchStationsByRouteId',
  async ({ railroadId, routeId }: paramProps, thunkAPI) => {
    try {
      const data = await getStationsByRouteId({
        railroadId: railroadId,
        routeId: routeId
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchBroadcastDataByRailroadId = createAsyncThunk(
  'portalData/fetchBroadcastDataByRailroadId',
  async ({ railroadId }: paramProps, thunkAPI) => {
    try {
      const data = await getBroadcastDataByRailroadId({
        railroadId: railroadId
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchLocalizationUnits = createAsyncThunk(
  'portalData/fetchLocalizationUnits',
  async ({ railroadId }: paramProps, thunkAPI) => {
    try {
      const data = await GetLocalizationUnits(railroadId)
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

export const fetchZoneSpeedLimitsByRouteId = createAsyncThunk(
  'portalData/fetchZoneSpeedLimitsByRouteId',
  async (
    { railroadId, routeId, startDestinationId, endDestinationId }: paramProps,
    thunkAPI
  ) => {
    try {
      const data = await getTemporaryZoneSpeedLimitsByRouteId({
        routeId: routeId,
        railroadId: railroadId,
        startDestinationId: startDestinationId,
        endDestinationId: endDestinationId
      })
      return data
    } catch (error) {
      return thunkAPI.rejectWithValue(error)
    }
  }
)

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

export const portalDataSlice = createSlice({
  name: 'portalData',
  initialState: {
    latestDate: {
      loading: false,
      error: {},
      data: null
    },
    dailyDayResults: {
      loading: false,
      error: {},
      data: null
    },
    trendingDayResults: {
      loading: false,
      error: {},
      data: null
    },
    dailyFuelMileResults: {
      loading: false,
      error: {},
      data: null
    },
    fuelMileResults: {
      loading: false,
      error: {},
      data: null
    },
    locoInfractions: {
      loading: false,
      error: {},
      data: null
    },
    tripFuelSaving: {
      loading: false,
      error: {},
      data: null
    },
    trips: {
      loading: false,
      error: {},
      data: null
    },
    tripDetails: {
      loading: false,
      error: {},
      data: null
    },
    fuelSavingHeroMetrics: {
      loading: false,
      error: {},
      data: null
    },
    fuelSavingTrends: {
      loading: false,
      error: {},
      data: null
    },
    routesByRailroad: {
      loading: false,
      error: {},
      data: null
    },
    trip: {
      loading: false,
      error: {},
      data: null
    },
    usersAndRolesByEmail: {
      loading: false,
      error: {},
      data: null
    },
    stationsByRouteId: {
      loading: false,
      error: {},
      data: null
    },
    broadcastDataByRailroadId: {
      loading: false,
      error: {},
      data: null
    },
    localizationUnits: {
      loading: false,
      error: {},
      data: null
    },
    zoneSpeedLimitsByRouteId: {
      loading: false,
      error: {},
      data: null
    }
  },
  reducers: {},
  extraReducers: {
    ...generateAsyncReducers(fetchLatestDate, 'latestDate'),
    ...generateAsyncReducers(fetchDailyDayResults, 'dailyDayResults'),
    ...generateAsyncReducers(fetchTrendingDayResults, 'trendingDayResults'),
    ...generateAsyncReducers(fetchDailyFuelMileResults, 'dailyFuelMileResults'),
    ...generateAsyncReducers(fetchFuelMileResults, 'fuelMileResults'),
    ...generateAsyncReducers(fetchLocoInfractions, 'locoInfractions'),
    ...generateAsyncReducers(fetchTripFuelSaving, 'tripFuelSaving'),
    ...generateAsyncReducers(fetchTrips, 'trips'),
    ...generateAsyncReducers(fetchTripDetails, 'tripDetails'),
    ...generateAsyncReducers(
      fetchFuelSavingHeroMetrics,
      'fuelSavingHeroMetrics'
    ),
    ...generateAsyncReducers(fetchFuelSavingTrends, 'fuelSavingTrends'),
    ...generateAsyncReducers(fetchRoutesByRailroad, 'routesByRailroad'),
    ...generateAsyncReducers(fetchTrip, 'trip'),
    ...generateAsyncReducers(fetchUsersAndRolesByEmail, 'usersAndRolesByEmail'),
    ...generateAsyncReducers(fetchStationsByRouteId, 'stationsByRouteId'),
    ...generateAsyncReducers(
      fetchBroadcastDataByRailroadId,
      'broadcastDataByRailroadId'
    ),
    ...generateAsyncReducers(fetchLocalizationUnits, 'localizationUnits'),
    ...generateAsyncReducers(
      fetchZoneSpeedLimitsByRouteId,
      'zoneSpeedLimitsByRouteId'
    )
  }
})

export default portalDataSlice.reducer
Leave a Comment