From 6775f7363b6f30fb642a5cf9e77cd9fb84e37250 Mon Sep 17 00:00:00 2001 From: paolosantarsiero Date: Fri, 3 Jan 2025 03:05:01 +0100 Subject: [PATCH] refactor: change layout profile and home page --- app/checkout/page.tsx | 4 +- app/page.tsx | 73 ++++- app/profile/area/page.tsx | 7 + app/profile/layout.tsx | 66 ++++ app/profile/orders/page.tsx | 2 +- app/profile/page.tsx | 105 +------ components/button/logout.tsx | 2 +- components/cart/cart-context.tsx | 19 +- components/grid/three-items.tsx | 8 +- components/layout/breadcrumb.tsx | 37 +++ components/shipping/form.tsx | 13 +- lib/utils.ts | 7 + lib/woocomerce/models/base.ts | 1 + lib/wordpress/models/client.ts | 429 ++++++++++++++++++++++++++ lib/wordpress/models/clientOptions.ts | 59 ++++ lib/wordpress/wordpress.ts | 11 + middleware.ts | 8 +- tsconfig.json | 3 + 18 files changed, 716 insertions(+), 138 deletions(-) create mode 100644 app/profile/area/page.tsx create mode 100644 app/profile/layout.tsx create mode 100644 components/layout/breadcrumb.tsx create mode 100644 lib/wordpress/models/client.ts create mode 100644 lib/wordpress/models/clientOptions.ts create mode 100644 lib/wordpress/wordpress.ts diff --git a/app/checkout/page.tsx b/app/checkout/page.tsx index 6c2f2e0aa..aafc8cfaf 100644 --- a/app/checkout/page.tsx +++ b/app/checkout/page.tsx @@ -80,13 +80,13 @@ export default function CheckoutPage() {
- + setSameBilling(v)} className="mt-2"> Use same address for billing? - +
diff --git a/app/page.tsx b/app/page.tsx index 7b0b81e28..3201b60f5 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,18 +1,77 @@ -import { Carousel } from 'components/carousel'; -import { ThreeItemGrid } from 'components/grid/three-items'; - export const metadata = { description: 'High-performance ecommerce store built with Next.js, Vercel, and Shopify.', openGraph: { type: 'website' } }; +import { Carousel } from 'components/carousel'; +import { ThreeItemGrid } from 'components/grid/three-items'; +import { Category } from 'lib/woocomerce/models/base'; +import { Product } from 'lib/woocomerce/models/product'; +import { woocommerce } from 'lib/woocomerce/woocommerce'; +import { wordpress } from 'lib/wordpress/wordpress'; +import React from 'react'; export default async function HomePage() { + const categories: Category[] = await woocommerce.get('products/categories'); + const productsByCategory: Record = {}; + await Promise.all( + categories.map((category) => + woocommerce.get('products', { category: category.id.toString() }).then((products) => { + productsByCategory[category.name] = products; + }) + ) + ); + const posts = await wordpress.get('posts'); + return ( - <> - - - +
+ {categories.map((category, index) => ( +
+
+ + {category.name} + +
+
+ + {category.description} + +
+ + {productsByCategory[category.name] && ( + + )} + + {index === 1 && ( +
+ Top products + +
+ )} +
+ ))} +
+ Latest posts +
+ {posts.map((post: any) => ( +
+ {post.title.rendered} +
+

{post.title.rendered}

+
+
+
+ ))} +
+
+
); } diff --git a/app/profile/area/page.tsx b/app/profile/area/page.tsx new file mode 100644 index 000000000..61385631e --- /dev/null +++ b/app/profile/area/page.tsx @@ -0,0 +1,7 @@ +export default async function PersonalArea() { + return ( +
+

Personal Area

+
+ ); +} diff --git a/app/profile/layout.tsx b/app/profile/layout.tsx new file mode 100644 index 000000000..869301b94 --- /dev/null +++ b/app/profile/layout.tsx @@ -0,0 +1,66 @@ +'use client'; + +import { Avatar } from '@nextui-org/react'; +import LogoutButton from 'components/button/logout'; +import { Customer } from 'lib/woocomerce/models/customer'; +import { Shipping } from 'lib/woocomerce/models/shipping'; +import Link from 'next/link'; +import { useEffect, useState } from 'react'; + +export default function ProfileLayout({ children }: { children: React.ReactNode }) { + const [customer, setCustomer] = useState(undefined); + const [shippingAddress, setShippingAddress] = useState(undefined); + + useEffect(() => { + const fetchCustomer = async () => { + const data = (await ( + await fetch('/api/customer', { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }) + ).json()) as Customer; + setCustomer(data); + }; + + fetchCustomer(); + }, []); + + return ( +
+
+

Profile

+ {customer && ( +
+ +
+ {customer.first_name} + {customer.last_name} +
+
+ + + +
+
+ + + +
+
+ +
+
+ )} +
+
+ {children} +
+
+ ); +} diff --git a/app/profile/orders/page.tsx b/app/profile/orders/page.tsx index 4c5f28c6d..ef11d4b2d 100644 --- a/app/profile/orders/page.tsx +++ b/app/profile/orders/page.tsx @@ -10,7 +10,7 @@ export default async function OrdersPage() { const orders = await woocommerce.get('orders'); return ( -
+

Orders

{orders.map((order) => ( diff --git a/app/profile/page.tsx b/app/profile/page.tsx index e17ca8e23..e025c0185 100644 --- a/app/profile/page.tsx +++ b/app/profile/page.tsx @@ -1,106 +1,7 @@ -'use client'; - -import LogoutButton from 'components/button/logout'; -import ShippingForm from 'components/shipping/form'; -import { Customer } from 'lib/woocomerce/models/customer'; -import { Shipping } from 'lib/woocomerce/models/shipping'; -import Link from 'next/link'; -import { useEffect, useState } from 'react'; - -export default function ProfilePage() { - const [customer, setCustomer] = useState(undefined); - const [shippingAddress, setShippingAddress] = useState(undefined); - - useEffect(() => { - const fetchCustomer = async () => { - const data = (await ( - await fetch('/api/customer', { - method: 'GET', - headers: { - 'Content-Type': 'application/json' - } - }) - ).json()) as Customer; - setCustomer(data); - }; - - fetchCustomer(); - }, []); - +export default async function ProfilePage() { return ( -
-

Profile

-
-

Info

- {customer && ( -
- avatar -
-
- - -
-
- - -
-
- - - - - -
- - - -
-
- -
-
-
-
- )} -
+
+

Personal Area

); } diff --git a/components/button/logout.tsx b/components/button/logout.tsx index 99c6d0d6b..33beafb12 100644 --- a/components/button/logout.tsx +++ b/components/button/logout.tsx @@ -7,7 +7,7 @@ export default function LogoutButton() { return (