@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.
97 lines (96 loc) • 3.61 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dealsReducer = exports.selectEligibleDeals = exports.selectDeals = exports.setSelectedDeals = exports.resetDeals = exports.fetchDeals = exports.DealsActionType = void 0;
const tslib_1 = require("tslib");
const toolkit_1 = require("@reduxjs/toolkit");
const types_1 = require("./types");
const utils_1 = require("@open-tender/utils");
const initialState = {
entities: [],
selected: null,
error: null,
loading: 'idle'
};
var DealsActionType;
(function (DealsActionType) {
DealsActionType["FetchDeals"] = "deals/fetchDeals";
})(DealsActionType = exports.DealsActionType || (exports.DealsActionType = {}));
exports.fetchDeals = (0, toolkit_1.createAsyncThunk)(DealsActionType.FetchDeals, (_, { getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { api } = getState().config;
if (!api)
return;
try {
const { profile } = getState().customer.account;
const { customer_id = null } = profile || {};
return yield api.getDeals(customer_id);
}
catch (err) {
return rejectWithValue(err);
}
}));
const dealsSlice = (0, toolkit_1.createSlice)({
name: types_1.ReducerType.Deals,
initialState,
reducers: {
resetDeals: () => initialState,
setSelectedDeals: (state, action) => {
state.selected = action.payload;
}
},
extraReducers: builder => {
builder
.addCase(exports.fetchDeals.fulfilled, (state, action) => {
state.entities = action.payload;
state.loading = 'idle';
state.error = null;
})
.addCase(exports.fetchDeals.pending, state => {
state.loading = 'pending';
})
.addCase(exports.fetchDeals.rejected, (state, action) => {
state.error = action.payload;
state.loading = 'idle';
});
}
});
_a = dealsSlice.actions, exports.resetDeals = _a.resetDeals, exports.setSelectedDeals = _a.setSelectedDeals;
const selectDeals = (state) => state.deals;
exports.selectDeals = selectDeals;
const selectEligibleDeals = (state) => {
const { brand } = state.config || {};
if (!brand || !brand.has_deals)
return [];
let { entities: deals } = state.deals;
const { orderType, serviceType, revenueCenter } = state.order;
if (orderType) {
deals = deals.filter(i => !i.order_type || i.order_type === orderType);
}
if (serviceType) {
deals = deals.filter(i => !i.service_type || i.service_type === serviceType);
}
if (revenueCenter) {
const { revenue_center_id } = revenueCenter;
deals = deals.filter(i => {
var _a;
return !((_a = i.revenue_centers) === null || _a === void 0 ? void 0 : _a.length) ||
i.revenue_centers
.map(r => r.revenue_center_id)
.includes(revenue_center_id);
});
}
const minutes = (0, utils_1.getMinutesfromDate)(new Date());
deals = deals.filter(i => {
if (!i.dayparts.length)
return true;
const validDayparts = i.dayparts.filter(d => {
const start = (0, utils_1.time24ToMinutes)(d.start_time);
const end = (0, utils_1.time24ToMinutes)(d.end_time);
return start <= minutes && minutes <= end;
});
return validDayparts.length > 0;
});
return deals;
};
exports.selectEligibleDeals = selectEligibleDeals;
exports.dealsReducer = dealsSlice.reducer;