From 23f9f55fabf3f2542544be0f9048189a58d06ecd Mon Sep 17 00:00:00 2001 From: paolosantarsiero Date: Fri, 3 Jan 2025 12:35:38 +0100 Subject: [PATCH] refactor: change layout profile and related products --- app/api/customer/route.ts | 10 +++--- app/product/[name]/page.tsx | 30 +++++++++++++++++ app/profile/area/page.tsx | 7 ---- app/profile/layout.tsx | 11 ++++--- app/profile/page.tsx | 57 ++++++++++++++++++++++++++++++++- components/button/logout.tsx | 4 ++- components/cart/add-to-cart.tsx | 2 ++ components/cart/modal.tsx | 7 +++- lib/auth/config.ts | 6 ++-- lib/woocomerce/models/client.ts | 6 +--- 10 files changed, 114 insertions(+), 26 deletions(-) delete mode 100644 app/profile/area/page.tsx diff --git a/app/api/customer/route.ts b/app/api/customer/route.ts index e8c6349f9..199ce5bd5 100644 --- a/app/api/customer/route.ts +++ b/app/api/customer/route.ts @@ -9,8 +9,8 @@ export async function GET(req: NextRequest) { if (!session?.user?.store_id) { return NextResponse.json({ error: 'User not logged' }, { status: 401 }); } - const cart = await woocommerce.get('customers', { id: session?.user.store_id }); - return NextResponse.json(cart, { status: 200 }); + const customer = await woocommerce.get('customers', { id: session?.user.store_id }); + return NextResponse.json(customer, { status: 200 }); } catch (error) { return NextResponse.json({ error: JSON.stringify(error) }, { status: 500 }); } @@ -19,9 +19,9 @@ export async function GET(req: NextRequest) { export async function POST(req: NextRequest) { try { const data = await req.json(); - const cart = await woocommerce.post('customers', data); - return NextResponse.json(cart, { status: 200 }); + const customer = await woocommerce.post('customers', data); + return NextResponse.json(customer, { status: 200 }); } catch (error) { - return NextResponse.json({ error: 'Failed to add item to cart' }, { status: 500 }); + return NextResponse.json({ error: 'Failed to add item to customer' }, { status: 500 }); } } diff --git a/app/product/[name]/page.tsx b/app/product/[name]/page.tsx index 07e078f52..2d536690c 100644 --- a/app/product/[name]/page.tsx +++ b/app/product/[name]/page.tsx @@ -11,6 +11,7 @@ import { HIDDEN_PRODUCT_TAG } from 'lib/constants'; import { Image } from 'lib/woocomerce/models/base'; import { Product, ProductVariations } from 'lib/woocomerce/models/product'; import { woocommerce } from 'lib/woocomerce/woocommerce'; +import Link from 'next/link'; import { Suspense } from 'react'; export async function generateMetadata(props: { @@ -51,6 +52,10 @@ export default async function ProductPage(props: { params: Promise<{ name: strin if (!product) return notFound(); + const relatedProducts = await Promise.all( + product.related_ids?.map(async (id) => woocommerce.get(`products/${id}`)) || [] + ); + const productJsonLd = { '@context': 'https://schema.org', '@type': 'Product', @@ -114,6 +119,31 @@ export default async function ProductPage(props: { params: Promise<{ name: strin +
+

Related Products

+
+ {relatedProducts.map((relatedProduct) => { + return ( +
+ {relatedProduct.name} +
+ +

{relatedProduct.name}

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

Personal Area

-
- ); -} diff --git a/app/profile/layout.tsx b/app/profile/layout.tsx index 869301b94..e1ce1eceb 100644 --- a/app/profile/layout.tsx +++ b/app/profile/layout.tsx @@ -1,5 +1,6 @@ 'use client'; +import { CubeIcon, UserCircleIcon } from '@heroicons/react/24/outline'; import { Avatar } from '@nextui-org/react'; import LogoutButton from 'components/button/logout'; import { Customer } from 'lib/woocomerce/models/customer'; @@ -39,15 +40,17 @@ export default function ProfileLayout({ children }: { children: React.ReactNode {customer.last_name}
- -
- - diff --git a/app/profile/page.tsx b/app/profile/page.tsx index e025c0185..fcec61f0d 100644 --- a/app/profile/page.tsx +++ b/app/profile/page.tsx @@ -1,7 +1,62 @@ -export default async function ProfilePage() { +'use server'; + +import { authOptions } from 'lib/auth/config'; +import { woocommerce } from 'lib/woocomerce/woocommerce'; +import { getServerSession } from 'next-auth'; + +export default async function PersonalArea() { + const session = await getServerSession(authOptions); + if (!session?.user?.store_id) { + return { status: 401, body: { error: 'User not logged' } }; + } + + const customer = await woocommerce.get('customers', { id: session?.user.store_id }); + return (

Personal Area

+
+ + + + + + +
); } diff --git a/components/button/logout.tsx b/components/button/logout.tsx index 33beafb12..d9d56f8c3 100644 --- a/components/button/logout.tsx +++ b/components/button/logout.tsx @@ -1,4 +1,5 @@ 'use client'; +import { ArrowRightEndOnRectangleIcon } from '@heroicons/react/24/outline'; import { signOut } from 'next-auth/react'; import { useRouter } from 'next/navigation'; @@ -7,11 +8,12 @@ export default function LogoutButton() { return ( ); diff --git a/components/cart/add-to-cart.tsx b/components/cart/add-to-cart.tsx index de82686fc..456f5536b 100644 --- a/components/cart/add-to-cart.tsx +++ b/components/cart/add-to-cart.tsx @@ -4,6 +4,7 @@ import { PlusIcon } from '@heroicons/react/24/outline'; import clsx from 'clsx'; import { useProduct } from 'components/product/product-context'; import { Product, ProductVariations } from 'lib/woocomerce/models/product'; +import { toast } from 'sonner'; import { useCart } from './cart-context'; function SubmitButton({ disabled = false }: { disabled: boolean }) { @@ -50,6 +51,7 @@ export function AddToCart({ }) ).json(); setNewCart(cart); + toast('Item added to cart'); } catch (error) { console.error(error); } diff --git a/components/cart/modal.tsx b/components/cart/modal.tsx index 8bd4f4276..1e41ea8d0 100644 --- a/components/cart/modal.tsx +++ b/components/cart/modal.tsx @@ -89,7 +89,12 @@ export default function CartModal() { key={i} className="flex w-full flex-col border-b border-neutral-300 dark:border-neutral-700" > - + ); })} diff --git a/lib/auth/config.ts b/lib/auth/config.ts index 6224c8b88..ecf5117e9 100644 --- a/lib/auth/config.ts +++ b/lib/auth/config.ts @@ -1,5 +1,5 @@ import { storeApi } from 'lib/woocomerce/storeApi'; -import { woocommerce } from 'lib/woocomerce/woocommerce'; +import { wordpress } from 'lib/wordpress/wordpress'; import { NextAuthOptions, Session, User } from 'next-auth'; import { JWT } from 'next-auth/jwt'; import CredentialsProvider from 'next-auth/providers/credentials'; @@ -20,11 +20,13 @@ export const authOptions = { if (!credentials?.username || !credentials?.password) { return null; } - const user = await woocommerce.login(credentials.username, credentials.password); + const user = await wordpress.login(credentials.username, credentials.password); // If no error and we have user data, return it if (user) { return user; } + storeApi._seCartToken(''); + storeApi._setAuthorizationToken(''); // Return null if user data could not be retrieved return null; } diff --git a/lib/woocomerce/models/client.ts b/lib/woocomerce/models/client.ts index 1e7af76b7..d9403d37e 100644 --- a/lib/woocomerce/models/client.ts +++ b/lib/woocomerce/models/client.ts @@ -30,7 +30,7 @@ export type WooRestApiParams = CouponsParams & type WooCommerceResponse< T extends WooRestApiEndpoint, P extends Partial = {} -> = P['id'] extends number | string // Verifica se `id` รจ definito e di tipo string +> = P['id'] extends number | string ? T extends 'products' ? Product : T extends 'customers' @@ -115,10 +115,6 @@ export default class WooCommerceRestApi { this._opt.classVersion = '0.0.2'; } - login(username: string, password: string): Promise { - return this._request('POST', 'token', { username, password }, {}, 'jwt-auth/v1'); - } - /** * Parse params to object. *