usimple-saleor-sdk
Version:
This package contains all queries and mutations that are used in our sample storefront. It can be used for semi-custom or fully-custom (with ability to extend existing queries) storefront solutions.
172 lines • 9.64 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaleorCartAPI = void 0;
const helpers_1 = require("../../helpers");
const Cart_1 = require("../../jobs/Cart");
const types_1 = require("../../state/types");
const utils_1 = require("./utils");
class SaleorCartAPI extends helpers_1.ErrorListener {
constructor(localStorageManager, apolloClientManager, saleorState, jobsManager, config) {
super();
this.addItem = (variantId, quantity) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
// 1. save in local storage
this.localStorageManager.addItemToCart(variantId, quantity);
// 2. save online if possible (if checkout id available)
if ((_a = this.saleorState.checkout) === null || _a === void 0 ? void 0 : _a.lines) {
const { data, error, } = yield this.apolloClientManager.getRefreshedCheckoutLines(this.saleorState.checkout.lines, this.config.channel);
if (error) {
this.fireError(error, Cart_1.ErrorCartTypes.SET_CART_ITEM);
}
else {
this.localStorageManager.getHandler().setCheckout(Object.assign(Object.assign({}, this.saleorState.checkout), { lines: data }));
}
}
if ((_b = this.saleorState.checkout) === null || _b === void 0 ? void 0 : _b.id) {
this.jobsManager.addToQueue("cart", "setCartItem");
return {
pending: true,
};
}
return {
pending: false,
};
});
this.removeItem = (variantId) => __awaiter(this, void 0, void 0, function* () {
var _c, _d;
// 1. save in local storage
this.localStorageManager.removeItemFromCart(variantId);
// 2. save online if possible (if checkout id available)
if ((_c = this.saleorState.checkout) === null || _c === void 0 ? void 0 : _c.lines) {
const { data, error, } = yield this.apolloClientManager.getRefreshedCheckoutLines(this.saleorState.checkout.lines, this.config.channel);
if (error) {
this.fireError(error, Cart_1.ErrorCartTypes.SET_CART_ITEM);
}
else {
this.localStorageManager.getHandler().setCheckout(Object.assign(Object.assign({}, this.saleorState.checkout), { lines: data }));
}
}
if ((_d = this.saleorState.checkout) === null || _d === void 0 ? void 0 : _d.id) {
this.jobsManager.addToQueue("cart", "setCartItem");
return {
pending: true,
};
}
return {
pending: false,
};
});
this.subtractItem = (variantId) => __awaiter(this, void 0, void 0, function* () {
var _e, _f;
// 1. save in local storage
this.localStorageManager.subtractItemFromCart(variantId);
// 2. save online if possible (if checkout id available)
if ((_e = this.saleorState.checkout) === null || _e === void 0 ? void 0 : _e.lines) {
const { data, error, } = yield this.apolloClientManager.getRefreshedCheckoutLines(this.saleorState.checkout.lines, this.config.channel);
if (error) {
this.fireError(error, Cart_1.ErrorCartTypes.SET_CART_ITEM);
}
else {
this.localStorageManager.getHandler().setCheckout(Object.assign(Object.assign({}, this.saleorState.checkout), { lines: data }));
}
}
if ((_f = this.saleorState.checkout) === null || _f === void 0 ? void 0 : _f.id) {
this.jobsManager.addToQueue("cart", "setCartItem");
return {
pending: true,
};
}
return {
pending: false,
};
});
this.updateItem = (variantId, quantity) => __awaiter(this, void 0, void 0, function* () {
var _g, _h;
// 1. save in local storage
this.localStorageManager.updateItemInCart(variantId, quantity);
// 2. save online if possible (if checkout id available)
if ((_g = this.saleorState.checkout) === null || _g === void 0 ? void 0 : _g.lines) {
const { data, error, } = yield this.apolloClientManager.getRefreshedCheckoutLines(this.saleorState.checkout.lines, this.config.channel);
if (error) {
this.fireError(error, Cart_1.ErrorCartTypes.SET_CART_ITEM);
}
else {
this.localStorageManager.getHandler().setCheckout(Object.assign(Object.assign({}, this.saleorState.checkout), { lines: data }));
}
}
if ((_h = this.saleorState.checkout) === null || _h === void 0 ? void 0 : _h.id) {
this.jobsManager.addToQueue("cart", "setCartItem");
return {
pending: true,
};
}
return {
pending: false,
};
});
this.saleorState = saleorState;
this.localStorageManager = localStorageManager;
this.apolloClientManager = apolloClientManager;
this.jobsManager = jobsManager;
this.config = config;
this.loaded = false;
this.jobsManager.attachErrorListener("cart", this.fireError);
this.saleorState.subscribeToChange(types_1.StateItems.CHECKOUT, (checkout) => {
var _a;
this.items = (_a = checkout === null || checkout === void 0 ? void 0 : checkout.lines) === null || _a === void 0 ? void 0 : _a.filter(line => line.quantity > 0).sort(utils_1.sortCheckoutLines);
});
this.saleorState.subscribeToChange(types_1.StateItems.SUMMARY_PRICES, (summaryPrices) => {
var _a, _b;
const { totalPrice, subtotalPrice, shippingPrice, discount } = summaryPrices || {};
this.totalPrice = totalPrice;
this.subtotalPrice = subtotalPrice;
this.shippingPrice = shippingPrice;
if (shippingPrice && ((_a = this.saleorState.checkout) === null || _a === void 0 ? void 0 : _a.shippingMethod)) {
this.apolloClientManager
.getCheckout(this.saleorState.checkout !== null, this.config.channel, (_b = this.saleorState.checkout) === null || _b === void 0 ? void 0 : _b.token)
.then(({ data }) => {
var _a, _b, _c, _d, _e, _f;
if (data &&
data.availableShippingMethods &&
data.availableShippingMethods[0]) {
const shiopMethod = data.availableShippingMethods[0];
if (shiopMethod && shiopMethod.price) {
shiopMethod.price.amount =
((_b = (_a = data === null || data === void 0 ? void 0 : data.shippingPrice) === null || _a === void 0 ? void 0 : _a.gross) === null || _b === void 0 ? void 0 : _b.amount) || 0;
data.availableShippingMethods[0] =
shiopMethod || null;
}
}
this.saleorState.checkout = data;
shippingPrice.amount =
((_d = (_c = data === null || data === void 0 ? void 0 : data.shippingPrice) === null || _c === void 0 ? void 0 : _c.gross) === null || _d === void 0 ? void 0 : _d.amount) ||
shippingPrice.amount;
if (this.totalPrice && this.subtotalPrice) {
this.totalPrice.gross.amount =
this.subtotalPrice.gross.amount +
shippingPrice.amount;
const netAmount = ((_f = (_e = data === null || data === void 0 ? void 0 : data.shippingPrice) === null || _e === void 0 ? void 0 : _e.net) === null || _f === void 0 ? void 0 : _f.amount) || 0;
this.totalPrice.net.amount =
this.subtotalPrice.net.amount + netAmount;
}
this.shippingPrice = shippingPrice;
});
}
this.discount = discount;
});
this.saleorState.subscribeToChange(types_1.StateItems.LOADED, (loaded) => {
this.loaded = loaded.checkout && loaded.summaryPrices;
});
}
}
exports.SaleorCartAPI = SaleorCartAPI;
//# sourceMappingURL=index.js.map