UNPKG

@open-tender/cloud

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our cloud-based Order API.

58 lines (57 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerHistoryReducer = exports.selectCustomerHistory = exports.resetCustomerHistory = exports.fetchCustomerHistory = exports.CustomerHistoryActionType = void 0; const tslib_1 = require("tslib"); const toolkit_1 = require("@reduxjs/toolkit"); const types_1 = require("../types"); const types_2 = require("@open-tender/types"); const account_1 = require("./account"); const initialState = { entities: [], error: null, loading: 'idle' }; var CustomerHistoryActionType; (function (CustomerHistoryActionType) { CustomerHistoryActionType["FetchHistory"] = "history/fetchCustomerHistory"; })(CustomerHistoryActionType = exports.CustomerHistoryActionType || (exports.CustomerHistoryActionType = {})); exports.fetchCustomerHistory = (0, toolkit_1.createAsyncThunk)(CustomerHistoryActionType.FetchHistory, (_, { getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const { api } = getState().config; if (!api) return; const token = (0, account_1.selectToken)(getState()); if (!token) throw new Error(types_2.MISSING_CUSTOMER); return yield api.getCustomerTplsHistory(token); } catch (err) { return rejectWithValue(err); } })); const historySlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.History, initialState, reducers: { resetCustomerHistory: () => initialState }, extraReducers: builder => { builder .addCase(exports.fetchCustomerHistory.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerHistory.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerHistory.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); exports.resetCustomerHistory = historySlice.actions.resetCustomerHistory; const selectCustomerHistory = (state) => state.customer.history; exports.selectCustomerHistory = selectCustomerHistory; exports.customerHistoryReducer = historySlice.reducer;