Skip to content

Commit

Permalink
get cart info from checkout info storage instead of legacy cart record
Browse files Browse the repository at this point in the history
  • Loading branch information
juvirez committed Oct 2, 2023
1 parent be5c718 commit 6ad83bc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ecwid/sdk",
"version": "0.6.0",
"version": "0.7.0",
"description": "Lightweight SDK library to access Ecwid API functions.",
"author": "Ecwid",
"license": "Apache-2.0",
Expand Down
12 changes: 12 additions & 0 deletions src/converter/checkout-converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LocalStorageCheckout } from '../type/local-storage';
import { Cart } from '../type/cart';

export default class CheckoutConverter {
toCart(localStorageCart: LocalStorageCheckout): Cart {
return {
cartId: '',
items: [],
productsQuantity: localStorageCart.itemsCount ?? 0,
};
}
}
18 changes: 18 additions & 0 deletions src/service/cart-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cart } from '../type/cart';
import CartConverter from '../converter/cart-converter';
import CheckoutConverter from '../converter/checkout-converter';

/**
* Ecommerce SDK cart service. Contains cart operations.
Expand All @@ -10,16 +11,22 @@ class CartService {

private readonly cartConverter: CartConverter;

private readonly checkoutConverter: CheckoutConverter;

private readonly localStorageCartKey: string;

private readonly localStorageCheckoutKey: string;

private readonly defaultStoreLocationPath: string;

constructor(storeId: number, storeLocationPath?: string) {
this.localStorageCartKey = this.getCartKey(storeId);
this.localStorageCheckoutKey = this.getCheckoutKey(storeId);
this.defaultStoreLocationPath = storeLocationPath
? this.unifyStoreLocationPath(storeLocationPath)
: this.DEFAULT_STORE_LOCATION_PATH;
this.cartConverter = new CartConverter();
this.checkoutConverter = new CheckoutConverter();
}

/**
Expand All @@ -30,6 +37,13 @@ class CartService {
if (typeof window === 'undefined' || !window || !window.localStorage) {
return this.createEmptyCartPromise();
}
const checkoutRecord = window.localStorage.getItem(this.localStorageCheckoutKey);
if (checkoutRecord != null) {
const localStorageCheckout = JSON.parse(checkoutRecord);
const cart = this.checkoutConverter.toCart(localStorageCheckout);
return this.createCartPromise(cart);
}

const cartRecord = window.localStorage.getItem(this.localStorageCartKey);
if (cartRecord == null) {
return this.createEmptyCartPromise();
Expand Down Expand Up @@ -70,6 +84,10 @@ class CartService {
return `PSecwid__${storeId}PScart`;
}

private getCheckoutKey(storeId: number) {
return `ec-${storeId}-checkout`;
}

private createCartPromise(cart: Cart): Promise<Cart> {
return Promise.resolve(cart);
}
Expand Down
5 changes: 5 additions & 0 deletions src/type/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export interface LocalStorageCart {
readonly cartId: string;
readonly order: LocalStorageOrder;
}

export interface LocalStorageCheckout {
readonly id?: string;
readonly itemsCount: number;
}

0 comments on commit 6ad83bc

Please sign in to comment.