@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.
61 lines (60 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.signUpReducer = exports.selectSignUp = exports.resetSignUp = exports.signUpCustomer = exports.SignUpActionType = 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 = {
loading: 'idle',
error: null
};
var SignUpActionType;
(function (SignUpActionType) {
SignUpActionType["SignUpCustomer"] = "signUp/signUpCustomer";
})(SignUpActionType = exports.SignUpActionType || (exports.SignUpActionType = {}));
exports.signUpCustomer = (0, toolkit_1.createAsyncThunk)(SignUpActionType.SignUpCustomer, ({ data, callback }, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const { api, brand } = getState().config;
const response = yield api.postSignUp(data);
const { email, password } = data;
if (email && password) {
if ((brand === null || brand === void 0 ? void 0 : brand.tpls) && (brand === null || brand === void 0 ? void 0 : brand.tpls) !== 'SPARKFLY') {
const resp = yield api.postCustomerSignIn(data);
dispatch((0, customer_1.setCustomerAuth)(resp));
yield dispatch((0, customer_1.fetchCustomer)());
}
else {
dispatch((0, customer_1.loginCustomer)({ email, password }));
}
}
if (callback)
callback(response);
return;
}
catch (err) {
return rejectWithValue(err);
}
}));
const signUpSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.SignUp,
initialState,
reducers: {
resetSignUp: () => initialState
},
extraReducers: builder => {
builder
.addCase(exports.signUpCustomer.fulfilled, () => initialState)
.addCase(exports.signUpCustomer.pending, state => {
state.loading = 'pending';
})
.addCase(exports.signUpCustomer.rejected, (state, action) => {
state.loading = 'idle';
state.error = action.payload;
});
}
});
exports.resetSignUp = signUpSlice.actions.resetSignUp;
const selectSignUp = (state) => state.signUp;
exports.selectSignUp = selectSignUp;
exports.signUpReducer = signUpSlice.reducer;