@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.
165 lines (164 loc) • 8.84 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectCustomerCommunicationPreferences = exports.customerCommunicationPrefsReducer = exports.setCustomerCommunicationPreferences = exports.resetCustomerCommunicationPreferencesError = exports.resetCustomerCommunicationPreferences = exports.setCustomerCommunicationDefaultPreferences = exports.removeCustomerCommunicationPreference = exports.addCustomerCommunicationPreference = exports.fetchCustomerCommunicationPreferences = exports.CustomerCommunicationPrefsActionType = 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 CustomerCommunicationPrefsActionType;
(function (CustomerCommunicationPrefsActionType) {
CustomerCommunicationPrefsActionType["FetchCustomerCommunicationPreferences"] = "customer/fetchCustomerCommunicationPreferences";
CustomerCommunicationPrefsActionType["AddCustomerCommunicationPreference"] = "customer/addCustomerCommunicationPreference";
CustomerCommunicationPrefsActionType["RemoveCustomerCommunicationPreference"] = "customer/removeCustomerCommunicationPreference";
CustomerCommunicationPrefsActionType["SetCustomerCommunicationDefaultPreferences"] = "customer/setCustomerCommunicationDefaultPreferences";
})(CustomerCommunicationPrefsActionType = exports.CustomerCommunicationPrefsActionType || (exports.CustomerCommunicationPrefsActionType = {}));
exports.fetchCustomerCommunicationPreferences = (0, toolkit_1.createAsyncThunk)(CustomerCommunicationPrefsActionType.FetchCustomerCommunicationPreferences, (_, { 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: communicationPreferences } = yield api.getCustomerCommunicationPreferences(token);
return communicationPreferences;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
exports.addCustomerCommunicationPreference = (0, toolkit_1.createAsyncThunk)(CustomerCommunicationPrefsActionType.AddCustomerCommunicationPreference, (requestData, { 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 = {
notification_area: requestData.area,
notification_channel: requestData.channel
};
yield api.postCustomerCommunicationPreference(token, data);
const { data: communicationPreferences } = yield api.getCustomerCommunicationPreferences(token);
dispatch((0, notifications_1.showNotification)('Preference added!'));
if (requestData.callback)
requestData.callback();
return communicationPreferences;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
exports.removeCustomerCommunicationPreference = (0, toolkit_1.createAsyncThunk)(CustomerCommunicationPrefsActionType.RemoveCustomerCommunicationPreference, (requestData, { 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.deleteCustomerCommunicationPreference(token, requestData.prefId);
const { data: communicationPreferences } = yield api.getCustomerCommunicationPreferences(token);
dispatch((0, notifications_1.showNotification)('Preference removed!'));
if (requestData.callback)
requestData.callback();
return communicationPreferences;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
exports.setCustomerCommunicationDefaultPreferences = (0, toolkit_1.createAsyncThunk)(CustomerCommunicationPrefsActionType.SetCustomerCommunicationDefaultPreferences, (requestData, { 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.putCustomerCommunicationPreference(token, requestData.prefs);
const { data: communicationPreferences } = yield api.getCustomerCommunicationPreferences(token);
if (requestData.callback)
requestData.callback();
return communicationPreferences;
}
catch (err) {
const error = err;
return (0, account_1.checkAuth)(error, dispatch, () => rejectWithValue(error));
}
}));
const customerCommunicationPrefsSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.CommunicationPrefs,
initialState,
reducers: {
resetCustomerCommunicationPreferences: () => initialState,
resetCustomerCommunicationPreferencesError: state => {
state.error = null;
state.loading = 'idle';
},
setCustomerCommunicationPreferences: (state, action) => {
state.entities = action.payload;
state.error = null;
}
},
extraReducers: builder => {
builder
.addCase(exports.fetchCustomerCommunicationPreferences.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.fetchCustomerCommunicationPreferences.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchCustomerCommunicationPreferences.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(exports.addCustomerCommunicationPreference.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.addCustomerCommunicationPreference.pending, state => {
state.loading = 'pending';
})
.addCase(exports.addCustomerCommunicationPreference.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(exports.removeCustomerCommunicationPreference.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.removeCustomerCommunicationPreference.pending, state => {
state.loading = 'pending';
})
.addCase(exports.removeCustomerCommunicationPreference.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
})
.addCase(exports.setCustomerCommunicationDefaultPreferences.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.setCustomerCommunicationDefaultPreferences.pending, state => {
state.loading = 'pending';
})
.addCase(exports.setCustomerCommunicationDefaultPreferences.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
});
}
});
_a = customerCommunicationPrefsSlice.actions, exports.resetCustomerCommunicationPreferences = _a.resetCustomerCommunicationPreferences, exports.resetCustomerCommunicationPreferencesError = _a.resetCustomerCommunicationPreferencesError, exports.setCustomerCommunicationPreferences = _a.setCustomerCommunicationPreferences;
exports.customerCommunicationPrefsReducer = customerCommunicationPrefsSlice.reducer;
const selectCustomerCommunicationPreferences = (state) => state.customer.communicationPreferences;
exports.selectCustomerCommunicationPreferences = selectCustomerCommunicationPreferences;