Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding final state of the app after Nightclass #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
experimental: {
reactCompiler: true,
},
};

export default nextConfig;
77 changes: 41 additions & 36 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
"use client";

import { useCart } from "@/queries/useCart";
import { type FormEvent, useState } from "react";
import * as Form from "@/components/form/Form";
import { RelatedProductsClient } from "@/components/product/RelatedProductClient";
import { RelatedProducts } from "@/components/product/RelatedProducts";
import { getRelated_PROMISE } from "@/components/product/getRelatedProduct";
import { addToCart, getCart } from "@/repository/cart";
import { timeout } from "@/utils/timeout";
import { revalidatePath } from "next/cache";
import { notFound } from "next/navigation";
import { Suspense } from "react";

export default function Page() {
const [error, setError] = useState(false);
const [isLoading, setIsLoading] = useState(false);

const { data, refreshCart } = useCart();
const cart = getCart("1");
if (!cart) {
notFound();
}

async function handleSubmit(e: FormEvent) {
setIsLoading(true);
e.preventDefault();
const res = await fetch("/api/cart/1", { method: "POST" });
if (!res.ok) {
setError(true);
} else {
setError(false);
await refreshCart();
async function handleAction() {
"use server";
await timeout(1000);
try {
// Update cart
addToCart("1");
revalidatePath("/");
return false;
} catch (e) {
return true;
}
setIsLoading(false);
}

const promise = getRelated_PROMISE();

return (
<>
{/* PAGE */}
Expand All @@ -37,31 +44,29 @@ export default function Page() {
alt="a green sweater with long sleeves"
/>
</section>

<Suspense fallback={<div className="loading" />}>
<RelatedProducts />
</Suspense>

<Suspense fallback={<div className="loading" />}>
<RelatedProductsClient relatedProductsPromise={promise} />
</Suspense>
</section>
<form
onSubmit={handleSubmit}
<Form.Root
action={handleAction}
className="flex-0 p-4 rounded bg-gray-200 text-slate-950 dark:bg-slate-800 dark:text-white text-2xl"
>
<button className="btn btn-primary" type="submit">
Add to cart
</button>
<h2 className="text-4xl font-bold">your cart</h2>
{data ? (
<div className="h-[2rem]">{data.items} items</div>
) : (
<div className="h-[2rem]" />
)}
{!data || isLoading ? (
<div className="loading" />
) : (
<div className="h-[2rem]" />
)}
{error ? (
<div className="text-red-400">Out of stock</div>
) : (
<div className="h-[2rem]" />
)}
</form>

<div className="h-[2rem]">{cart.items} items</div>

<Form.Pending />
<Form.Error />
</Form.Root>
</article>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/product/RelatedProductClient.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
import type { getRelated_PROMISE } from "@/components/product/getRelatedProduct";
import { use } from "react";

Expand Down