Repository URL to install this package:
|
Version:
0.2.4 ▾
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.default = adsSaga;
var _effects = require('redux-saga/effects');
var _usersApiConnector = require('@doodle/users-api-connector');
var _geolocation = require('@doodle/geolocation');
var _gpt = require('./gpt');
var _gpt2 = _interopRequireDefault(_gpt);
var _urbanMedia = require('./urbanMedia');
var _urbanMedia2 = _interopRequireDefault(_urbanMedia);
var _actions = require('../actions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _marked = /*#__PURE__*/_regenerator2.default.mark(doDefineSlot),
_marked2 = /*#__PURE__*/_regenerator2.default.mark(watchDefineSlot),
_marked3 = /*#__PURE__*/_regenerator2.default.mark(watchDestroySlot),
_marked4 = /*#__PURE__*/_regenerator2.default.mark(watchSetTargeting),
_marked5 = /*#__PURE__*/_regenerator2.default.mark(doRefresh),
_marked6 = /*#__PURE__*/_regenerator2.default.mark(watchRefresh),
_marked7 = /*#__PURE__*/_regenerator2.default.mark(doInitialize),
_marked8 = /*#__PURE__*/_regenerator2.default.mark(watchInitialize),
_marked9 = /*#__PURE__*/_regenerator2.default.mark(doLoadGpt),
_marked10 = /*#__PURE__*/_regenerator2.default.mark(watchLoadGpt),
_marked11 = /*#__PURE__*/_regenerator2.default.mark(doLoadUrbanMedia),
_marked12 = /*#__PURE__*/_regenerator2.default.mark(watchLoadUrbanMedia),
_marked13 = /*#__PURE__*/_regenerator2.default.mark(toggleAdvertisementOnUserChange),
_marked14 = /*#__PURE__*/_regenerator2.default.mark(watchUserChange),
_marked15 = /*#__PURE__*/_regenerator2.default.mark(waitForUser),
_marked16 = /*#__PURE__*/_regenerator2.default.mark(waitForGeolocation),
_marked17 = /*#__PURE__*/_regenerator2.default.mark(adsSaga);
var isGermany = function isGermany(geolocationState) {
return geolocationState.geolocation && geolocationState.geolocation.country_code === 'DE';
};
var normalizePlacement = function normalizePlacement(pathname) {
return pathname.split('/')[1].replace(/(create|edit)/, 'wizard');
};
var expandAdUnitPath = function expandAdUnitPath(adUnitPath) {
var language = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en';
var country = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'US';
var pathname = arguments[3];
return '/46753895/' + (country === 'CH' && ['de', 'fr', 'it'].indexOf(language) === 0 ? language : 'int') + '-doodle.com/' + adUnitPath + (pathname ? '/' + normalizePlacement(pathname) : '');
};
function doDefineSlot() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _ref = arguments[1];
var payload = _ref.payload;
var i18nState, routerState, geolocationState, slot, id, adUnitPath, sizeMapping, expandedAdUnitPath;
return _regenerator2.default.wrap(function doDefineSlot$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return (0, _effects.select)(function (state) {
return state[options.geolocationDomain || 'i18n'];
});
case 2:
i18nState = _context.sent;
_context.next = 5;
return (0, _effects.select)(function (state) {
return state[options.routerDomain || 'router'];
});
case 5:
routerState = _context.sent;
_context.next = 8;
return (0, _effects.select)(function (state) {
return state[options.geolocationDomain || 'geolocation'];
});
case 8:
geolocationState = _context.sent;
slot = payload.slot;
id = slot.id, adUnitPath = slot.adUnitPath, sizeMapping = slot.sizeMapping;
if (id && adUnitPath && sizeMapping) {
expandedAdUnitPath = expandAdUnitPath(adUnitPath, i18nState.locale, geolocationState.geolocation.country_code, routerState.location.pathname);
_gpt2.default.defineSlot({ id: id, adUnitPath: expandedAdUnitPath, sizeMapping: sizeMapping });
}
case 12:
case 'end':
return _context.stop();
}
}
}, _marked, this);
}
function watchDefineSlot() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchDefineSlot$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.DEFINE_SLOT, doDefineSlot, options);
case 2:
case 'end':
return _context2.stop();
}
}
}, _marked2, this);
}
function doDestroySlot() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _ref2 = arguments[1];
var payload = _ref2.payload;
var slot = payload.slot;
_gpt2.default.destroySlot(slot.id);
}
function watchDestroySlot() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchDestroySlot$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.DESTROY_SLOT, doDestroySlot, options);
case 2:
case 'end':
return _context3.stop();
}
}
}, _marked3, this);
}
function doSetTargeting() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _ref3 = arguments[1];
var payload = _ref3.payload;
var targeting = payload.targeting;
_gpt2.default.setTargeting(targeting);
}
function watchSetTargeting() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchSetTargeting$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.SET_TARGETING, doSetTargeting, options);
case 2:
case 'end':
return _context4.stop();
}
}
}, _marked4, this);
}
function doRefresh() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var advertisingState, geolocationState, slots, urbanIds;
return _regenerator2.default.wrap(function doRefresh$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return (0, _effects.select)(function (state) {
return state[options.domain || 'advertising'];
});
case 2:
advertisingState = _context5.sent;
_context5.next = 5;
return (0, _effects.select)(function (state) {
return state[options.geolocationDomain || 'geolocation'];
});
case 5:
geolocationState = _context5.sent;
slots = advertisingState.slots;
if (!isGermany(geolocationState)) {
_context5.next = 13;
break;
}
urbanIds = Object.keys(slots).map(function (id) {
return slots[id].urbanId;
}).filter(function (urbanId) {
return urbanId;
});
_context5.next = 11;
return _urbanMedia2.default.refresh(urbanIds);
case 11:
_context5.next = 15;
break;
case 13:
_context5.next = 15;
return _gpt2.default.refreshSlots(Object.keys(slots));
case 15:
case 'end':
return _context5.stop();
}
}
}, _marked5, this);
}
function watchRefresh() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchRefresh$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.REFRESH, doRefresh, options);
case 2:
case 'end':
return _context6.stop();
}
}
}, _marked6, this);
}
function doInitialize() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var advertisingState, geolocationState, slots;
return _regenerator2.default.wrap(function doInitialize$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return (0, _effects.select)(function (state) {
return state[options.domain || 'advertising'];
});
case 2:
advertisingState = _context7.sent;
_context7.next = 5;
return (0, _effects.select)(function (state) {
return state[options.geolocationDomain || 'geolocation'];
});
case 5:
geolocationState = _context7.sent;
if (!advertisingState.enabled) {
_context7.next = 19;
break;
}
if (!(!advertisingState.gpt.loaded && !advertisingState.gpt.loading)) {
_context7.next = 14;
break;
}
_context7.next = 10;
return (0, _effects.put)((0, _actions.loadGpt)());
case 10:
_context7.next = 12;
return (0, _effects.take)([_actions.ActionTypes.GPT_LOAD_ERROR, _actions.ActionTypes.GPT_LOAD_SUCCESS]);
case 12:
_gpt2.default.setAdsense({
page_url: window.location
});
_gpt2.default.setTargeting(advertisingState.targeting);
case 14:
if (!(isGermany(geolocationState) && !advertisingState.urbanMedia.loaded && !advertisingState.urbanMedia.loading)) {
_context7.next = 19;
break;
}
_context7.next = 17;
return (0, _effects.put)((0, _actions.loadUrbanMedia)(advertisingState.targeting));
case 17:
_context7.next = 19;
return (0, _effects.take)([_actions.ActionTypes.URBAN_MEDIA_LOAD_ERROR, _actions.ActionTypes.URBAN_MEDIA_LOAD_SUCCESS]);
case 19:
slots = advertisingState.slots;
_context7.next = 22;
return (0, _effects.all)(Object.keys(slots).map(function (id) {
return (0, _effects.call)(doDefineSlot, options, { payload: { slot: _extends({}, slots[id]) } });
}));
case 22:
_context7.next = 24;
return (0, _effects.all)([(0, _effects.call)(doRefresh, options), (0, _effects.call)(watchRefresh, options)]);
case 24:
case 'end':
return _context7.stop();
}
}
}, _marked7, this);
}
function watchInitialize() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchInitialize$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.INITIALIZE, doInitialize, options);
case 2:
case 'end':
return _context8.stop();
}
}
}, _marked8, this);
}
function doLoadGpt() {
return _regenerator2.default.wrap(function doLoadGpt$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.prev = 0;
_context9.next = 3;
return _gpt2.default.initialize();
case 3:
_context9.next = 5;
return (0, _effects.put)((0, _actions.gptLoadSuccess)());
case 5:
_context9.next = 11;
break;
case 7:
_context9.prev = 7;
_context9.t0 = _context9['catch'](0);
_context9.next = 11;
return (0, _effects.put)((0, _actions.gptLoadError)(_context9.t0));
case 11:
case 'end':
return _context9.stop();
}
}
}, _marked9, this, [[0, 7]]);
}
function watchLoadGpt() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchLoadGpt$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
_context10.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.LOAD_GPT, doLoadGpt, options);
case 2:
case 'end':
return _context10.stop();
}
}
}, _marked10, this);
}
function doLoadUrbanMedia() {
return _regenerator2.default.wrap(function doLoadUrbanMedia$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_context11.prev = 0;
_context11.next = 3;
return _urbanMedia2.default.initialize();
case 3:
_context11.next = 5;
return (0, _effects.put)((0, _actions.urbanMediaLoadSuccess)());
case 5:
_context11.next = 11;
break;
case 7:
_context11.prev = 7;
_context11.t0 = _context11['catch'](0);
_context11.next = 11;
return (0, _effects.put)((0, _actions.urbanMediaLoadError)());
case 11:
case 'end':
return _context11.stop();
}
}
}, _marked11, this, [[0, 7]]);
}
function watchLoadUrbanMedia() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchLoadUrbanMedia$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_context12.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.LOAD_URBAN_MEDIA, doLoadUrbanMedia, options);
case 2:
case 'end':
return _context12.stop();
}
}
}, _marked12, this);
}
function toggleAdvertisementOnUserChange() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var userState;
return _regenerator2.default.wrap(function toggleAdvertisementOnUserChange$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return (0, _effects.select)(function (state) {
return state[options.userDomain || 'user'];
});
case 2:
userState = _context13.sent;
if (!(userState.data && userState.data.premium)) {
_context13.next = 8;
break;
}
_context13.next = 6;
return (0, _effects.put)((0, _actions.disable)());
case 6:
_context13.next = 10;
break;
case 8:
_context13.next = 10;
return (0, _effects.put)((0, _actions.enable)());
case 10:
case 'end':
return _context13.stop();
}
}
}, _marked13, this);
}
function watchUserChange() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function watchUserChange$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
_context14.next = 2;
return (0, _effects.takeLatest)([_usersApiConnector.ActionTypes.USER_LOAD_FAILED, _usersApiConnector.ActionTypes.USER_LOADED], toggleAdvertisementOnUserChange, options);
case 2:
case 'end':
return _context14.stop();
}
}
}, _marked14, this);
}
function waitForUser() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function waitForUser$(_context15) {
while (1) {
switch (_context15.prev = _context15.next) {
case 0:
_context15.next = 2;
return (0, _effects.take)([_usersApiConnector.ActionTypes.USER_LOAD_FAILED, _usersApiConnector.ActionTypes.USER_LOADED]);
case 2:
_context15.next = 4;
return (0, _effects.call)(toggleAdvertisementOnUserChange, options);
case 4:
case 'end':
return _context15.stop();
}
}
}, _marked15, this);
}
function waitForGeolocation() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var geolocationState;
return _regenerator2.default.wrap(function waitForGeolocation$(_context16) {
while (1) {
switch (_context16.prev = _context16.next) {
case 0:
_context16.next = 2;
return (0, _effects.select)(function (state) {
return state[options.geolocationDomain || 'geolocation'];
});
case 2:
geolocationState = _context16.sent;
if (!(!geolocationState.loading && (!geolocationState.geolocation || !geolocationState.geolocation.country_code))) {
_context16.next = 10;
break;
}
_context16.next = 6;
return (0, _effects.put)((0, _geolocation.fetchGeolocation)());
case 6:
_context16.next = 8;
return (0, _effects.take)([_geolocation.ActionTypes.FETCH_GEOLOCATION_SUCCESS, _geolocation.ActionTypes.FETCH_GEOLOCATION_ERROR]);
case 8:
_context16.next = 13;
break;
case 10:
if (!geolocationState.loading) {
_context16.next = 13;
break;
}
_context16.next = 13;
return (0, _effects.take)([_geolocation.ActionTypes.FETCH_GEOLOCATION_SUCCESS, _geolocation.ActionTypes.FETCH_GEOLOCATION_ERROR]);
case 13:
case 'end':
return _context16.stop();
}
}
}, _marked16, this);
}
function adsSaga() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _regenerator2.default.wrap(function adsSaga$(_context18) {
while (1) {
switch (_context18.prev = _context18.next) {
case 0:
_context18.next = 2;
return (0, _effects.all)([(0, _effects.call)(waitForGeolocation, options), (0, _effects.call)(waitForUser, options)]);
case 2:
_context18.next = 4;
return (0, _effects.spawn)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
return _regenerator2.default.wrap(function _callee$(_context17) {
while (1) {
switch (_context17.prev = _context17.next) {
case 0:
_context17.next = 2;
return (0, _effects.all)([(0, _effects.call)(watchUserChange, options), (0, _effects.call)(watchLoadGpt, options), (0, _effects.call)(watchLoadUrbanMedia, options), (0, _effects.call)(watchDefineSlot, options), (0, _effects.call)(watchDestroySlot, options), (0, _effects.call)(watchSetTargeting, options)]);
case 2:
case 'end':
return _context17.stop();
}
}
}, _callee, this);
}));
case 4:
_context18.next = 6;
return (0, _effects.all)([(0, _effects.call)(watchInitialize, options), (0, _effects.put)((0, _actions.initialize)())]);
case 6:
case 'end':
return _context18.stop();
}
}
}, _marked17, this);
}