Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
844cfe9
feat: add update total price method
Friday-13 Jun 17, 2024
3b88a77
feat: show discounted price only when discount applyed
Friday-13 Jun 18, 2024
0c031cc
feat: add fetching actual promocodes to main
Friday-13 Jun 18, 2024
a8a49e9
feat: add register user
Jun 18, 2024
5586435
Merge pull request #213 from Friday-13/feat/RSS-ECOMM-4_16
Friday-13 Jun 18, 2024
3f8b300
Merge branch 'feat/basket-page' of github.com:Friday-13/eCommerce-App…
Jun 18, 2024
fe5d817
Merge pull request #214 from Friday-13/feat/add-remove-product-cart
Golosova76 Jun 18, 2024
1c48691
feat: add delete product
Jun 18, 2024
c1f0ef4
feat: add delete product
Golosova76 Jun 18, 2024
c303856
Merge branch 'feat/add-remove-product-cart' of github.com:Friday-13/e…
Friday-13 Jun 18, 2024
8ce09a7
feat: add updating anonymus cart, add creating anonymus cart
Friday-13 Jun 18, 2024
f1a81a7
feat: add removing product from anonymus cart
Friday-13 Jun 18, 2024
f2f3379
feat: add creating castomer cart
Friday-13 Jun 19, 2024
8474c24
feat: add cart merging
Friday-13 Jun 19, 2024
17719b3
refactor: refactor style about us page
Jun 19, 2024
29c2660
feat: add adaptive page about
Jun 19, 2024
afc6719
refactor: refactor basket api on packet content
Friday-13 Jun 19, 2024
bd0ac6d
feat: add count digits
Jun 19, 2024
3d82ae4
feat: add jest test cookie
Jun 19, 2024
0bffbbd
feat: add promocodes applying
Friday-13 Jun 19, 2024
7d9ce37
fix: load basket only after cart creating
Friday-13 Jun 19, 2024
ae81290
feat: add updating summary on removing
Friday-13 Jun 19, 2024
780a529
refactor: make cartdata optional
Friday-13 Jun 19, 2024
7fe6092
refactor: return Cartdata value
Friday-13 Jun 19, 2024
b7f6d81
Merge branch 'release/basket-about_us' of github.com:Friday-13/eComme…
Friday-13 Jun 19, 2024
62b08c6
Merge pull request #217 from Friday-13/fix/basket-configuration
Friday-13 Jun 19, 2024
de9094b
Merge branch 'feat/add-remove-product-cart' of github.com:Friday-13/e…
Friday-13 Jun 19, 2024
683e41e
feat: add adding product from catalog
Friday-13 Jun 19, 2024
2c0a0fd
feat: add checking if current product is already inside the cart
Friday-13 Jun 19, 2024
927e75c
feat: add adding/removing product quantity
Friday-13 Jun 19, 2024
cdb23cd
refactor: rewrire product page with current-cart implementation
Friday-13 Jun 19, 2024
87705a6
feat: add clear cart layout
Friday-13 Jun 19, 2024
45bf9fb
feat: add removing cart and creatin new
Friday-13 Jun 19, 2024
407ecab
fix: fix export error
Friday-13 Jun 19, 2024
6c919e6
feat: add hiding/showing submit block on summary
Friday-13 Jun 19, 2024
8e5646a
feat: add updating cart view after delating products
Friday-13 Jun 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import currentCart from '@services/current-cart';
import isPageAccessable from '@utils/access-control';
import CookieManager from '@utils/cookie';

