From 7cd677912c6d890c9cae49c731b37bfb62f596a2 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:56:15 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=9B=9E=E7=AD=94=E9=80=81?= =?UTF-8?q?=E4=BF=A1=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=A7nextjs=E3=81=AEapi?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=81=86=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/questions/route.ts | 35 +++++++++++++++++++++++++++++++++ src/app/forms/[formId]/page.tsx | 29 ++++++++++++++++----------- 2 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/app/api/questions/route.ts diff --git a/src/app/api/questions/route.ts b/src/app/api/questions/route.ts new file mode 100644 index 00000000..1e4d2bd1 --- /dev/null +++ b/src/app/api/questions/route.ts @@ -0,0 +1,35 @@ +'use server'; + +import { BACKEND_SERVER_URL } from '@/env'; +import { getCachedToken } from '@/features/user/api/mcToken'; +import { NextRequest, NextResponse } from 'next/server'; +import { redirectByResponse } from '../util/responseOrErrorResponse'; + +export async function GET(req: NextRequest) { + const token = await getCachedToken(); + if (!token) { + return NextResponse.redirect('/'); + } + + const searchParms = req.nextUrl.searchParams; + const formId = searchParms.get('formId'); + if (!formId) { + return NextResponse.redirect('/badrequest'); + } + + const response = await fetch( + `${BACKEND_SERVER_URL}/forms/${formId}/questions`, + { + method: 'GET', + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}`, + }, + cache: 'no-cache', + } + ); + + redirectByResponse(response); + + return NextResponse.json(await response.json()); +} diff --git a/src/app/forms/[formId]/page.tsx b/src/app/forms/[formId]/page.tsx index 7b5a60a6..af285808 100644 --- a/src/app/forms/[formId]/page.tsx +++ b/src/app/forms/[formId]/page.tsx @@ -1,19 +1,24 @@ -import { isRight } from 'fp-ts/lib/Either'; -import { redirectOrDoNothing } from '@/app/error/RedirectByErrorResponse'; -import { getFormQuestions } from '@/features/form/api/form'; +'use client'; + import AnswerForm from '@/features/form/components/AnswerForm'; -import { getCachedToken } from '@/features/user/api/mcToken'; +import { FormQuestion } from '@/features/form/types/formSchema'; +import useSWR from 'swr'; +import { redirect } from 'next/navigation'; -const Home = async ({ params }: { params: { formId: number } }) => { - const token = (await getCachedToken()) ?? ''; - const questions = await getFormQuestions(params.formId, token); +const Home = ({ params }: { params: { formId: number } }) => { + const fetcher = (url: string) => fetch(url).then((res) => res.json()); + const { data: questions, isLoading } = useSWR( + `http://localhost:3000/api/questions?formId=${params.formId}`, + fetcher + ); - if (isRight(questions)) { - return ; - } else { - redirectOrDoNothing(questions); - return <>; + if (!isLoading && !questions) { + redirect('/'); + } else if (!questions) { + return null; } + + return ; }; export default Home;