src/redux/slices/portalDataSlice.ts
unknown
typescript
6 months ago
14 kB
5
Indexable
import { GetLocalizationUnits, getBroadcastDataByRailroadId, getDailyDaysResults, getDailyFuelMileResults, getFuelMileResults, getFuelSavingHeroMetrics, getFuelSavingTrends, getLatestDate, getLocoInfractions, getRoutesByRailroad, getSpeedLimitsDataByRouteId, getStationsByRouteId, getSubdivisionDataByRouteId, getTSODiffReport, getTrackCoordinatesByRoutesId, getTrendingDaysResults, getTrip, getTripByStcId, getTripDetails, getTripFuelSaving, getTrips, getUsersAndRolesbyEmail, parseTsoFromDocument } from '@GraphQL/Api' import { createAsyncThunk, createSlice } from '@reduxjs/toolkit' 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 documentS3Uri?: string } 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 fetchSpeedLimitsDataByRouteId = createAsyncThunk( 'portalData/fetchSpeedLimitsDataByRouteId', async ( { railroadId, routeId, startDestinationId, endDestinationId }: paramProps, thunkAPI ) => { try { const data = await getSpeedLimitsDataByRouteId({ routeId: routeId, railroadId: railroadId, startDestinationId: startDestinationId, endDestinationId: endDestinationId }) return data } catch (error) { return thunkAPI.rejectWithValue(error) } } ) export const fetchTrackCoordinatesByRouteId = createAsyncThunk( 'portalData/fetchTrackCoordinatesByRouteId', async ( { railroadId, routeId, startDestinationId, endDestinationId }: paramProps, thunkAPI ) => { try { const data = await getTrackCoordinatesByRoutesId({ routeId: routeId, railroadId: railroadId, startDestinationId: startDestinationId, endDestinationId: endDestinationId }) return data } catch (error) { return thunkAPI.rejectWithValue(error) } } ) export const fetchSubdivisionDataByRouteId = createAsyncThunk( 'portalData/fetchSubdivisionDataByRouteId', async ({ route_direction, route_id }: paramProps, thunkAPI) => { try { const data = await getSubdivisionDataByRouteId({ routeId: route_id, routeDirection: route_direction }) return data } catch (error) { return thunkAPI.rejectWithValue(error) } } ) export const fetchTSODiffReport = createAsyncThunk( 'portalData/fetchTSODiffReport', async ({ documentS3Uri, route_id }: paramProps, thunkAPI) => { try { const data = await getTSODiffReport({ documentS3Uri: documentS3Uri, routeId: route_id }) return data } catch (error) { return thunkAPI.rejectWithValue(error) } } ) export const fetchParseTsoFromDocument = createAsyncThunk( 'portalData/fetchParseTsoFromDocument', async ({ documentS3Uri, route_id }: paramProps, thunkAPI) => { try { const data = await parseTsoFromDocument({ documentS3Uri: documentS3Uri, routeId: route_id }) 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 }, speedLimitsDataByRouteId: { loading: false, error: {}, data: null }, trackCoordinatesByRouteId: { loading: false, error: {}, data: null }, subdivisionDataByRouteId: { loading: false, error: {}, data: null }, TSODiffReport: { loading: false, error: {}, data: null }, ParseTsoFromDocument: { 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( fetchSpeedLimitsDataByRouteId, 'speedLimitsDataByRouteId' ), ...generateAsyncReducers( fetchTrackCoordinatesByRouteId, 'trackCoordinatesByRouteId' ), ...generateAsyncReducers( fetchSubdivisionDataByRouteId, 'subdivisionDataByRouteId' ), ...generateAsyncReducers(fetchTSODiffReport, 'TSODiffReport'), ...generateAsyncReducers(fetchParseTsoFromDocument, 'ParseTsoFromDocument') } }) export default portalDataSlice.reducer
Editor is loading...
Leave a Comment