@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.
133 lines (132 loc) • 6.23 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.customerAddressesReducer = exports.selectCustomerAddresses = exports.resetCustomerAddressesError = exports.setCustomerAddresses = exports.resetCustomerAddresses = exports.removeCustomerAddress = exports.updateCustomerAddress = exports.fetchCustomerAddresses = exports.CustomerAddressesActionType = void 0;
const tslib_1 = require("tslib");
const toolkit_1 = require("@reduxjs/toolkit");
const types_1 = require("../types");
const types_2 = require("@open-tender/types");
const account_1 = require("./account");
const notifications_1 = require("../notifications");
const initialState = {
entities: [],
lookup: {},
loading: 'idle',
error: null
};
var CustomerAddressesActionType;
(function (CustomerAddressesActionType) {
CustomerAddressesActionType["FetchCustomerAddresses"] = "customer/fetchCustomerAddresses";
CustomerAddressesActionType["UpdateCustomerAddresses"] = "customer/updateCustomerAddresses";
CustomerAddressesActionType["RemoveCustomerAddress"] = "customer/removeCustomerAddress";
})(CustomerAddressesActionType = exports.CustomerAddressesActionType || (exports.CustomerAddressesActionType = {}));
exports.fetchCustomerAddresses = (0, toolkit_1.createAsyncThunk)(CustomerAddressesActionType.FetchCustomerAddresses, (limit, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const api = getState().config.api;
const token = (0, account_1.selectToken)(getState());
if (!token)
throw new Error(types_2.MISSING_CUSTOMER);
const { data: addresses } = yield api.getCustomerAddresses(token, limit);
return addresses;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
exports.updateCustomerAddress = (0, toolkit_1.createAsyncThunk)(CustomerAddressesActionType.UpdateCustomerAddresses, (data, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const api = getState().config.api;
const token = (0, account_1.selectToken)(getState());
if (!token)
throw new Error(types_2.MISSING_CUSTOMER);
yield api.putCustomerAddress(token, data.addressId, data.data);
const limit = getState().customer.addresses.entities.length;
const { data: addresses } = yield api.getCustomerAddresses(token, limit);
dispatch((0, notifications_1.showNotification)('Address updated!'));
if (data.callback)
data.callback();
return addresses;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
exports.removeCustomerAddress = (0, toolkit_1.createAsyncThunk)(CustomerAddressesActionType.RemoveCustomerAddress, (data, { dispatch, getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const api = getState().config.api;
const token = (0, account_1.selectToken)(getState());
if (!token)
throw new Error(types_2.MISSING_CUSTOMER);
yield api.deleteCustomerAddress(token, data.addressId);
const limit = getState().customer.addresses.entities.length;
const { data: addresses } = yield api.getCustomerAddresses(token, limit);
dispatch((0, notifications_1.showNotification)('Address removed!'));
if (data.callback)
data.callback();
return addresses;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
const customerAddressesSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.CustomerAddresses,
initialState,
reducers: {
resetCustomerAddresses: () => initialState,
setCustomerAddresses: (state, action) => {
state.entities = action.payload;
state.error = null;
},
resetCustomerAddressesError: state => {
state.error = null;
state.loading = 'idle';
}
},
extraReducers: builder => {
builder
.addCase(exports.fetchCustomerAddresses.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.fetchCustomerAddresses.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchCustomerAddresses.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(exports.updateCustomerAddress.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.updateCustomerAddress.pending, state => {
state.loading = 'pending';
})
.addCase(exports.updateCustomerAddress.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(exports.removeCustomerAddress.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.removeCustomerAddress.pending, state => {
state.loading = 'pending';
})
.addCase(exports.removeCustomerAddress.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
});
}
});
_a = customerAddressesSlice.actions, exports.resetCustomerAddresses = _a.resetCustomerAddresses, exports.setCustomerAddresses = _a.setCustomerAddresses, exports.resetCustomerAddressesError = _a.resetCustomerAddressesError;
const selectCustomerAddresses = (state) => state.customer.addresses;
exports.selectCustomerAddresses = selectCustomerAddresses;
exports.customerAddressesReducer = customerAddressesSlice.reducer;