From 5b37e7130a8819c204bb1fbadd5e1c8077970fa9 Mon Sep 17 00:00:00 2001 From: soheeyeo Date: Wed, 14 Feb 2024 20:00:10 +0900 Subject: [PATCH] :recycle: Refactor data fetch function --- app/globals.css | 6 +++ app/loading.js | 7 ++- app/test/result/[type]/Content.js | 59 +++++++++++++++++++++++ app/test/result/[type]/page.js | 77 ++++++++----------------------- 4 files changed, 91 insertions(+), 58 deletions(-) create mode 100644 app/test/result/[type]/Content.js diff --git a/app/globals.css b/app/globals.css index 695cec9..0308fd1 100644 --- a/app/globals.css +++ b/app/globals.css @@ -341,6 +341,12 @@ header { color: var(--main-color); } +.loading_txt_result { + padding-top: 30px; + font-size: 18px; + color: var(--main-color); +} + @keyframes bounce { 0%, 100% { diff --git a/app/loading.js b/app/loading.js index 335cfa6..c917cb5 100644 --- a/app/loading.js +++ b/app/loading.js @@ -1,4 +1,4 @@ -export default function Loading({ style }) { +export default function Loading({ style, result }) { return (
@@ -7,6 +7,11 @@ export default function Loading({ style }) {

Loading...

+ {result ? ( +

초콜릿을 찾고 있어요🍫

+ ) : ( + "" + )}
); } diff --git a/app/test/result/[type]/Content.js b/app/test/result/[type]/Content.js new file mode 100644 index 0000000..021fe55 --- /dev/null +++ b/app/test/result/[type]/Content.js @@ -0,0 +1,59 @@ +"use client"; +import { useEffect, useState } from "react"; +import Link from "next/link"; +import Loading from "@/app/loading"; + +export default function Content({ styles, item }) { + const data = item; + + const [isLoading, setIsLoading] = useState(false); + + useEffect(() => { + setTimeout(() => { + setIsLoading(true); + }, 3000); + }); + + return ( + <> + {isLoading ? ( +
+

+ 이 초콜릿을 추천드려요. +

+

+ 제품을 클릭하면 상세 페이지로 이동합니다. +

+
+ +
+
+

{data.country}

+
+
+

{data.type}

+
+
+
+ +
+
+

+ {data.brand} +

+

+ {data.name} +

+
+ +
+
+ ) : ( + + )} + + ); +} diff --git a/app/test/result/[type]/page.js b/app/test/result/[type]/page.js index 527e005..3749b15 100644 --- a/app/test/result/[type]/page.js +++ b/app/test/result/[type]/page.js @@ -1,16 +1,26 @@ -"use client"; import styles from "../../test.module.css"; import testData from "@/util/testData"; -import { useEffect, useState } from "react"; -import Link from "next/link"; -import Loading from "@/app/loading"; +import Content from "./Content"; -export default function Result({ params: { type } }) { - const [data, setData] = useState([]); - const [isLoading, setIsLoading] = useState(false); +export const metadata = { + description: "초콜릿 추천 테스트 결과", + openGraph: { + description: "초콜릿 추천 테스트 결과", + }, +}; +async function getData(resultItem) { + const res = await fetch(`${process.env.URL}/api/result`, { + method: "POST", + body: JSON.stringify(resultItem.name), + }); + const result = await res.json(); + return result; +} + +export default async function Result({ params: { type } }) { const testResult = testData.result; - const result = testResult.find((data) => { + const resultItem = testResult.find((data) => { if (Array.isArray(data.type)) { return data.type.includes(type); } else { @@ -18,58 +28,11 @@ export default function Result({ params: { type } }) { } }); - useEffect(() => { - fetch("/api/result", { - method: "POST", - body: JSON.stringify(result.name), - }) - .then((res) => res.json()) - .then((result) => { - setData(result); - setIsLoading(true); - }); - }, []); + const item = await getData(resultItem); return (
- {isLoading ? ( -
-

- 이 초콜릿을 추천드려요. -

-

- 제품을 클릭하면 상세 페이지로 이동합니다. -

-
- -
-
-

{data.country}

-
-
-

{data.type}

-
-
-
- -
-
-

- {data.brand} -

-

- {data.name} -

-
- -
-
- ) : ( - - )} +
); }