UNPKG

@open-tender/store

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API

60 lines (59 loc) 2.37 kB
import { __awaiter, __generator } from "tslib"; import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; import { ReducerType } from './types'; var initialState = { data: null, loading: 'idle', error: null }; export var CustomerIdentifyActionType; (function (CustomerIdentifyActionType) { CustomerIdentifyActionType["FetchCustomerIdentify"] = "CustomerIdentify/fetchCustomerIdentify"; })(CustomerIdentifyActionType || (CustomerIdentifyActionType = {})); export var fetchCustomerIdentify = createAsyncThunk(CustomerIdentifyActionType.FetchCustomerIdentify, function (data_1, _a) { return __awaiter(void 0, [data_1, _a], void 0, function (data, _b) { var api, err_1; var getState = _b.getState, rejectWithValue = _b.rejectWithValue; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 2, , 3]); api = getState().config.api; return [4 /*yield*/, api.postIdentifyCustomer(data)]; case 1: return [2 /*return*/, _c.sent()]; case 2: err_1 = _c.sent(); return [2 /*return*/, rejectWithValue(err_1)]; case 3: return [2 /*return*/]; } }); }); }); var customerIdentifySlice = createSlice({ name: ReducerType.CustomerIdentify, initialState: initialState, reducers: { resetCustomerIdentify: function () { return initialState; } }, extraReducers: function (builder) { builder .addCase(fetchCustomerIdentify.fulfilled, function (state, action) { state.data = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(fetchCustomerIdentify.pending, function (state) { state.loading = 'pending'; }) .addCase(fetchCustomerIdentify.rejected, function (state, action) { state.error = action.payload; state.loading = 'idle'; }); } }); export var resetCustomerIdentify = customerIdentifySlice.actions.resetCustomerIdentify; export var selectCustomerIdentify = function (state) { return state.customerIdentify; }; export var selectCustomerIdentified = function (state) { return state.customerIdentify.data; }; export var customerIdentifyReducer = customerIdentifySlice.reducer;