Repository URL to install this package:
|
Version:
1.7.1 ▾
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
exports.watchLoadUser = watchLoadUser;
exports.watchLoadTheme = watchLoadTheme;
exports.watchLoadBranding = watchLoadBranding;
var _effects = require('redux-saga/effects');
var _api = require('./api');
var _api2 = _interopRequireDefault(_api);
var _storage = require('../storage');
var _storage2 = _interopRequireDefault(_storage);
var _actions = require('../actions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _marked = /*#__PURE__*/_regenerator2.default.mark(onLoadUser),
_marked2 = /*#__PURE__*/_regenerator2.default.mark(onLoadTheme),
_marked3 = /*#__PURE__*/_regenerator2.default.mark(onLoadBranding),
_marked4 = /*#__PURE__*/_regenerator2.default.mark(watchLoadUser),
_marked5 = /*#__PURE__*/_regenerator2.default.mark(watchLoadTheme),
_marked6 = /*#__PURE__*/_regenerator2.default.mark(watchLoadBranding);
function onLoadUser(options, action) {
var state, force, token, cookie, _ref, user;
return _regenerator2.default.wrap(function onLoadUser$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return (0, _effects.select)(function (globalState) {
return globalState[options.domain || 'user'];
});
case 3:
state = _context.sent;
force = action.force;
token = null;
if (!(force === true)) {
_context.next = 10;
break;
}
token = state.data.accessToken;
_context.next = 21;
break;
case 10:
cookie = {
doodleIdentification: _storage2.default.getCookie('DoodleIdentification', options),
doodleAuthentication: _storage2.default.getCookie('DoodleAuthentication', options)
};
if (!(cookie.doodleAuthentication && cookie.doodleIdentification)) {
_context.next = 20;
break;
}
_context.next = 14;
return (0, _effects.call)(_api2.default.tokenFromCookie, cookie, options);
case 14:
_ref = _context.sent;
token = _ref.accessToken;
if (token) {
_context.next = 18;
break;
}
throw new Error('could not obtain access token');
case 18:
_context.next = 21;
break;
case 20:
throw Error('invalid cookie');
case 21:
if (!token) {
_context.next = 39;
break;
}
_context.next = 24;
return (0, _effects.call)(_api2.default.me, token, options);
case 24:
user = _context.sent;
if (!user.accessToken) {
_context.next = 36;
break;
}
_context.next = 28;
return (0, _effects.put)((0, _actions.userLoaded)(user));
case 28:
if (!(user.premium && user.premium.active && user.premium.active.id)) {
_context.next = 31;
break;
}
_context.next = 31;
return (0, _effects.put)((0, _actions.loadTheme)());
case 31:
if (!(user.mandator && user.mandator.id)) {
_context.next = 34;
break;
}
_context.next = 34;
return (0, _effects.put)((0, _actions.loadBranding)());
case 34:
_context.next = 37;
break;
case 36:
throw Error('token is not valid anymore');
case 37:
_context.next = 40;
break;
case 39:
throw Error('no token');
case 40:
_context.next = 46;
break;
case 42:
_context.prev = 42;
_context.t0 = _context['catch'](0);
_context.next = 46;
return (0, _effects.put)((0, _actions.userLoadFailed)(_context.t0));
case 46:
case 'end':
return _context.stop();
}
}
}, _marked, this, [[0, 42]]);
}
function onLoadTheme(options) {
var userData, token, mandatorId, theme;
return _regenerator2.default.wrap(function onLoadTheme$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
_context2.next = 3;
return (0, _effects.select)(function (state) {
return state.user.data;
});
case 3:
userData = _context2.sent;
token = userData.accessToken;
mandatorId = userData.premium && userData.premium.active && userData.premium.active.id;
_context2.next = 8;
return (0, _effects.call)(_api2.default.loadTheme, token, mandatorId, options);
case 8:
theme = _context2.sent;
_context2.next = 11;
return (0, _effects.put)((0, _actions.loadThemeSuccess)(theme));
case 11:
_context2.next = 17;
break;
case 13:
_context2.prev = 13;
_context2.t0 = _context2['catch'](0);
_context2.next = 17;
return (0, _effects.put)((0, _actions.loadThemeError)(_context2.t0));
case 17:
case 'end':
return _context2.stop();
}
}
}, _marked2, this, [[0, 13]]);
}
function onLoadBranding(options) {
var userData, token, mandatorId, branding;
return _regenerator2.default.wrap(function onLoadBranding$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.prev = 0;
_context3.next = 3;
return (0, _effects.select)(function (state) {
return state.user.data;
});
case 3:
userData = _context3.sent;
token = userData.accessToken;
mandatorId = userData.mandator.id;
_context3.next = 8;
return (0, _effects.call)(_api2.default.loadBranding, token, mandatorId, options);
case 8:
branding = _context3.sent;
_context3.next = 11;
return (0, _effects.put)((0, _actions.loadBrandingSuccess)(branding));
case 11:
_context3.next = 17;
break;
case 13:
_context3.prev = 13;
_context3.t0 = _context3['catch'](0);
_context3.next = 17;
return (0, _effects.put)((0, _actions.loadBrandingError)(_context3.t0));
case 17:
case 'end':
return _context3.stop();
}
}
}, _marked3, this, [[0, 13]]);
}
function watchLoadUser(options) {
return _regenerator2.default.wrap(function watchLoadUser$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.LOAD_USER, onLoadUser, options);
case 2:
case 'end':
return _context4.stop();
}
}
}, _marked4, this);
}
function watchLoadTheme(options) {
return _regenerator2.default.wrap(function watchLoadTheme$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.LOAD_THEME, onLoadTheme, options);
case 2:
case 'end':
return _context5.stop();
}
}
}, _marked5, this);
}
function watchLoadBranding(options) {
return _regenerator2.default.wrap(function watchLoadBranding$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return (0, _effects.takeLatest)(_actions.ActionTypes.LOAD_BRANDING, onLoadBranding, options);
case 2:
case 'end':
return _context6.stop();
}
}
}, _marked6, this);
}