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.

174 lines (173 loc) 8.34 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerFavoritesReducer = exports.selectCustomerFavorites = exports.setCustomerFavoritesLookup = exports.setCustomerFavorites = exports.resetCustomerFavoritesError = exports.resetCustomerFavorites = exports.addCustomerFavorite = exports.removeCustomerFavorite = exports.updateCustomerFavorite = exports.fetchCustomerFavorites = exports.CustomerFavoritesActionType = 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 notifications_1 = require("../notifications"); const utils_1 = require("@open-tender/utils"); const initialState = { entities: [], error: null, loading: 'idle', lookup: {} }; var CustomerFavoritesActionType; (function (CustomerFavoritesActionType) { CustomerFavoritesActionType["FetchCustomerFavorites"] = "customer/fetchCustomerFavorites"; CustomerFavoritesActionType["UpdateCustomerFavorite"] = "customer/updateCustomerFavorite"; CustomerFavoritesActionType["RemoveCustomerFavorite"] = "customer/removeCustomerFavorite"; CustomerFavoritesActionType["AddCustomerFavorite"] = "customer/addCustomerFavorite"; })(CustomerFavoritesActionType = exports.CustomerFavoritesActionType || (exports.CustomerFavoritesActionType = {})); exports.fetchCustomerFavorites = (0, toolkit_1.createAsyncThunk)(CustomerFavoritesActionType.FetchCustomerFavorites, (_, { 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 { data: favorites } = yield api.getCustomerFavorites(token); const lookup = (0, utils_1.makeFavoritesLookup)(favorites); dispatch((0, exports.setCustomerFavoritesLookup)(lookup)); return favorites; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.updateCustomerFavorite = (0, toolkit_1.createAsyncThunk)(CustomerFavoritesActionType.UpdateCustomerFavorite, (requestData, { 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); yield api.putCustomerFavorite(token, requestData.favoriteId, requestData.data); const { data: favorites } = yield api.getCustomerFavorites(token); const lookup = (0, utils_1.makeFavoritesLookup)(favorites); dispatch((0, exports.setCustomerFavoritesLookup)(lookup)); dispatch((0, notifications_1.showNotification)('Favorite updated!')); if (requestData.callback) requestData.callback(); return favorites; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.removeCustomerFavorite = (0, toolkit_1.createAsyncThunk)(CustomerFavoritesActionType.RemoveCustomerFavorite, (requestData, { 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); yield api.deleteCustomerFavorite(token, requestData.favoriteId); const { data: favorites } = yield api.getCustomerFavorites(token); const lookup = (0, utils_1.makeFavoritesLookup)(favorites); dispatch((0, exports.setCustomerFavoritesLookup)(lookup)); if (requestData.callback) requestData.callback(); return favorites; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.addCustomerFavorite = (0, toolkit_1.createAsyncThunk)(CustomerFavoritesActionType.AddCustomerFavorite, ({ item, name, callback }, { 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 data = { cart: item, name: name || '' }; yield api.postCustomerFavorite(token, data); const { data: favorites } = yield api.getCustomerFavorites(token); const lookup = (0, utils_1.makeFavoritesLookup)(favorites); dispatch((0, exports.setCustomerFavoritesLookup)(lookup)); if (callback) callback(); return favorites; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const customerFavoritesSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Favorites, initialState, reducers: { resetCustomerFavorites: () => initialState, resetCustomerFavoritesError: state => { state.error = null; state.loading = 'idle'; }, setCustomerFavorites: (state, action) => { state.entities = action.payload; state.error = null; }, setCustomerFavoritesLookup: (state, action) => { state.lookup = action.payload; state.error = null; } }, extraReducers: builder => { builder .addCase(exports.fetchCustomerFavorites.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerFavorites.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerFavorites.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.updateCustomerFavorite.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.updateCustomerFavorite.pending, state => { state.loading = 'pending'; }) .addCase(exports.updateCustomerFavorite.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.removeCustomerFavorite.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.removeCustomerFavorite.pending, state => { state.loading = 'pending'; }) .addCase(exports.removeCustomerFavorite.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.addCustomerFavorite.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.addCustomerFavorite.pending, state => { state.loading = 'pending'; }) .addCase(exports.addCustomerFavorite.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = customerFavoritesSlice.actions, exports.resetCustomerFavorites = _a.resetCustomerFavorites, exports.resetCustomerFavoritesError = _a.resetCustomerFavoritesError, exports.setCustomerFavorites = _a.setCustomerFavorites, exports.setCustomerFavoritesLookup = _a.setCustomerFavoritesLookup; const selectCustomerFavorites = (state) => state.customer.favorites; exports.selectCustomerFavorites = selectCustomerFavorites; exports.customerFavoritesReducer = customerFavoritesSlice.reducer;