UNPKG

escalade-price-store

Version:

An event-based store system for getting Escalade Sports pricing. Stores all fetched prices in a cookie for quick retrieval.

128 lines (104 loc) 3.75 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _usdFormatter = require('usd-formatter'); var _usdFormatter2 = _interopRequireDefault(_usdFormatter); var _escaladeDataStore = require('escalade-data-store'); var _escaladeDataStore2 = _interopRequireDefault(_escaladeDataStore); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function formatPrice(price) { if (price === 'undefined') { return 'undefined'; } return (0, _usdFormatter2.default)(price); } var PriceStore = function (_DataStore) { (0, _inherits3.default)(PriceStore, _DataStore); function PriceStore(options) { (0, _classCallCheck3.default)(this, PriceStore); options = (0, _extends3.default)({ envEndpoints: ['PRICING_ENDPOINT', 'GATSBY_PRICING_ENDPOINT'], cookiePrefix: 'escaPricing', productionEndpoint: 'https://cojn6cbcd7.execute-api.us-east-1.amazonaws.com/production/handler', stagingEndpoint: 'https://hmfnvefe14.execute-api.us-east-1.amazonaws.com/staging/handler' }, options); return (0, _possibleConstructorReturn3.default)(this, (PriceStore.__proto__ || Object.getPrototypeOf(PriceStore)).call(this, options)); } (0, _createClass3.default)(PriceStore, [{ key: 'triggerChange', value: function triggerChange() { var _this2 = this; var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; this.log('Triggering any changes...'); this.changeEventsOptions.forEach(function (opt, key) { if (id !== false) { id = id.toLowerCase(); if (opt.id === id) { if (opt.formatted) { _this2.log('Triggering change for ' + id + ': formatted price'); _this2.changeEvents[key](_this2.getFormattedPrice(id)); } else { _this2.log('Triggering change for ' + id + ': unformatted price'); _this2.changeEvents[key](_this2.getPrice(id)); } } } else if (opt.id === false) { if (opt.formatted) { _this2.log('Triggering change for all: formatted price'); _this2.changeEvents[key](_this2.getFormattedPrices()); } else { _this2.log('Triggering change for all: unformatted price'); _this2.changeEvents[key](_this2.getPrices()); } } }); } }, { key: 'getPrice', value: function getPrice(id) { id = id.toLowerCase(); if (id in this.store) { return this.store[id]; } } }, { key: 'getPrices', value: function getPrices() { return (0, _extends3.default)({}, this.store); } }, { key: 'getFormattedPrice', value: function getFormattedPrice(id) { var price = this.getPrice(id); if (price) { return formatPrice(price); } } }, { key: 'getFormattedPrices', value: function getFormattedPrices() { var prices = {}; for (var i in this.store) { if (this.store[i]) { prices[i] = formatPrice(this.store[i]); } } return prices; } }]); return PriceStore; }(_escaladeDataStore2.default); exports.default = PriceStore; module.exports = exports['default']; //# sourceMappingURL=index.js.map