-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
49 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,44 @@ | ||
import { common_OrderItemInsert } from "@/api/proto-http/frontend"; | ||
import CheckoutForm from "@/components/forms/CheckoutForm"; | ||
import NewOrderForm from "@/components/forms/NewOrderForm"; | ||
import { redirect } from "next/navigation"; | ||
import CoreLayout from "@/components/layouts/CoreLayout"; | ||
import { | ||
getCartProductSlugAndSizeFromKey, | ||
getCookieCart, | ||
} from "@/lib/utils/cart"; | ||
|
||
export default async function Page() { | ||
const orderItems = createOrderArray(); | ||
const cartData = getCookieCart(); | ||
const cartProducts = cartData?.products; | ||
|
||
function createOrderArray(): common_OrderItemInsert[] { | ||
const cartItems = getCookieCart(); | ||
if (!cartItems || !cartItems.products) return []; | ||
if (!cartProducts || !Object.keys(cartProducts)) return redirect("/cart"); | ||
|
||
const orderArray: common_OrderItemInsert[] = []; | ||
|
||
for (const key in cartItems.products) { | ||
const productData = cartItems.products[key]; | ||
const orderItems = Object.entries(cartProducts).reduce( | ||
(acc, [key, value]) => { | ||
const slugAndSize = getCartProductSlugAndSizeFromKey(key); | ||
|
||
if (!slugAndSize) continue; | ||
|
||
const { slug, size } = slugAndSize; | ||
const productId = getProductIdFromSlug(slug); | ||
|
||
//TO-DO map size id from dictionary | ||
// const sizeId = getSizeIdBySize(size); | ||
if (!slugAndSize) return acc; | ||
|
||
const sizeId = 1; | ||
const [_, __, ___, id] = slugAndSize.slug | ||
.replaceAll("/product/", "") | ||
.split("/"); | ||
|
||
const orderItem: common_OrderItemInsert = { | ||
productId, | ||
quantity: productData.quantity, | ||
sizeId, | ||
const item = { | ||
productId: Number(id), | ||
quantity: value.quantity, | ||
sizeId: Number(slugAndSize.size), | ||
}; | ||
|
||
orderArray.push(orderItem); | ||
} | ||
acc.push(item); | ||
|
||
return orderArray; | ||
} | ||
|
||
function getProductIdFromSlug(slug: string): number | undefined { | ||
const [gender, brand, name, id] = slug | ||
?.replaceAll("/product/", "") | ||
.split("/"); | ||
|
||
return id ? parseInt(id) : undefined; | ||
} | ||
return acc; | ||
}, | ||
[] as common_OrderItemInsert[], | ||
); | ||
|
||
return ( | ||
<CoreLayout> | ||
<CheckoutForm orderItems={orderItems} /> | ||
<NewOrderForm orderItems={orderItems} /> | ||
</CoreLayout> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters