@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.
68 lines (67 loc) • 3.18 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.customerHouseAccountsReducer = exports.selectCustomerHouseAccounts = exports.setCustomerHouseAccounts = exports.resetCustomerHouseAccountsError = exports.resetCustomerHouseAccounts = exports.fetchCustomerHouseAccounts = exports.CustomerHouseAccountsActionType = 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',
lookup: {}
};
var CustomerHouseAccountsActionType;
(function (CustomerHouseAccountsActionType) {
CustomerHouseAccountsActionType["FetchCustomerHouseAccounts"] = "customer/fetchCustomerHouseAccounts";
})(CustomerHouseAccountsActionType = exports.CustomerHouseAccountsActionType || (exports.CustomerHouseAccountsActionType = {}));
exports.fetchCustomerHouseAccounts = (0, toolkit_1.createAsyncThunk)(CustomerHouseAccountsActionType.FetchCustomerHouseAccounts, (_, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const api = getState().config.api;
const token = (0, account_1.selectToken)(getState());
if (!token)
throw new Error(types_2.MISSING_CUSTOMER);
const houseAccounts = yield api.getCustomerHouseAccounts(token);
return houseAccounts;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
const customerHouseAccountsSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.HouseAccounts,
initialState,
reducers: {
resetCustomerHouseAccounts: () => initialState,
resetCustomerHouseAccountsError: state => {
state.error = null;
state.loading = 'idle';
},
setCustomerHouseAccounts: (state, action) => {
state.entities = action.payload;
state.error = null;
}
},
extraReducers: builder => {
builder
.addCase(exports.fetchCustomerHouseAccounts.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.fetchCustomerHouseAccounts.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchCustomerHouseAccounts.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
});
}
});
_a = customerHouseAccountsSlice.actions, exports.resetCustomerHouseAccounts = _a.resetCustomerHouseAccounts, exports.resetCustomerHouseAccountsError = _a.resetCustomerHouseAccountsError, exports.setCustomerHouseAccounts = _a.setCustomerHouseAccounts;
const selectCustomerHouseAccounts = (state) => state.customer.houseAccounts;
exports.selectCustomerHouseAccounts = selectCustomerHouseAccounts;
exports.customerHouseAccountsReducer = customerHouseAccountsSlice.reducer;