shopperman
Version:
shopping cart ui for shopify stores
90 lines • 4.26 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var mobx_1 = require("mobx");
var preact_1 = require("preact");
var mobx_preact_1 = require("mobx-preact");
var ProductDisplay = /** @class */ (function (_super) {
__extends(ProductDisplay, _super);
function ProductDisplay() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.handleButtonClick = function (event) {
if (!_this.inCart)
_this.addToCart();
};
return _this;
}
Object.defineProperty(ProductDisplay.prototype, "cartItem", {
get: function () {
var _a = this.props, cart = _a.cart, product = _a.product;
return cart.getProductItem(product);
},
enumerable: true,
configurable: true
});
Object.defineProperty(ProductDisplay.prototype, "inCart", {
get: function () {
return this.cartItem.quantity > 0;
},
enumerable: true,
configurable: true
});
ProductDisplay.prototype.addToCart = function () {
var cartItem = this.cartItem;
var cart = this.props.cart;
var quantity = cartItem.quantity;
this.cartItem.setQuantity(quantity + 1);
cart.togglePanelOpen(true);
};
ProductDisplay.prototype.render = function () {
var inCart = this.inCart;
var _a = this.props, product = _a.product, _b = _a.buttonText1, buttonText1 = _b === void 0 ? "Add to Cart" : _b, _c = _a.buttonText2, buttonText2 = _c === void 0 ? "In Cart" : _c, _d = _a.buttonTitle, buttonTitle = _d === void 0 ? undefined : _d;
return (preact_1.h("div", __assign({ className: "product-display", "data-in-cart": inCart ? "true" : "false" }, product.attributes),
preact_1.h("h3", { className: "title" }, product.title),
preact_1.h("div", { className: "box" },
preact_1.h("p", { className: "price" }, product.price),
preact_1.h("button", { className: "add-to-cart-button", title: inCart ? undefined : buttonTitle, onClick: this.handleButtonClick, disabled: inCart }, inCart ? buttonText2 : buttonText1)),
preact_1.h("p", { className: "description", dangerouslySetInnerHTML: { __html: product.description } })));
};
__decorate([
mobx_1.computed
], ProductDisplay.prototype, "cartItem", null);
__decorate([
mobx_1.computed
], ProductDisplay.prototype, "inCart", null);
ProductDisplay = __decorate([
mobx_preact_1.observer
], ProductDisplay);
return ProductDisplay;
}(preact_1.Component));
exports.ProductDisplay = ProductDisplay;
//# sourceMappingURL=product-display.js.map