From 2a98b95f10638a99b63d95409ef488c5ba5d4c42 Mon Sep 17 00:00:00 2001 From: EunsolnCho Date: Wed, 21 Aug 2024 14:43:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[FEAT]=20=EB=A7=88=EC=9D=B4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=BB=A4=EB=AE=A4=EB=8B=88=ED=8B=B0=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EB=B0=8F=20=EC=82=AD=EC=A0=9C=20=EC=97=B0?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/instance.js | 2 + src/components/MyCommunityItem.jsx | 23 +++++++- src/pages/MyCommunity.jsx | 92 ++++++++++++++++++++++++++---- 3 files changed, 103 insertions(+), 14 deletions(-) diff --git a/src/api/instance.js b/src/api/instance.js index 4480502..7adce79 100644 --- a/src/api/instance.js +++ b/src/api/instance.js @@ -99,8 +99,10 @@ instance.interceptors.response.use( console.log('Decoded Token:', checkAcces); // 디코딩된 토큰 로그 확인 removeSessionStorage('accessToken'); removeSessionStorage('role'); + removeSessionStorage('memberId'); setSessionStorage('accessToken', accessToken); setSessionStorage('role', checkAcces.role); + setSessionStorage('memberId', checkAcces.memberId); } if (refreshToken) { const checkRefresh = jwtDecode(refreshToken); diff --git a/src/components/MyCommunityItem.jsx b/src/components/MyCommunityItem.jsx index b4e0b5b..7126b9e 100644 --- a/src/components/MyCommunityItem.jsx +++ b/src/components/MyCommunityItem.jsx @@ -1,17 +1,36 @@ import { useNavigate } from "react-router-dom"; import { format } from 'date-fns'; +import instance from "../api/instance"; + +const categoryMap = { + 'FREE': '자유게시판', + 'PORTFOLIO': '포트폴리오', + 'TEAM': '팀원모집', +} // 지원현황 페이지 - 아이템 컴포넌트 const MyCommunityItem = ({ id, category, title, writer, date }) => { const nav = useNavigate(); + // 삭제 함수 추가 + const handleDelete = async () => { + try { + await instance.delete(`/api/post/${id}/delete`); + alert('삭제되었습니다.'); + window.location.reload(); + } catch (error) { + console.error('삭제 실패:', error); + alert('삭제에 실패했습니다.'); + } + }; + return (
-
{category}
+
{categoryMap[category]}
{title}
{writer}
{`${format(new Date(date), 'yyyy.MM.dd')}`}
-
nav('/user/mypage/editinfo')} className="w-1/12 cursor-pointer text-[15px] text-gray-deleteBtn text-center">삭제
+
삭제
); }; diff --git a/src/pages/MyCommunity.jsx b/src/pages/MyCommunity.jsx index 0292eef..1f63668 100644 --- a/src/pages/MyCommunity.jsx +++ b/src/pages/MyCommunity.jsx @@ -5,6 +5,7 @@ import MenubarMyPage from "../components/MenubarMyPage"; import MyCommunityItem from "../components/MyCommunityItem"; import { ChevronDownIcon } from '@heroicons/react/24/outline'; import Toggle from "../components/Toggle"; +import instance from "../api/instance"; const communityData = [ { @@ -24,7 +25,66 @@ const communityData = [ ]; const MyCommunity = () => { - const [memberData, jobPostData, applicationData, applicaionDetailData, infoData] = useContext(GrowthStateContext); + const [selectedMenu, setSelectedMenu] = useState("작성한 글"); + const [data, setData] = useState([]); + const memberId = sessionStorage.getItem('memberId'); + + // 선택된 메뉴에 따라 데이터를 가져오는 함수 + const fetchData = async (menu) => { + let url = ""; + let responseData = []; + + try { + switch (menu) { + case "작성한 글": { + const response = await instance.get('/api/post'); + if (response.status === 200) { + console.log(response.data.data); + responseData = response.data.data.posts; + } else { + console.log('사용자 작성 글 목록 조회 실패:', response.data.message); + } + break; + } + + case "작성한 댓글": { + const response = await instance.get('/api/post'); + if (response.status === 200) { + console.log(response.data.data); + responseData = response.data.data.commentList; + } else { + console.log('사용자 작성 댓글 목록 조회 실패:', response.data.message); + } + break; + } + + case "저장한 글": { + url = `/api/post/saves/${memberId}/list`; + const response = await instance.get(url); + if (response.status === 200) { + console.log(response.data.data); + responseData = response.data.data; + } else { + console.log('사용자 저장 글 목록 조회 실패:', response.data.message); + } + break; + } + + default: + console.error('알 수 없는 메뉴:', menu); + } + + setData(responseData); + + } catch (error) { + console.error('API 요청 에러:', error); + } + }; + + // 토글에서 메뉴 선택이 변경될 때마다 데이터를 요청함 + useEffect(() => { + fetchData(selectedMenu); + }, [selectedMenu]); // 아이콘 스타일 const iconClass = 'size-6 stroke-1'; @@ -34,7 +94,7 @@ const MyCommunity = () => {
- +
@@ -42,7 +102,12 @@ const MyCommunity = () => {
- +
@@ -53,17 +118,20 @@ const MyCommunity = () => {
- {communityData.map((data)=>{// 아이템 순회하면서 렌더링 - return ( + {data && data.length > 0 ? ( + data.map((item, index) => ( - ); - })} + )) + ) : ( +
데이터가 없습니다.
+ )}
From 948e52c31ba23df765180f661d3d98b3b728c0da Mon Sep 17 00:00:00 2001 From: EunsolnCho Date: Wed, 21 Aug 2024 14:44:57 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[UPDATE]=20=EC=9E=84=EC=8B=9C=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/MyCommunity.jsx | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/pages/MyCommunity.jsx b/src/pages/MyCommunity.jsx index 1f63668..a6c1061 100644 --- a/src/pages/MyCommunity.jsx +++ b/src/pages/MyCommunity.jsx @@ -7,23 +7,6 @@ import { ChevronDownIcon } from '@heroicons/react/24/outline'; import Toggle from "../components/Toggle"; import instance from "../api/instance"; -const communityData = [ - { - id: 0, - category: "포트폴리오", - title: "프론트엔드 개발자 구하는 방법 물어봅니다.", - writer: "김수윤", - date: new Date("2024-7-24"), - }, - { - id: 1, - category: "자유", - title: "백엔드 스터디 같이 하실 분 찾아요", - writer: "김수윤", - date: new Date("2024-7-24"), - }, -]; - const MyCommunity = () => { const [selectedMenu, setSelectedMenu] = useState("작성한 글"); const [data, setData] = useState([]);