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.

99 lines (98 loc) 4.41 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.customerOrderReducer = exports.selectCustomerOrder = exports.setCustomerOrder = exports.resetCustomerOrderError = exports.resetCustomerOrder = exports.updateCustomerOrderRating = exports.fetchCustomerOrder = exports.CustomerOrderActionType = 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 initialState = { entity: null, error: null, loading: 'idle' }; var CustomerOrderActionType; (function (CustomerOrderActionType) { CustomerOrderActionType["FetchCustomerOrder"] = "customer/fetchCustomerOrder"; CustomerOrderActionType["UpdateCustomerOrderRating"] = "customer/updateCustomerOrderRating"; })(CustomerOrderActionType = exports.CustomerOrderActionType || (exports.CustomerOrderActionType = {})); exports.fetchCustomerOrder = (0, toolkit_1.createAsyncThunk)(CustomerOrderActionType.FetchCustomerOrder, (orderId, { 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 order = yield api.getCustomerOrder(token, orderId); return order; } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); exports.updateCustomerOrderRating = (0, toolkit_1.createAsyncThunk)(CustomerOrderActionType.UpdateCustomerOrderRating, ({ orderId, 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); yield api.putCustomerOrderRating(token, orderId, data); dispatch((0, exports.fetchCustomerOrder)(orderId)); dispatch((0, notifications_1.showNotification)('Rating updated!')); } catch (err) { const error = err; return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error)); } })); const customerOrderSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.Order, initialState, reducers: { resetCustomerOrder: () => initialState, resetCustomerOrderError: state => { state.error = null; state.loading = 'idle'; }, setCustomerOrder: (state, action) => { state.entity = action.payload; state.error = null; } }, extraReducers: builder => { builder .addCase(exports.fetchCustomerOrder.fulfilled, (state, action) => { state.entity = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchCustomerOrder.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchCustomerOrder.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }) .addCase(exports.updateCustomerOrderRating.fulfilled, state => { state.loading = 'idle'; state.error = null; }) .addCase(exports.updateCustomerOrderRating.pending, state => { state.loading = 'pending'; }) .addCase(exports.updateCustomerOrderRating.rejected, (state, action) => { state.error = action.payload; state.loading = 'idle'; }); } }); _a = customerOrderSlice.actions, exports.resetCustomerOrder = _a.resetCustomerOrder, exports.resetCustomerOrderError = _a.resetCustomerOrderError, exports.setCustomerOrder = _a.setCustomerOrder; exports.selectCustomerOrder = (0, toolkit_1.createSelector)((state) => { const { entity: order, loading, error } = state.customer.order; return { order, loading, error }; }, ({ order, loading, error }) => { return { order, loading, error }; }); exports.customerOrderReducer = customerOrderSlice.reducer;