UNPKG

rizzo-next

Version:

The next generation of Lonely Planet's style guide and pattern library.

142 lines (109 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _dec, _desc, _value, _class; var _jquery = require("jquery"); var _jquery2 = _interopRequireDefault(_jquery); var _publish = require("../../core/decorators/publish"); var _publish2 = _interopRequireDefault(_publish); var _user = require("./user"); var _user2 = _interopRequireDefault(_user); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; } var LoginManager = (_dec = (0, _publish2.default)("user.status.update"), (_class = function () { function LoginManager() { _classCallCheck(this, LoginManager); this.lunaStatusUrl = "https://auth.lonelyplanet.com/users/status.json"; this.dotcomConnectStatusUrl = "https://connect.lonelyplanet.com/users/status.json"; this.feedUrl = "https://www.lonelyplanet.com/thorntree/users/feed"; if (window.lp.auth && window.lp.auth.host) { this.dotcomConnectStatusUrl = window.lp.auth.host + "/users/status.json"; } this.checkStatus(); } /** * Check to see if the user is currently logged in. * @return {jQuery.Deferred} */ _createClass(LoginManager, [{ key: "checkStatus", value: function checkStatus() { var _this = this; return _jquery2.default.when(_jquery2.default.ajax({ url: this.lunaStatusUrl, dataType: "jsonp", error: this.error.bind(this) }), _jquery2.default.ajax({ url: this.dotcomConnectStatusUrl, dataType: "jsonp", error: this.error.bind(this) })).done(function (_ref, _ref2) { var _ref4 = _slicedToArray(_ref, 1), lunaUser = _ref4[0]; var _ref3 = _slicedToArray(_ref2, 1), connectUser = _ref3[0]; var user = Object.assign({}, connectUser, { "connect": true }); if (lunaUser.id) { user = Object.assign({}, lunaUser, { "luna": true }); } _this.statusFetched(user); }); } /** * Callback from checking the user's login status. * If the user is not logged in, it will publish a user with a null id. * Will check for user notifications if the user is logged in. * @param {Object} user User login information */ }, { key: "statusFetched", value: function statusFetched(user) { // When swapping UI components through _v cookie being set with a specific value // We need to instantiante the user with any variants being sent from the status check this.user = user.username ? new _user2.default(user) : new _user2.default({ variant: user.variant }); if (!user.id) { return this._updateStatus(); } this._updateStatus(); } }, { key: "_updateStatus", value: function _updateStatus() { return this.user.toJSON(); } }, { key: "error", value: function error() { throw "Error retrieving luna login information"; } }]); return LoginManager; }(), (_applyDecoratedDescriptor(_class.prototype, "_updateStatus", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "_updateStatus"), _class.prototype)), _class)); exports.default = LoginManager;