@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.81 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.oneTimePasscodeReducer = exports.selectOneTimePasscode = exports.toggleOtpSent = exports.resetOneTimePasscode = exports.signInCustomer = exports.sendOneTimePasscode = exports.OneTimePasscodeActionType = void 0;
const tslib_1 = require("tslib");
const toolkit_1 = require("@reduxjs/toolkit");
const types_1 = require("./types");
const customer_1 = require("./customer");
const initialState = {
otpSent: false,
loading: 'idle',
error: null
};
var OneTimePasscodeActionType;
(function (OneTimePasscodeActionType) {
OneTimePasscodeActionType["OneTimePasscodeCustomer"] = "oneTimePasscode/oneTimePasscodeCustomer";
OneTimePasscodeActionType["SignInCustomer"] = "oneTimePasscode/signInCustomer";
})(OneTimePasscodeActionType = exports.OneTimePasscodeActionType || (exports.OneTimePasscodeActionType = {}));
exports.sendOneTimePasscode = (0, toolkit_1.createAsyncThunk)(OneTimePasscodeActionType.OneTimePasscodeCustomer, ({ data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const api = getState().config.api;
const response = yield api.postCustomerSignIn(data);
if (data.one_time_passcode) {
dispatch((0, customer_1.setCustomerAuth)(response));
yield dispatch((0, customer_1.fetchCustomer)());
if (callback)
callback();
return false;
}
else {
return true;
}
}
catch (err) {
return rejectWithValue(err);
}
}));
exports.signInCustomer = (0, toolkit_1.createAsyncThunk)(OneTimePasscodeActionType.SignInCustomer, ({ data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const api = getState().config.api;
const response = yield api.postCustomerSignIn(data);
dispatch((0, customer_1.setCustomerAuth)(response));
yield dispatch((0, customer_1.fetchCustomer)());
if (callback)
callback();
return;
}
catch (err) {
return rejectWithValue(err);
}
}));
const oneTimePasscodeSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.OneTimePasscode,
initialState,
reducers: {
resetOneTimePasscode: () => initialState,
toggleOtpSent: (state, action) => {
state.otpSent = action.payload;
}
},
extraReducers: builder => {
builder
.addCase(exports.sendOneTimePasscode.fulfilled, (state, action) => {
state.otpSent = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.sendOneTimePasscode.pending, state => {
state.loading = 'pending';
})
.addCase(exports.sendOneTimePasscode.rejected, (state, action) => {
state.loading = 'idle';
state.error = action.payload;
})
.addCase(exports.signInCustomer.fulfilled, state => {
state.loading = 'idle';
state.error = null;
})
.addCase(exports.signInCustomer.pending, state => {
state.loading = 'pending';
})
.addCase(exports.signInCustomer.rejected, (state, action) => {
state.loading = 'idle';
state.error = action.payload;
});
}
});
_a = oneTimePasscodeSlice.actions, exports.resetOneTimePasscode = _a.resetOneTimePasscode, exports.toggleOtpSent = _a.toggleOtpSent;
const selectOneTimePasscode = (state) => state.oneTimePasscode;
exports.selectOneTimePasscode = selectOneTimePasscode;
exports.oneTimePasscodeReducer = oneTimePasscodeSlice.reducer;