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.

59 lines (58 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.giftCardsReducer = exports.selectGiftCards = exports.resetGiftCards = exports.purchaseGiftCards = exports.GiftCardsActionType = void 0; const tslib_1 = require("tslib"); const toolkit_1 = require("@reduxjs/toolkit"); const types_1 = require("./types"); const initialState = { loading: 'idle', error: null, success: false, giftCards: [] }; var GiftCardsActionType; (function (GiftCardsActionType) { GiftCardsActionType["PurchaseGiftCards"] = "giftCards/purchaseGiftCards"; })(GiftCardsActionType = exports.GiftCardsActionType || (exports.GiftCardsActionType = {})); exports.purchaseGiftCards = (0, toolkit_1.createAsyncThunk)(GiftCardsActionType.PurchaseGiftCards, ({ data, callback }, { getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { api } = getState().config; if (!api) return; try { const giftCards = yield api.postPurchaseGiftCards(data); if (callback) callback(); return giftCards; } catch (err) { return rejectWithValue(err); } })); const giftCardsSlice = (0, toolkit_1.createSlice)({ name: types_1.ReducerType.GiftCards, initialState, reducers: { resetGiftCards: () => initialState }, extraReducers: builder => { builder .addCase(exports.purchaseGiftCards.fulfilled, (state, action) => { state.success = true; state.giftCards = action.payload; state.loading = 'idle'; state.error = null; }) .addCase(exports.purchaseGiftCards.pending, state => { state.loading = 'pending'; }) .addCase(exports.purchaseGiftCards.rejected, (state, action) => { state.loading = 'idle'; state.success = false; state.error = action.payload; }); } }); exports.resetGiftCards = giftCardsSlice.actions.resetGiftCards; const selectGiftCards = (state) => state.giftCards; exports.selectGiftCards = selectGiftCards; exports.giftCardsReducer = giftCardsSlice.reducer;