From 29af45435b156bccad2bffa32481791c7da80492 Mon Sep 17 00:00:00 2001 From: nemesislw Date: Fri, 12 Jul 2024 00:15:10 +0530 Subject: [PATCH] build: form state, data fetching, server actions integrated --- app/(auth)/auth/callback/route.ts | 8 +-- app/page.tsx | 5 +- components/generate-cta-button.tsx | 44 ++++++++-------- components/pickupline-input-form.tsx | 41 ++++++++++----- lib/ai/actions.ts | 79 ++++++++++++++++++++++------ lib/supabase/actions.ts | 3 -- 6 files changed, 123 insertions(+), 57 deletions(-) diff --git a/app/(auth)/auth/callback/route.ts b/app/(auth)/auth/callback/route.ts index 554c4d0..e33a173 100644 --- a/app/(auth)/auth/callback/route.ts +++ b/app/(auth)/auth/callback/route.ts @@ -1,6 +1,7 @@ import { cookies } from "next/headers"; import { NextResponse } from "next/server"; import { createServerClient, type CookieOptions } from "@supabase/ssr"; +import { revalidatePath } from "next/cache"; export async function GET(request: Request) { const { searchParams, origin } = new URL(request.url); @@ -25,14 +26,15 @@ export async function GET(request: Request) { cookieStore.delete({ name, ...options }); }, }, - } + }, ); const { error } = await supabase.auth.exchangeCodeForSession(code); if (!error) { + revalidatePath("/", "layout"); return NextResponse.redirect(`${origin}/generate`); } } - // return the user to an error page - return NextResponse.redirect(`${origin}/error`); + // return the user to home page + return NextResponse.redirect(`${origin}`); } diff --git a/app/page.tsx b/app/page.tsx index 1e9e432..98f2951 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,6 +1,7 @@ import Image from "next/image"; import GenerateButton from "@/components/generate-cta-button"; +import Link from "next/link"; export default function Home() { return ( @@ -25,7 +26,9 @@ export default function Home() { Generator - + + + diff --git a/components/generate-cta-button.tsx b/components/generate-cta-button.tsx index c2aa481..9f6399c 100644 --- a/components/generate-cta-button.tsx +++ b/components/generate-cta-button.tsx @@ -1,4 +1,3 @@ -import Link from "next/link"; import { Button } from "./ui/button"; import Image from "next/image"; import { cn } from "@/lib/utils"; @@ -6,39 +5,42 @@ import { cn } from "@/lib/utils"; interface GenerateButtonProps { className?: string; text?: string; + isServerAction?: boolean; + pending?: boolean; } function GenerateButton({ className, text = "Generate one for me", + isServerAction = false, + pending = false, }: GenerateButtonProps) { return ( ); } diff --git a/components/pickupline-input-form.tsx b/components/pickupline-input-form.tsx index a69afb9..febc508 100644 --- a/components/pickupline-input-form.tsx +++ b/components/pickupline-input-form.tsx @@ -2,48 +2,65 @@ import { Textarea } from "@/components/ui/textarea"; import { Input } from "@/components/ui/input"; -import React, { useState } from "react"; +import React from "react"; import GenerateButton from "./generate-cta-button"; +import { Generate } from "@/lib/ai/actions"; +import { useFormState, useFormStatus } from "react-dom"; + +const initialState: { + message: string; + Data?: any; +} = { message: "" }; function PickupLineInputForm() { - const [crush, setCrush] = useState(""); - const [style, setStyle] = useState(""); + const { pending } = useFormStatus(); + const [state, formAction] = useFormState(Generate, initialState); + return (

Pickup Line Generator

- -
+