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.

95 lines (94 loc) 4.34 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerAllergensReducer = exports.selectCustomerAllergens = exports.setCustomerAllergens = exports.resetCustomerAllergens = exports.updateCustomerAllergens = exports.fetchCustomerAllergens = exports.CustomerAllergensActionType = 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 allergens_1 = require("../allergens"); const notifications_1 = require("../notifications"); const initialState = { entities: [], lookup: {}, loading: 'idle', error: null }; var CustomerAllergensActionType; (function (CustomerAllergensActionType) { CustomerAllergensActionType["FetchCustomerAllergens"] = "customer/fetchCustomerAllergens"; CustomerAllergensActionType["UpdateCustomerAllergens"] = "customer/updateCustomerAllergens"; })(CustomerAllergensActionType = exports.CustomerAllergensActionType || (exports.CustomerAllergensActionType = {})); exports.fetchCustomerAllergens = (0, toolkit_1.createAsyncThunk)(CustomerAllergensActionType.FetchCustomerAllergens, (_, { 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 allergens = yield api.getCustomerAllergens(token); return allergens; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.updateCustomerAllergens = (0, toolkit_1.createAsyncThunk)(CustomerAllergensActionType.UpdateCustomerAllergens, (data, { 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 allergens = yield api.putCustomerAllergens(token, data); dispatch((0, allergens_1.setSelectedAllergens)(allergens || [])); dispatch((0, notifications_1.showNotification)('Allergens updated!')); return allergens; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const customerAllergensSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.CustomerAllergens, initialState, reducers: { resetCustomerAllergens: () => initialState, setCustomerAllergens: (state, action) => { state.entities = action.payload; state.error = null; } }, extraReducers: builder => { builder .addCase(exports.fetchCustomerAllergens.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerAllergens.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerAllergens.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.updateCustomerAllergens.fulfilled, (state, action) => { state.entities = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.updateCustomerAllergens.pending, state => { state.loading = 'pending'; }) .addCase(exports.updateCustomerAllergens.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = customerAllergensSlice.actions, exports.resetCustomerAllergens = _a.resetCustomerAllergens, exports.setCustomerAllergens = _a.setCustomerAllergens; const selectCustomerAllergens = (state) => state.customer.allergens; exports.selectCustomerAllergens = selectCustomerAllergens; exports.customerAllergensReducer = customerAllergensSlice.reducer;