Repository URL to install this package:
|
Version:
2.2.0 ▾
|
'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 _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, response, accessToken, anonymous, 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 = 19;
break;
case 10:
_context.next = 12;
return (0, _effects.call)(_api2.default.tokenFromCookie, options);
case 12:
response = _context.sent;
accessToken = response.accessToken, anonymous = response.anonymous;
if (!(accessToken && !anonymous)) {
_context.next = 18;
break;
}
token = response.accessToken;
_context.next = 19;
break;
case 18:
throw new Error('User is anonymous or has invalid cookies');
case 19:
if (!token) {
_context.next = 37;
break;
}
_context.next = 22;
return (0, _effects.call)(_api2.default.me, token, options);
case 22:
user = _context.sent;
if (!user.accessToken) {
_context.next = 34;
break;
}
_context.next = 26;
return (0, _effects.put)((0, _actions.userLoaded)(user));
case 26:
if (!(user.premium && user.premium.active && user.premium.active.id)) {
_context.next = 29;
break;
}
_context.next = 29;
return (0, _effects.put)((0, _actions.loadTheme)());
case 29:
if (!(user.mandator && user.mandator.id)) {
_context.next = 32;
break;
}
_context.next = 32;
return (0, _effects.put)((0, _actions.loadBranding)());
case 32:
_context.next = 35;
break;
case 34:
throw Error('token is not valid anymore');
case 35:
_context.next = 38;
break;
case 37:
throw Error('no token');
case 38:
_context.next = 44;
break;
case 40:
_context.prev = 40;
_context.t0 = _context['catch'](0);
_context.next = 44;
return (0, _effects.put)((0, _actions.userLoadFailed)(_context.t0));
case 44:
case 'end':
return _context.stop();
}
}
}, _marked, this, [[0, 40]]);
}
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);
}