From f0beabe3545658641ad77be059be19189ece9275 Mon Sep 17 00:00:00 2001 From: Junhyeong Park Date: Fri, 17 May 2024 02:59:01 +0900 Subject: [PATCH] fix: auto routing logic update --- src/App.tsx | 4 +-- src/Pages/MainPage.tsx | 22 +----------- src/Pages/ResultPage.tsx | 73 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 8a7db12..f96d5c6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,14 +1,14 @@ import React from "react"; import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; -import ResultPage from "./Pages/ResultPage"; import MainPage from "./Pages/MainPage"; +import ResultPage from "./Pages/ResultPage"; const App: React.FC = () => { return ( } /> - } /> + } /> ); diff --git a/src/Pages/MainPage.tsx b/src/Pages/MainPage.tsx index 0436513..6322094 100644 --- a/src/Pages/MainPage.tsx +++ b/src/Pages/MainPage.tsx @@ -40,27 +40,7 @@ const MainPage: React.FC = () => { const { task_id } = response.data; console.log("File uploaded successfully, task ID:", task_id); - - const checkTaskStatus = async () => { - try { - const statusResponse = await axios.get( - `https://ajtksbackend.p-e.kr/task/${task_id}` - ); - const { status, searchResult } = statusResponse.data; - - if (status === "completed") { - navigate("/result", { state: { searchResult } }); - setIsAnalyzing(false); - } else { - setTimeout(checkTaskStatus, 3000); - } - } catch (error) { - console.error("Error checking task status:", error); - setIsAnalyzing(false); - } - }; - - checkTaskStatus(); + navigate(`/result/${task_id}`); } catch (error) { console.error("Error uploading file:", error); setIsAnalyzing(false); diff --git a/src/Pages/ResultPage.tsx b/src/Pages/ResultPage.tsx index e14329c..c83793d 100644 --- a/src/Pages/ResultPage.tsx +++ b/src/Pages/ResultPage.tsx @@ -1,6 +1,65 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; +import { useParams, useNavigate } from "react-router-dom"; +import axios from "axios"; const ResultPage = () => { + const { task_id } = useParams<{ task_id: string }>(); + const [searchResult, setSearchResult] = useState(null); + const [error, setError] = useState(null); + const navigate = useNavigate(); + + useEffect(() => { + const fetchResult = async () => { + try { + const response = await axios.get( + `https://ajtksbackend.p-e.kr/task/${task_id}` + ); + const { status, searchResult } = response.data; + + if (status === "completed") { + setSearchResult(searchResult); + } else { + setTimeout(fetchResult, 3000); + } + } catch (error) { + console.error("Error fetching task status:", error); + setError("결과를 가져오는 중 오류가 발생했습니다."); + } + }; + + fetchResult(); + }, [task_id]); + + if (error) { + return ( +
+
+

{error}

+ +
+
+ ); + } + + if (!searchResult) { + return ( +
+ +
+ 분석 중입니다... +
+
+ ); + } + return (
{ 추천 음악 목록
- {/* Cards Repeated with Different Contents */} - {Array.from({ length: 5 }).map((_, index) => ( + {searchResult.map((result: any, index: number) => (
{
{`Music
@@ -33,7 +91,7 @@ const ResultPage = () => {
Featured music
@@ -42,7 +100,10 @@ const ResultPage = () => {
음악 설명 by MU-LLaMA
-
+
navigate("/")} + > 다시 인식하기