From 55c7da77c0bff6ae7264544d626d807eabb9ee90 Mon Sep 17 00:00:00 2001 From: jonaeunnn Date: Wed, 21 Aug 2024 16:42:35 +0900 Subject: [PATCH] =?UTF-8?q?feat:=EC=95=88=EA=B1=B4=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20url=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Vote/VoteList/VoteList.tsx | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/components/Vote/VoteList/VoteList.tsx b/src/components/Vote/VoteList/VoteList.tsx index 496f24a..9f326d6 100644 --- a/src/components/Vote/VoteList/VoteList.tsx +++ b/src/components/Vote/VoteList/VoteList.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from 'react'; import * as S from './Styles'; import VoteContainer from '../VoteContainer/VoteContainer'; -import { useNavigate } from 'react-router-dom'; +import { useNavigate, useLocation } from 'react-router-dom'; import { useRecoilState, useRecoilValue } from 'recoil'; import { agendasList } from 'recoil/states/vote'; import axios from 'axios'; @@ -13,6 +13,7 @@ const token = getCookie('access-token') || process.env.REACT_APP_REFRESH_TOKEN; const VoteList: React.FC = () => { const navigate = useNavigate(); + const location = useLocation(); const groupId = useRecoilValue(shareGroupId); const [agendas, setAgendas] = useRecoilState(agendasList); const [currentPage, setCurrentPage] = useState(0); @@ -39,9 +40,19 @@ const VoteList: React.FC = () => { }, }); const { agendaDetailInfoList, totalPages } = response.data.data; - console.log('Fetched Agendas:', agendaDetailInfoList); - setAgendas((prevAgendas) => [...prevAgendas, ...agendaDetailInfoList]); // 기존 데이터에 추가 + + if (agendaDetailInfoList.length === 0) { + // 안건이 없는 경우 /vote 페이지로 이동 + navigate('/vote', { replace: true }); + return; + } + + // 안건이 있는 경우 상태 업데이트 및 /vote/list로 이동 + setAgendas((prevAgendas) => [...prevAgendas, ...agendaDetailInfoList]); setTotalPages(totalPages); + if (location.pathname !== '/vote/list') { + navigate('/vote/list', { replace: true }); + } } catch (error: any) { setError(error.message || 'Error fetching data'); } finally { @@ -50,8 +61,11 @@ const VoteList: React.FC = () => { }; useEffect(() => { - fetchAgendas(currentPage); - }, [groupId, currentPage]); + // 새로운 그룹을 선택할 때마다 기존 안건 목록 초기화 및 첫 페이지 호출 + setAgendas([]); + setCurrentPage(0); + fetchAgendas(0); + }, [groupId]); const handleScroll = () => { if (