@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.
92 lines (91 loc) • 3.83 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.guestReducer = exports.selectGuest = exports.setGuestEmail = exports.resetGuestErrors = exports.resetGuest = exports.fetchGuestThanx = exports.fetchGuest = exports.GuestActionType = void 0;
const tslib_1 = require("tslib");
const toolkit_1 = require("@reduxjs/toolkit");
const types_1 = require("./types");
const order_1 = require("./order");
const initialState = {
brands: null,
email: null,
error: null,
loading: 'idle',
showMagicLink: false
};
var GuestActionType;
(function (GuestActionType) {
GuestActionType["FetchGuest"] = "guest/fetchGuest";
GuestActionType["FetchGuestThanx"] = "guest/fetchGuestThanx";
})(GuestActionType = exports.GuestActionType || (exports.GuestActionType = {}));
exports.fetchGuest = (0, toolkit_1.createAsyncThunk)(GuestActionType.FetchGuest, ({ email, callback }, { getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { api } = getState().config;
if (!api)
return;
try {
const response = yield api.getGuest(email);
if (callback)
callback();
return response;
}
catch (err) {
const error = { error: err, email };
return rejectWithValue(error);
}
}));
exports.fetchGuestThanx = (0, toolkit_1.createAsyncThunk)(GuestActionType.FetchGuestThanx, ({ email, callback }, { getState, rejectWithValue, dispatch }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { api } = getState().config;
if (!api)
return;
try {
yield api.postThanxLogin(email, origin);
if (callback)
callback();
dispatch((0, order_1.addMessage)('Thanks! Please check your email on this device.'));
return { email: email };
}
catch (err) {
const error = { error: err, email };
return rejectWithValue(error);
}
}));
const guestSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.Guest,
initialState,
reducers: {
resetGuest: () => initialState,
resetGuestErrors: state => {
state.error = null;
state.loading = 'idle';
state.showMagicLink = false;
},
setGuestEmail: (state, action) => {
state.email = action.payload;
}
},
extraReducers: builder => {
builder
.addCase(exports.fetchGuest.fulfilled, (state, action) => {
return Object.assign(Object.assign(Object.assign({}, state), { loading: 'idle', errors: null }), action.payload);
})
.addCase(exports.fetchGuest.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchGuest.rejected, (state, action) => {
return Object.assign(Object.assign(Object.assign({}, state), { loading: 'idle', brands: null }), action.payload);
})
.addCase(exports.fetchGuestThanx.fulfilled, (state, action) => {
return Object.assign(Object.assign(Object.assign({}, state), { loading: 'idle', errors: null, showMagicLink: true }), action.payload);
})
.addCase(exports.fetchGuestThanx.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchGuestThanx.rejected, (state, action) => {
return Object.assign(Object.assign(Object.assign({}, state), { loading: 'idle', showMagicLink: false, brands: null }), action.payload);
});
}
});
_a = guestSlice.actions, exports.resetGuest = _a.resetGuest, exports.resetGuestErrors = _a.resetGuestErrors, exports.setGuestEmail = _a.setGuestEmail;
const selectGuest = (state) => state.guest;
exports.selectGuest = selectGuest;
exports.guestReducer = guestSlice.reducer;