Repository URL to install this package:
|
Version:
2.1.0 ▾
|
import { store } from '../state/store';
import { setData } from '../state/actions';
import { processType, getUserError, paywallStatus, getCalendarError } from '../constants/paywall';
import { getCalendarRequest, getUserDataRequest } from './requests';
import getStatusFromUserData from '../utils/getStatusFromUserData';
export default async ({ token, id, trackingEventPrefix }) => {
const { apiHost } = store.getState();
return new Promise(resolve => {
getUserDataRequest({ token, apiHost })
.then(async res => {
const data = await res.text();
const { status } = getStatusFromUserData({ data, token, trackingEventPrefix, apiHost });
if (!status.shouldOpenModal) {
getCalendarRequest({ token, id, apiHost, resolve })
.then(async calRes => {
const calendarResponse = await calRes.text();
const parsedCalendarResponse = JSON.parse(calendarResponse);
const { slots } = parsedCalendarResponse;
const filteredSlots = slots
.filter(slot => +new Date(slot.start) > +new Date())
.map(slot => ({ ...slot, attendee: null }));
const isSlotInThePast = slots.length !== filteredSlots.length;
if (isSlotInThePast) {
new Promise((duplicateResolveFunc, duplicateRejectFunc) => {
store.dispatch(
setData({
duplicateResolveFunc,
duplicateRejectFunc,
isWarningModalOpen: true,
})
);
})
.then(() => {
resolve({
status: paywallStatus.PASS,
});
})
.catch(() => {
resolve({ status: paywallStatus.REJECT });
});
} else {
resolve({
status: paywallStatus.PASS,
});
}
})
.catch(e => {
resolve({ ...getCalendarError, message: e.message });
});
} else {
store.dispatch(
setData({
trackingEventPostfix: status.trackingEventPostfix,
modalType: status.modalType,
isPaywallModalOpen: true,
resolve,
process: processType.DUPLICATE,
})
);
}
})
.catch(e => {
resolve({ ...getUserError, message: e.message });
});
});
};