import {
Error404,
MainPageView,
Expand Down Expand Up @@ -39,6 +41,9 @@ class App {

window.addEventListener('hashchange', this.route);
window.addEventListener('load', this.route);

const userId = CookieManager.getUserId();
currentCart.initCurrentCart(userId);
}

private addHeader(): void {
Expand Down Expand Up @@ -77,8 +82,8 @@ class App {

if (route === 'product' && productId) {
this.hideFooterHeader = false;
const userId = CookieManager.getUserId();
this.mainView.page = new ProductPageView(productId, userId);

this.mainView.page = new ProductPageView(productId);
} else {
switch (window.location.hash) {
case '#main':
Expand Down
8 changes: 6 additions & 2 deletions src/app/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ export interface ILineItem {
export interface IVariant {
id: number;
sku?: string;
prices: IPrice[];
prices: IPrices[];
images: { url: string }[];
}

export interface IPrice {
value: IMoney;
discounted?: IDiscounted;
}

export interface IPrices {
value: IMoney;
discounted?: IDiscounted | null;
}

export interface IMoney {
Expand Down
33 changes: 33 additions & 0 deletions src/app/services/apply-promocode-to-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ClientResponse, Cart } from '@commercetools/platform-sdk';
import ApiRoot from './api-root';

export default function applyPromocodeToCart(
cartId: string,
cartVersion: number,
code: string,
successCallback: (cartData: Cart) => void,
errorCallback: (message: string) => void
) {
ApiRoot.root
.carts()
.withId({ ID: cartId })
.post({
body: {
version: cartVersion,
actions: [
{
action: 'addDiscountCode',
code,
},
],
},
})
.execute()
.then((response: ClientResponse<Cart>) => {
const cartData: Cart = response.body;
successCallback(cartData);
})
.catch((error: ClientResponse<{ message: string }>) => {
errorCallback(error.body.message);
});
}
16 changes: 8 additions & 8 deletions src/app/services/cart-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export const createCartData = (cart: Cart): ICartData => {
centAmount: price.value.centAmount,
fractionDigits: price.value.fractionDigits,
},
discountedPrice: price.discounted
discounted: price.discounted
? {
// ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅ΠΌ, Π΅ΡΡ‚ΡŒ Π»ΠΈ скидочная Ρ†Π΅Π½Π°
currencyCode: price.discounted.value.currencyCode,
centAmount: price.discounted.value.centAmount,
fractionDigits: price.discounted.value.fractionDigits,
value: {
currencyCode: price.discounted.value.currencyCode,
centAmount: price.discounted.value.centAmount,
fractionDigits: price.discounted.value.fractionDigits,
},
}
: null, // Если Π½Π΅Ρ‚ скидочной Ρ†Π΅Π½Ρ‹, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ null
}))
Expand Down Expand Up @@ -97,7 +98,7 @@ export const createCartData = (cart: Cart): ICartData => {

export const getCartById = (
cartId: string,
successCallback: (cartData: ICartData) => void,
successCallback: (cartData: Cart) => void,
errorCallback: (message: string) => void
): void => {
ApiRoot.root
Expand All @@ -106,8 +107,7 @@ export const getCartById = (
.get()
.execute()
.then(({ body }) => {
const cartData = createCartData(body);
successCallback(cartData);
successCallback(body);
})
.catch((error) => {
errorCallback(`Failed to load cart: ${error.message}`);
Expand Down
104 changes: 72 additions & 32 deletions src/app/services/cart-handler.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import { showSucessMessage } from '@utils/toast-messages';
import { showErrorMessage } from '@utils/toast-messages';
import CookieManager from '@utils/cookie';
import { addProductToCart } from './add-product-cart';
import { createAnonymousCart, createCustomerCart } from './carts';

class CartHandler {
public currentCartId: string | null = null;

private currentCartVersion: number | null = null;
public currentCartVersion: number | null = null;

public currentCustomerCartId: string | null = null;

public currentCustomerCartVersion: number | null = null;

private userId: string | null = null;

constructor(userId: string | null) {
this.userId = userId;
if (!userId) {
this.loadCartFromLocalStorage();
} else {
this.loadCartFromServer(userId);
}
this.currentCartId = null;
this.currentCartVersion = null;
this.currentCustomerCartId = null;
this.currentCustomerCartVersion = null;

// if (!userId) {
// this.loadCartFromLocalStorage();
// } else {
// this.loadCartAuthFromLocalStorage();
// }
}

public handleAddToCart(productId: string, quantity: number): void {
if (!this.currentCartId || !this.currentCartVersion) {
// ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅ΠΌ, ΠΊΠ°ΠΊΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΠΊΠΎΡ€Π·ΠΈΠ½Ρ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ
let cartId;
let cartVersion;

if (this.userId) {
// ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ°, зарСгистрирован Π»ΠΈ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ
cartId = this.currentCustomerCartId;
cartVersion = this.currentCustomerCartVersion;
} else {
// Для Π°Π½ΠΎΠ½ΠΈΠΌΠ½Ρ‹Ρ… ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ
cartId = this.currentCartId;
cartVersion = this.currentCartVersion;
}

if (!cartId || !cartVersion) {
this.createCartAndAddProduct(productId, quantity);
} else {
this.addProductToExistingCart(
this.currentCartId,
this.currentCartVersion,
productId,
quantity
);
this.addProductToExistingCart(cartId, cartVersion, productId, quantity);
}
}

Expand All @@ -36,18 +55,19 @@ class CartHandler {
createCustomerCart(
this.userId,
(cartData) => {
this.currentCartId = cartData.id;
this.currentCartVersion = cartData.version;
this.currentCustomerCartId = cartData.id;
this.currentCustomerCartVersion = cartData.version;
this.saveCartAuthToLocalStorage();
this.addProductToExistingCart(
this.currentCartId,
this.currentCartVersion,
this.currentCustomerCartId,
this.currentCustomerCartVersion,
productId,
quantity
);
},
(errorMessage) => {
const SUCSESS_MSG = `Error creating user cart: ${errorMessage}`;
showSucessMessage(SUCSESS_MSG);
showErrorMessage(SUCSESS_MSG);
}
);
} else {
Expand All @@ -65,7 +85,7 @@ class CartHandler {
},
(errorMessage) => {
const SUCSESS_MSG = `Error creating anonymous cart: ${errorMessage}`;
showSucessMessage(SUCSESS_MSG);
showErrorMessage(SUCSESS_MSG);
}
);
}
Expand All @@ -83,24 +103,27 @@ class CartHandler {
productId,
quantity,
(cartData) => {
this.currentCartId = cartData.id;
this.currentCartVersion = cartData.version;
console.log(
`User ID before deciding to save to localStorage: ${this.userId}`
);
if (!this.userId) {
if (this.userId) {
// Для зарСгистрированных ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ
this.currentCustomerCartId = cartData.id;
this.currentCustomerCartVersion = cartData.version;
this.saveCartAuthToLocalStorage();
} else {
// Для Π°Π½ΠΎΠ½ΠΈΠΌΠ½Ρ‹Ρ… ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ
this.currentCartId = cartData.id;
this.currentCartVersion = cartData.version;
this.saveCartToLocalStorage();
}
console.log('Product added to cart:', cartData);
},
(errorMessage) => {
const SUCSESS_MSG = `Error adding product to cart: ${errorMessage}`;
showSucessMessage(SUCSESS_MSG);
showErrorMessage(SUCSESS_MSG);
}
);
}

private saveCartToLocalStorage(): void {
public saveCartToLocalStorage(): void {
const cartData = {
currentCartId: this.currentCartId,
currentCartVersion: this.currentCartVersion,
Expand All @@ -117,9 +140,26 @@ class CartHandler {
}
}

// ΠΌΠ΅Ρ‚ΠΎΠ΄ для Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ ΠΊΠΎΡ€Π·ΠΈΠ½Ρ‹ зарСгистрированного ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ с сСрвСра
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
private loadCartFromServer(userId: string): void {}
public saveCartAuthToLocalStorage(): void {
const cartData = {
currentCustomerCartId: this.currentCustomerCartId,
currentCustomerCartVersion: this.currentCustomerCartVersion,
};
localStorage.setItem('customerCart', JSON.stringify(cartData));
}

public loadCartAuthFromLocalStorage(): void {
const savedCart = localStorage.getItem('customerCart');
if (savedCart) {
const cartData = JSON.parse(savedCart);
this.currentCustomerCartId = cartData.currentCustomerCartId;
this.currentCustomerCartVersion = cartData.currentCustomerCartVersion;
}
}
}

export default CartHandler;
const userId = CookieManager.getUserId();

const cartHandler = new CartHandler(userId);

export default cartHandler;
60 changes: 58 additions & 2 deletions src/app/services/carts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const createAnonymousCart = (
.execute()
.then((response: ClientResponse<Cart>) => {
const cartData: Cart = response.body;
successCallback(cartData);
if (cartData.id) {
LocalStorageManager.setCartId(cartData.id);
successCallback(cartData);
} else {
errorCallback('Failed to retrieve cart ID');
}
Expand All @@ -46,10 +46,10 @@ export const createCustomerCart = (
.execute()
.then((response: ClientResponse<Cart>) => {
const cartData: Cart = response.body;
successCallback(cartData);
if (cartData.id) {
LocalStorageManager.setCartId(cartData.id);
console.log('Cart ID saved to localStorage:', cartData.id);
successCallback(cartData);
} else {
errorCallback('Failed to retrieve cart ID');
}
Expand All @@ -58,3 +58,59 @@ export const createCustomerCart = (
errorCallback(error.body.message);
});
};

export const isCustomerCartExist = (
customerId: string,
successCallback: (cartData: Cart) => void,
errorCallback: (customerId: string) => void
): void => {
ApiRoot.root
.carts()
.withCustomerId({ customerId })
.get()
.execute()
.then((response) => {
const { body } = response;
successCallback(body);
})
.catch((err) => {
if (err.statusCode === 404) {
errorCallback(customerId);
} else {
throw err;
}
});
};

export const removeProductFromCart = (
cartId: string,
cartVersion: number,
productId: string,
successCallback: (cartData: Cart) => void,
errorCallback: (message: string) => void,
quantity?: number
): void => {
ApiRoot.root
.carts()
.withId({ ID: cartId })
.post({
body: {
version: cartVersion,
actions: [
{
action: 'removeLineItem',
lineItemId: productId,
quantity,
},
],
},
})
.execute()
.then((response: ClientResponse<Cart>) => {
const cartData: Cart = response.body;
successCallback(cartData);
})
.catch((error: ClientResponse<{ message: string }>) => {
errorCallback(error.body.message);
});
};
Loading