@ikas/storefront 4.0.0-alpha.86 → 4.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { IkasBaseStore } from "../base";
2
2
  import { IkasCart, IkasOrderLineItem, IkasProduct, IkasProductVariant } from "../../models/data";
3
+ import { APIResponse } from "@ikas/storefront-api";
3
4
  export declare const CART_LS_KEY = "cartId";
4
5
  export declare class IkasCartStore {
5
6
  cart?: IkasCart | null;
@@ -11,14 +12,14 @@ export declare class IkasCartStore {
11
12
  get isCartLoadFinished(): boolean;
12
13
  get checkoutId(): string | undefined;
13
14
  get checkoutUrl(): string | undefined;
14
- addItem: (variant: IkasProductVariant, product: IkasProduct, initialQuantity?: number) => Promise<false | import("@ikas/storefront-api").APIResponse<IkasCart | null>>;
15
- changeItemQuantity: (item: IkasOrderLineItem, quantity: number) => Promise<false | import("@ikas/storefront-api").APIResponse<IkasCart | null>>;
16
- removeItem: (item: IkasOrderLineItem) => Promise<void>;
15
+ addItem: (variant: IkasProductVariant, product: IkasProduct, initialQuantity?: number) => Promise<IkasCartOperationResult>;
16
+ changeItemQuantity: (item: IkasOrderLineItem, quantity: number) => Promise<IkasCartOperationResult>;
17
+ removeItem: (item: IkasOrderLineItem) => Promise<IkasCartOperationResult>;
17
18
  removeCart: () => void;
18
19
  waitUntilInitialized: () => Promise<unknown>;
19
20
  findExistingItem: (variant: IkasProductVariant, product: IkasProduct) => IkasOrderLineItem | undefined;
20
- saveCouponCode: (couponCode?: string | null) => Promise<false | import("@ikas/storefront-api").APIResponse<IkasCart | null>>;
21
- saveCart: (cart: IkasCart) => Promise<import("@ikas/storefront-api").APIResponse<IkasCart | null>>;
21
+ saveCouponCode: (couponCode?: string | null) => Promise<IkasCartOperationResult>;
22
+ saveCart: (cart: IkasCart) => Promise<IkasCartOperationResult>;
22
23
  private removeCardIfEmpty;
23
24
  getCart: () => Promise<void>;
24
25
  private setCart;
@@ -26,3 +27,13 @@ export declare class IkasCartStore {
26
27
  private createCartLineOptions;
27
28
  private getOptionValues;
28
29
  }
30
+ export declare type IkasCartOperationResult = {
31
+ success: boolean;
32
+ validationError?: IkasCartOperationValidationError;
33
+ response?: APIResponse<IkasCart | null>;
34
+ };
35
+ export declare enum IkasCartOperationValidationError {
36
+ INSUFFICIENT_STOCK = "INSUFFICIENT_STOCK",
37
+ INVALID_PRODUCT_OPTION_VALUES = "INVALID_PRODUCT_OPTION_VALUES",
38
+ EMPTY_CART = "EMPTY_CART"
39
+ }
@@ -1 +1 @@
1
- import{__awaiter as t,__generator as e}from'./../../ext/tslib/tslib.es6.js';import{makeAutoObservable as i}from"mobx";import n from"./api.js";import{IkasStorefrontConfig as r}from"@ikas/storefront-config";import{Analytics as a}from"../../analytics/analytics.js";import"../../analytics/head/index.js";import{SaveCartInput as o}from"@ikas/storefront-api";var s="cartId",u=function(){function s(s){var u=this;this.cart=null,this._isLoadingCart=!1,this._isCartLoadFinished=!1,this.addItem=function(i,o,s){return void 0===s&&(s=1),t(u,void 0,void 0,(function(){var t,u,c,d,l,h,v,f,p,m;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),o.hasValidProductOptionValues&&(i.hasStock||i.sellIfOutOfStock)?[4,this.beforeCartOperationCheck()]:[2,!1];case 1:return e.sent(),this.cart&&(t=this.findExistingItem(i,o))?(null===(f=o.productOptionSet)||void 0===f||f.initOptionValues(),[2,this.changeItemQuantity(t,t.quantity+1)]):(u=this.getOptionValues(o),c=(null===(p=this.cart)||void 0===p?void 0:p.id)||localStorage.getItem("cartId"),[4,n.addItemToCart({input:{cartId:c,item:{id:null,quantity:s,variant:{id:i.id,name:o.name}},options:u.length?u:null,priceListId:r.getPriceListId()||null,salesChannelId:r.getSalesChannelId(),storefrontThemeId:r.getStorefrontThemeId()}})]);case 2:return(d=e.sent()).isSuccess&&d.data?[4,this.setCart(d.data)]:[3,4];case 3:e.sent(),e.label=4;case 4:return this.cart&&(l="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(h=this.cart.items.find((function(t){return t.variant.id===i.id})))&&a.addToCart(h,s,l,this.cart)),null===(m=o.productOptionSet)||void 0===m||m.initOptionValues(),[2,d];case 5:return v=e.sent(),console.log(v),[2,!1];case 6:return[2]}}))}))},this.changeItemQuantity=function(i,o){return t(u,void 0,void 0,(function(){var t,s,u,c,d;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),[4,this.beforeCartOperationCheck()];case 1:return e.sent(),[4,n.addItemToCart({input:{cartId:(null===(d=this.cart)||void 0===d?void 0:d.id)||null,item:{id:i.id,quantity:o,variant:{id:i.variant.id,name:i.variant.name}},options:this.createCartLineOptions(i),priceListId:r.getPriceListId()||null,salesChannelId:r.getSalesChannelId(),storefrontThemeId:r.getStorefrontThemeId()}})];case 2:return(t=e.sent()).isSuccess&&t.data?[4,this.setCart(t.data)]:[3,4];case 3:e.sent(),e.label=4;case 4:return this.removeCardIfEmpty(),this.cart&&(s="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(u=i.quantity)>o?a.removeFromCart(i,u-o,this.cart):a.addToCart(i,o-u,s,this.cart)),[2,t];case 5:return c=e.sent(),console.log(c),[2,!1];case 6:return[2]}}))}))},this.removeItem=function(i){return t(u,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,this.changeItemQuantity(i,0)];case 1:return t.sent(),[2]}}))}))},this.removeCart=function(){localStorage.removeItem("cartId"),u.cart=void 0},this.waitUntilInitialized=function(){return new Promise((function(t){var e=setInterval((function(){u.isCartLoadFinished&&(clearInterval(e),t(null))}),100)}))},this.findExistingItem=function(t,e){var i,n,r,a,o;if(!e.productOptionSetId)return null===(i=u.cart)||void 0===i?void 0:i.orderLineItems.find((function(e){return e.variant.id===t.id}));for(var s=u.getOptionValues(e),c=(null===(n=u.cart)||void 0===n?void 0:n.orderLineItems.filter((function(e){var i;return e.variant.id===t.id&&(null===(i=e.options)||void 0===i?void 0:i.length)===s.length})))||[],d=0,l=c;d<l.length;d++){var h=l[d];if(h.options){for(var v=!0,f=function(t){var i=t.values.map((function(t){return t.value})),n=null===(o=null===(a=null===(r=e.productOptionSet)||void 0===r?void 0:r.options)||void 0===a?void 0:a.find((function(e){return e.id===t.productOptionId})))||void 0===o?void 0:o.values;if(!(v=v&&i.length===(null==n?void 0:n.length)&&i.every((function(t){return null==n?void 0:n.includes(t)}))))return"break"},p=0,m=h.options;p<m.length;p++){if("break"===f(m[p]))break}if(v)return h}}},this.saveCouponCode=function(i){return t(u,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.saveCartCouponCode({cartId:this.cart.id,couponCode:i})]:[2,!1];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,t]}}))}))},this.saveCart=function(i){return t(u,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return t=new o(i),[4,n.saveCart({input:t})];case 1:return[2,e.sent()]}}))}))},this.removeCardIfEmpty=function(){var t;0===(null===(t=u.cart)||void 0===t?void 0:t.itemCount)&&u.removeCart()},this.getCart=function(){return t(u,void 0,void 0,(function(){var t,i,r,a,o,s;return e(this,(function(e){switch(e.label){case 0:if(!("undefined"!=typeof localStorage))return[2];e.label=1;case 1:return e.trys.push([1,6,7,8]),this._isLoadingCart=!0,t=null===(o=this.baseStore.customerStore.customer)||void 0===o?void 0:o.id,(i=(null===(s=this.cart)||void 0===s?void 0:s.id)||localStorage.getItem("cartId"))||t?[4,n.getCartById({id:i})]:[2];case 2:return(r=e.sent()).isSuccess&&r.data?[4,this.setCart(r.data)]:[3,4];case 3:return e.sent(),[3,5];case 4:this.removeCart(),e.label=5;case 5:return[3,8];case 6:return a=e.sent(),console.log(a),[3,8];case 7:return this._isLoadingCart=!1,this._isCartLoadFinished=!0,[7];case 8:return[2]}}))}))},this.setCart=function(i){return t(u,void 0,void 0,(function(){return e(this,(function(t){return localStorage.setItem("cartId",i.id),this.cart=i,[2]}))}))},this.beforeCartOperationCheck=function(){return t(u,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return t=localStorage.getItem("cartId"),!this.cart||t?[3,1]:(this.removeCart(),[3,3]);case 1:return this.cart||!t?[3,3]:[4,this.getCart()];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},this.createCartLineOptions=function(t){if(t&&t.options){for(var e=[],i=0,n=t.options;i<n.length;i++){var r=n[i],a={productOptionId:r.productOptionId,productOptionsSetId:r.productOptionsSetId,values:r.values.map((function(t){return t.value}))};e.push(a)}return e}return null},this.getOptionValues=function(t){var e,i=[];return null===(e=t.productOptionSet)||void 0===e||e.options.forEach((function(t){t.values.length&&i.push({productOptionId:t.id,productOptionsSetId:t.productOptionSetId,values:t.values})})),i},this.baseStore=s,i(this)}return Object.defineProperty(s.prototype,"isLoadingCart",{get:function(){return this._isLoadingCart},enumerable:!1,configurable:!0}),Object.defineProperty(s.prototype,"isCartLoadFinished",{get:function(){return this._isCartLoadFinished},enumerable:!1,configurable:!0}),Object.defineProperty(s.prototype,"checkoutId",{get:function(){var t;return null===(t=this.cart)||void 0===t?void 0:t.id},enumerable:!1,configurable:!0}),Object.defineProperty(s.prototype,"checkoutUrl",{get:function(){if(this.checkoutId)return"/checkout?id=".concat(this.checkoutId,"&step=info")},enumerable:!1,configurable:!0}),s}();export{s as CART_LS_KEY,u as IkasCartStore};
1
+ import{__awaiter as t,__generator as e}from'./../../ext/tslib/tslib.es6.js';import{makeAutoObservable as i}from"mobx";import r from"./api.js";import{IkasStorefrontConfig as n}from"@ikas/storefront-config";import{Analytics as o}from"../../analytics/analytics.js";import"../../analytics/head/index.js";import{SaveCartInput as s}from"@ikas/storefront-api";var a,c="cartId",u=function(){function c(c){var u=this;this.cart=null,this._isLoadingCart=!1,this._isCartLoadFinished=!1,this.addItem=function(i,s,c){return void 0===c&&(c=1),t(u,void 0,void 0,(function(){var t,u,d,l,h,v,f,p,I,m;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),s.hasValidProductOptionValues?i.hasStock||i.sellIfOutOfStock?[4,this.beforeCartOperationCheck()]:[2,{success:!1,validationError:a.INSUFFICIENT_STOCK}]:[2,{success:!1,validationError:a.INVALID_PRODUCT_OPTION_VALUES}];case 1:return e.sent(),this.cart&&(t=this.findExistingItem(i,s))?(null===(p=s.productOptionSet)||void 0===p||p.initOptionValues(),[2,this.changeItemQuantity(t,t.quantity+1)]):(u=this.getOptionValues(s),d=(null===(I=this.cart)||void 0===I?void 0:I.id)||localStorage.getItem("cartId"),[4,r.addItemToCart({input:{cartId:d,item:{id:null,quantity:c,variant:{id:i.id,name:s.name}},options:u.length?u:null,priceListId:n.getPriceListId()||null,salesChannelId:n.getSalesChannelId(),storefrontThemeId:n.getStorefrontThemeId()}})]);case 2:return(l=e.sent()).isSuccess&&l.data?[4,this.setCart(l.data)]:[3,4];case 3:e.sent(),e.label=4;case 4:return this.cart&&(h="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(v=this.cart.items.find((function(t){return t.variant.id===i.id})))&&o.addToCart(v,c,h,this.cart)),null===(m=s.productOptionSet)||void 0===m||m.initOptionValues(),[2,{success:l.isSuccess,response:l}];case 5:return f=e.sent(),console.log(f),[2,{success:!1}];case 6:return[2]}}))}))},this.changeItemQuantity=function(i,s){return t(u,void 0,void 0,(function(){var t,a,c,u,d;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),[4,this.beforeCartOperationCheck()];case 1:return e.sent(),[4,r.addItemToCart({input:{cartId:(null===(d=this.cart)||void 0===d?void 0:d.id)||null,item:{id:i.id,quantity:s,variant:{id:i.variant.id,name:i.variant.name}},options:this.createCartLineOptions(i),priceListId:n.getPriceListId()||null,salesChannelId:n.getSalesChannelId(),storefrontThemeId:n.getStorefrontThemeId()}})];case 2:return(t=e.sent()).isSuccess&&t.data?[4,this.setCart(t.data)]:[3,4];case 3:e.sent(),e.label=4;case 4:return this.removeCardIfEmpty(),this.cart&&(a="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(c=i.quantity)>s?o.removeFromCart(i,c-s,this.cart):o.addToCart(i,s-c,a,this.cart)),[2,{success:t.isSuccess}];case 5:return u=e.sent(),console.log(u),[2,{success:!1}];case 6:return[2]}}))}))},this.removeItem=function(i){return t(u,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,this.changeItemQuantity(i,0)];case 1:return[2,t.sent()]}}))}))},this.removeCart=function(){localStorage.removeItem("cartId"),u.cart=void 0},this.waitUntilInitialized=function(){return new Promise((function(t){var e=setInterval((function(){u.isCartLoadFinished&&(clearInterval(e),t(null))}),100)}))},this.findExistingItem=function(t,e){var i,r,n,o,s;if(!e.productOptionSetId)return null===(i=u.cart)||void 0===i?void 0:i.orderLineItems.find((function(e){return e.variant.id===t.id}));for(var a=u.getOptionValues(e),c=(null===(r=u.cart)||void 0===r?void 0:r.orderLineItems.filter((function(e){var i;return e.variant.id===t.id&&(null===(i=e.options)||void 0===i?void 0:i.length)===a.length})))||[],d=0,l=c;d<l.length;d++){var h=l[d];if(h.options){for(var v=!0,f=function(t){var i=t.values.map((function(t){return t.value})),r=null===(s=null===(o=null===(n=e.productOptionSet)||void 0===n?void 0:n.options)||void 0===o?void 0:o.find((function(e){return e.id===t.productOptionId})))||void 0===s?void 0:s.values;if(!(v=v&&i.length===(null==r?void 0:r.length)&&i.every((function(t){return null==r?void 0:r.includes(t)}))))return"break"},p=0,I=h.options;p<I.length;p++){if("break"===f(I[p]))break}if(v)return h}}},this.saveCouponCode=function(i){return t(u,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,r.saveCartCouponCode({cartId:this.cart.id,couponCode:i})]:[2,{success:!1,validationError:a.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.saveCart=function(i){return t(u,void 0,void 0,(function(){var t,n;return e(this,(function(e){switch(e.label){case 0:return t=new s(i),[4,r.saveCart({input:t})];case 1:return[2,{success:(n=e.sent()).isSuccess,response:n}]}}))}))},this.removeCardIfEmpty=function(){var t;0===(null===(t=u.cart)||void 0===t?void 0:t.itemCount)&&u.removeCart()},this.getCart=function(){return t(u,void 0,void 0,(function(){var t,i,n,o,s,a;return e(this,(function(e){switch(e.label){case 0:if(!("undefined"!=typeof localStorage))return[2];e.label=1;case 1:return e.trys.push([1,6,7,8]),this._isLoadingCart=!0,t=null===(s=this.baseStore.customerStore.customer)||void 0===s?void 0:s.id,(i=(null===(a=this.cart)||void 0===a?void 0:a.id)||localStorage.getItem("cartId"))||t?[4,r.getCartById({id:i})]:[2];case 2:return(n=e.sent()).isSuccess&&n.data?[4,this.setCart(n.data)]:[3,4];case 3:return e.sent(),[3,5];case 4:this.removeCart(),e.label=5;case 5:return[3,8];case 6:return o=e.sent(),console.log(o),[3,8];case 7:return this._isLoadingCart=!1,this._isCartLoadFinished=!0,[7];case 8:return[2]}}))}))},this.setCart=function(i){return t(u,void 0,void 0,(function(){return e(this,(function(t){return localStorage.setItem("cartId",i.id),this.cart=i,[2]}))}))},this.beforeCartOperationCheck=function(){return t(u,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return t=localStorage.getItem("cartId"),!this.cart||t?[3,1]:(this.removeCart(),[3,3]);case 1:return this.cart||!t?[3,3]:[4,this.getCart()];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},this.createCartLineOptions=function(t){if(t&&t.options){for(var e=[],i=0,r=t.options;i<r.length;i++){var n=r[i],o={productOptionId:n.productOptionId,productOptionsSetId:n.productOptionsSetId,values:n.values.map((function(t){return t.value}))};e.push(o)}return e}return null},this.getOptionValues=function(t){var e,i=[];return null===(e=t.productOptionSet)||void 0===e||e.options.forEach((function(t){t.values.length&&i.push({productOptionId:t.id,productOptionsSetId:t.productOptionSetId,values:t.values})})),i},this.baseStore=c,i(this)}return Object.defineProperty(c.prototype,"isLoadingCart",{get:function(){return this._isLoadingCart},enumerable:!1,configurable:!0}),Object.defineProperty(c.prototype,"isCartLoadFinished",{get:function(){return this._isCartLoadFinished},enumerable:!1,configurable:!0}),Object.defineProperty(c.prototype,"checkoutId",{get:function(){var t;return null===(t=this.cart)||void 0===t?void 0:t.id},enumerable:!1,configurable:!0}),Object.defineProperty(c.prototype,"checkoutUrl",{get:function(){if(this.checkoutId)return"/checkout?id=".concat(this.checkoutId,"&step=info")},enumerable:!1,configurable:!0}),c}();!function(t){t.INSUFFICIENT_STOCK="INSUFFICIENT_STOCK",t.INVALID_PRODUCT_OPTION_VALUES="INVALID_PRODUCT_OPTION_VALUES",t.EMPTY_CART="EMPTY_CART"}(a||(a={}));export{c as CART_LS_KEY,a as IkasCartOperationValidationError,u as IkasCartStore};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikas/storefront",
3
- "version": "4.0.0-alpha.86",
3
+ "version": "4.0.0-beta.1",
4
4
  "description": "Storefront functionality for ikas storefront themes.",
5
5
  "author": "ikas",
6
6
  "license": "ISC",
@@ -23,11 +23,11 @@
23
23
  "libphonenumber-js": "^1.10.6"
24
24
  },
25
25
  "devDependencies": {
26
- "@ikas/storefront-api": "^4.0.0-alpha.86",
27
- "@ikas/storefront-config": "^4.0.0-alpha.86",
28
- "@ikas/storefront-model-functions": "^4.0.0-alpha.86",
29
- "@ikas/storefront-models": "^4.0.0-alpha.86",
30
- "@ikas/storefront-providers": "^4.0.0-alpha.86",
26
+ "@ikas/storefront-api": "^4.0.0-beta.1",
27
+ "@ikas/storefront-config": "^4.0.0-beta.1",
28
+ "@ikas/storefront-model-functions": "^4.0.0-beta.1",
29
+ "@ikas/storefront-models": "^4.0.0-beta.1",
30
+ "@ikas/storefront-providers": "^4.0.0-beta.1",
31
31
  "@rollup/plugin-commonjs": "^22.0.0",
32
32
  "@rollup/plugin-json": "^4.1.0",
33
33
  "@rollup/plugin-node-resolve": "^13.3.0",
@@ -53,11 +53,11 @@
53
53
  "query-string": "^6.13.8"
54
54
  },
55
55
  "peerDependencies": {
56
- "@ikas/storefront-api": "^4.0.0-alpha.86",
57
- "@ikas/storefront-config": "^4.0.0-alpha.86",
58
- "@ikas/storefront-model-functions": "^4.0.0-alpha.86",
59
- "@ikas/storefront-models": "^4.0.0-alpha.86",
60
- "@ikas/storefront-providers": "^4.0.0-alpha.86",
56
+ "@ikas/storefront-api": "^4.0.0-beta.1",
57
+ "@ikas/storefront-config": "^4.0.0-beta.1",
58
+ "@ikas/storefront-model-functions": "^4.0.0-beta.1",
59
+ "@ikas/storefront-models": "^4.0.0-beta.1",
60
+ "@ikas/storefront-providers": "^4.0.0-beta.1",
61
61
  "mobx": "^6.1.3",
62
62
  "mobx-react-lite": "^3.1.5",
63
63
  "next": "12.2.0",