UNPKG

shopperman

Version:

shopping cart ui for shopify stores

209 lines 9.16 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var omnistorage_1 = require("omnistorage"); var mobx_1 = require("mobx"); /** * Default cart options */ exports.defaultCartOptions = { storageKey: "shopperman", omniStorage: new omnistorage_1.LocalClient({ storage: window.localStorage }) }; /** * Shopping cart which tracks products and maintains a subtotal * - maintain a cart containing products and keeping track of quantities etc * - you must provide a catalog of all available items * - items with quantity 0 are considered "not in the cart" */ var Cart = /** @class */ (function () { function Cart(opts) { this.itemCatalog = []; this.panelOpen = false; var options = __assign({}, exports.defaultCartOptions, opts); this.storageKey = options.storageKey; this.omniStorage = options.omniStorage; this.itemCatalog = options.itemCatalog; this.currencyControl = options.currencyControl; } /** * Rehydrate the item catalog details from storage */ Cart.prototype.loadFromStorage = function () { return __awaiter(this, void 0, void 0, function () { var _a, omniStorage, storageKey, data, _b, _c, _loop_1, this_1, _i, _d, productId, error_1; return __generator(this, function (_e) { switch (_e.label) { case 0: _a = this, omniStorage = _a.omniStorage, storageKey = _a.storageKey; _e.label = 1; case 1: _e.trys.push([1, 3, , 4]); _c = (_b = JSON).parse; return [4 /*yield*/, omniStorage.getItem(storageKey)]; case 2: data = _c.apply(_b, [_e.sent()]); _loop_1 = function (productId) { var cartStorage = data[productId]; var cartItem = this_1.itemCatalog.find(function (cartItem) { return cartItem.product.id === productId; }); cartItem.setQuantity(cartStorage.quantity); }; this_1 = this; for (_i = 0, _d = Object.keys(data); _i < _d.length; _i++) { productId = _d[_i]; _loop_1(productId); } return [3 /*break*/, 4]; case 3: error_1 = _e.sent(); console.error("shopperman cart load from storage error: " + error_1.message); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; /** * Save item catalog details to storage */ Cart.prototype.saveToStorage = function () { var _a = this, omniStorage = _a.omniStorage, storageKey = _a.storageKey; var data = {}; for (var _i = 0, _b = this.itemCatalog; _i < _b.length; _i++) { var _c = _b[_i], product = _c.product, quantity = _c.quantity; var productId = product.id; data[productId] = { quantity: quantity }; } omniStorage.setItem(storageKey, JSON.stringify(data)); }; Object.defineProperty(Cart.prototype, "activeItems", { /** * Getter for items which are "in the cart" * - active items are cart items with quantity greater than zero */ get: function () { return this.itemCatalog.filter(function (item) { return item.quantity > 0; }); }, enumerable: true, configurable: true }); Object.defineProperty(Cart.prototype, "value", { /** * Sum of cart item value */ get: function () { var reducer = function (subtotal, item) { return subtotal + item.value; }; return this.activeItems.reduce(reducer, 0); }, enumerable: true, configurable: true }); Object.defineProperty(Cart.prototype, "price", { /** * Formatter price tag for the whole cart's value */ get: function () { var _a = this, value = _a.value, currencyControl = _a.currencyControl; return currencyControl.convertAndFormat(value); }, enumerable: true, configurable: true }); /** * Toggle the cart panel open or closed */ Cart.prototype.togglePanelOpen = function (open) { if (open === void 0) { open = null; } this.panelOpen = open === null ? !this.panelOpen : open; return this.panelOpen; }; /** * Remove all products from the cart */ Cart.prototype.clear = function () { for (var _i = 0, _a = this.itemCatalog; _i < _a.length; _i++) { var item = _a[_i]; item.setQuantity(0); } }; /** * Get a cart item by providing the product inside */ Cart.prototype.getProductItem = function (product) { return this.itemCatalog.find(function (i) { return i.product === product; }); }; __decorate([ mobx_1.observable ], Cart.prototype, "itemCatalog", void 0); __decorate([ mobx_1.observable ], Cart.prototype, "panelOpen", void 0); __decorate([ mobx_1.computed ], Cart.prototype, "activeItems", null); __decorate([ mobx_1.computed ], Cart.prototype, "value", null); __decorate([ mobx_1.computed ], Cart.prototype, "price", null); __decorate([ mobx_1.action ], Cart.prototype, "togglePanelOpen", null); return Cart; }()); exports.Cart = Cart; //# sourceMappingURL=cart.js.map