Skip to content

Commit

Permalink
Feature/apply promo (#121)
Browse files Browse the repository at this point in the history
* wip apply promo

* apply promo

* added promo on submit order

* wip validate order items

* wip

* update cookies on client side

* fix flow

---------

Co-authored-by: maksim hodasevich <maksim.hodasevich@gmail.com>
  • Loading branch information
mikevelko and dogfrogfog authored Aug 23, 2024
1 parent 290516b commit d984fa2
Show file tree
Hide file tree
Showing 31 changed files with 705 additions and 622 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"react-hook-form": "^7.52.0",
"react-intersection-observer": "^9.13.0",
"react-photo-view": "^1.2.4",
"sonner": "^1.5.0",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 30 additions & 40 deletions src/actions/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,68 @@ import {
updateCookieCartProduct,
} from "@/lib/utils/cart";

export const GRBPWR_CART = "grbpwr-cart";

export async function addCartProduct({
slug,
id,
size,
price,
quantity = 1,
}: {
slug: string;
id: number;
size: string;
price: number;
quantity?: number;
}) {
"use server";

const cartData = getCookieCart();

try {
if (!cartData) {
createCookieCartProduct({ productSlug: slug, size, price });
createCookieCartProduct({ id, size, quantity });

return;
}

const productKey = getCartProductKey(slug, size);
const cartProduct = cartData.products[productKey];
const newProduct = {
quantity: 0,
price: 0,
};
const productKey = getCartProductKey(id, size);
const cartProductQuantity = cartData.products[productKey];
let newProductQuantity = 0;

if (cartProduct) {
newProduct.quantity = cartProduct.quantity + 1;
newProduct.price = cartProduct.price + price;
if (cartProductQuantity > 0) {
newProductQuantity = cartProductQuantity + quantity;
} else {
newProduct.quantity = 1;
newProduct.price = price;
newProductQuantity = quantity;
}

updateCookieCartProduct({ productSlug: slug, size, data: newProduct });
updateCookieCartProduct({
id,
size,
quantity: newProductQuantity,
});
} catch (error) {
console.log("failed to parse cart", error);
}
}

export async function removeCartProduct({
productSlug,
id,
size,
}: {
productSlug: string;
id: number;
size: string;
}) {
"use server";

try {
removeCookieCartProduct(productSlug, size);
removeCookieCartProduct(id, size);
} catch (error) {
console.log("failed to parse cart", error);
}
}

export async function changeCartProductQuantity({
slug,
id,
size,
operation,
price,
}: {
slug: string;
price: number;
id: number;
size: string;
operation: "increase" | "decrease";
}) {
Expand All @@ -83,34 +78,29 @@ export async function changeCartProductQuantity({
try {
if (!cartData) return;

const productKey = getCartProductKey(slug, size);
const cartProduct = cartData.products[productKey];
const productKey = getCartProductKey(id, size);
const cartProductQuantity = cartData.products[productKey];

if (operation === "decrease" && cartProduct.quantity === 1) {
removeCookieCartProduct(slug, size);
if (operation === "decrease" && cartProductQuantity === 1) {
removeCookieCartProduct(id, size);

return;
}

const newProduct = {
quantity: cartProduct.quantity,
price: cartProduct.price,
};
let newProductQuantity = cartProductQuantity;

if (operation === "decrease") {
newProduct.quantity = cartProduct.quantity - 1;
newProduct.price = cartProduct.price - price;
newProductQuantity = cartProductQuantity - 1;
}

if (operation === "increase") {
newProduct.quantity = cartProduct.quantity + 1;
newProduct.price = cartProduct.price + price;
newProductQuantity = cartProductQuantity + 1;
}

updateCookieCartProduct({
productSlug: slug,
id,
size,
data: newProduct,
quantity: newProductQuantity,
});
} catch (error) {
console.log("failed to parse cart", error);
Expand Down
24 changes: 14 additions & 10 deletions src/api/proto-http/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type Archive = {
// ArchiveBody represents the insertable fields of an archive.
export type ArchiveBody = {
heading: string | undefined;
description: string | undefined;
text: string | undefined;
};

// ArchiveItemFull represents an item within an archive.
Expand All @@ -65,7 +65,7 @@ export type ArchiveItemFull = {
export type ArchiveItem = {
media: MediaFull | undefined;
url: string | undefined;
title: string | undefined;
name: string | undefined;
};

// ArchiveNew represents a new archive with items for insertion.
Expand All @@ -77,7 +77,7 @@ export type ArchiveNew = {
export type ArchiveItemInsert = {
mediaId: number | undefined;
url: string | undefined;
title: string | undefined;
name: string | undefined;
};

export type Address = {
Expand All @@ -86,12 +86,12 @@ export type Address = {
};

export type AddressInsert = {
street: string | undefined;
houseNumber: string | undefined;
apartmentNumber: string | undefined;
city: string | undefined;
state: string | undefined;
country: string | undefined;
state: string | undefined;
city: string | undefined;
addressLineOne: string | undefined;
addressLineTwo: string | undefined;
company: string | undefined;
postalCode: string | undefined;
};

Expand Down Expand Up @@ -433,7 +433,7 @@ export type OrderNew = {
shippingAddress: AddressInsert | undefined;
billingAddress: AddressInsert | undefined;
buyer: BuyerInsert | undefined;
paymentMethodId: number | undefined;
paymentMethod: PaymentMethodNameEnum | undefined;
shipmentCarrierId: number | undefined;
promoCode: string | undefined;
};
Expand Down Expand Up @@ -474,8 +474,11 @@ export type OrderItem = {
thumbnail: string | undefined;
productName: string | undefined;
productPrice: string | undefined;
productPriceWithSale: string | undefined;
productSalePercentage: string | undefined;
productBrand: string | undefined;
slug: string | undefined;
color: string | undefined;
categoryId: number | undefined;
sku: string | undefined;
orderItem: OrderItemInsert | undefined;
Expand Down Expand Up @@ -531,18 +534,19 @@ export type HeroItemInsert = {
mediaId: number | undefined;
exploreLink: string | undefined;
exploreText: string | undefined;
isMain: boolean | undefined;
};

export type HeroItem = {
media: MediaFull | undefined;
exploreLink: string | undefined;
exploreText: string | undefined;
isMain: boolean | undefined;
};

export type HeroFull = {
id: number | undefined;
createdAt: wellKnownTimestamp | undefined;
main: HeroItem | undefined;
ads: HeroItem[] | undefined;
productsFeatured: Product[] | undefined;
};
Expand Down
Loading

0 comments on commit d984fa2

Please sign in to comment.