diff --git a/src/api/axios.ts b/src/api/axios.ts index 8d93c97..8e6c154 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -1,4 +1,5 @@ import axios from "axios"; +import useAuthStore from "@/store/useAuthStore"; const instance = axios.create({ baseURL: import.meta.env.VITE_API_BASE_URL, @@ -8,4 +9,33 @@ const instance = axios.create({ } }); +// 인증 절차가 필요한 API는 authInstance로 HTTP요청 +const authInstance = axios.create({ + baseURL: import.meta.env.VITE_API_BASE_URL, + timeout: 10000, + headers: { + "Content-Type": "application/json" + } +}); + +// authInstance 헤더에 accessToken 추가하는 로직 +authInstance.interceptors.request.use( + (config) => { + if (!config.headers) { + config.headers = {}; + } + + const { accessToken } = useAuthStore.getState(); + + if (accessToken) { + config.headers.Authorization = `Bearer ${accessToken}`; + } + return config; + }, + (error) => { + return Promise.reject(error); + } +); + export default instance; +export { authInstance }; diff --git a/src/components/Profile.tsx b/src/components/Profile.tsx index 2e02757..0dd39c6 100644 --- a/src/components/Profile.tsx +++ b/src/components/Profile.tsx @@ -1,31 +1,68 @@ -const Profile = () => { - return ( -
- Delete +import { SetStateAction } from "react"; +import { useNavigate } from "react-router-dom"; +import { authInstance } from "@/api/axios"; +import { VirtualFriend } from "@/types/virtualFreind"; + +interface ProfileProps { + info: VirtualFriend; + deleteIndex: number; + setVirtualFriendList: React.Dispatch>; +} +const Profile = ({ info, deleteIndex, setVirtualFriendList }: ProfileProps) => { + const navigate = useNavigate(); + + const handleDelete = async () => { + const res = await authInstance.delete( + `/api/virtual-friend/${info.virtualFriendId}` + ); + if (res.status === 200) { + setVirtualFriendList((prevList) => + prevList.filter((_, index) => index !== deleteIndex) + ); + } + }; + const handleNavigate = () => { + navigate("/chat", { + state: { + mode: "virtualFriend", + mbti: info.mbti, + id: info.virtualFriendId + } + }); + }; + + return ( +
+ Profile -
-

- 김엠비 - ENTP +
+

+ {info.virtualFriendName} + {info.mbti}

-

- 20대 · 여자 · 직장동료 · 여행 · 사회생활 +

+ {info.virtualFriendAge} · {info.virtualFriendSex} ·{" "} + {info.virtualFriendRelationship}

- -

diff --git a/src/components/ProfileContainer.tsx b/src/components/ProfileContainer.tsx new file mode 100644 index 0000000..dd98c7d --- /dev/null +++ b/src/components/ProfileContainer.tsx @@ -0,0 +1,27 @@ +import Profile from "@/components/Profile"; +import { VirtualFriend } from "@/types/virtualFreind"; +import { SetStateAction } from "react"; + +interface ProfileContainerProps { + list: VirtualFriend[]; + setVirtualFriendList: React.Dispatch>; +} +const ProfileContainer = ({ + list, + setVirtualFriendList +}: ProfileContainerProps) => { + return ( +
+ {list.map((el, index) => ( + + ))} +
+ ); +}; + +export default ProfileContainer; diff --git a/src/components/SubTitle.tsx b/src/components/SubTitle.tsx index d1343f9..bb165a4 100644 --- a/src/components/SubTitle.tsx +++ b/src/components/SubTitle.tsx @@ -15,8 +15,8 @@ const SubTitle = ({ mode }: { mode: "빠른대화" | "친구목록" }) => { }; const handleNavigate = () => { - const mode = "virtualFriend"; - navigate("/select-info", { state: mode }); + const type = mode === "빠른대화" ? "fastFriend" : "virtualFriend"; + navigate("/select-info", { state: { type: type } }); }; return ( diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 5c4165b..d3499a0 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -1,32 +1,66 @@ +import { useEffect, useState } from "react"; +import { useNavigate } from "react-router-dom"; +import type { AxiosResponse } from "axios"; +import { authInstance } from "@/api/axios"; +import { VirtualFriend } from "@/types/virtualFreind"; import Banner from "@/components/Banner"; import StrokeBanner from "@/components/StrokeBanner"; import SubTitle from "@/components/SubTitle"; import ChatStartButton from "@/components/button/ChatStartButton"; import Header from "@/components/header/Header"; +import useAuthStore from "@/store/useAuthStore"; +import ProfileContainer from "@/components/ProfileContainer"; const Home = () => { + const navigate = useNavigate(); + const { isLoggedIn } = useAuthStore(); + const [virtualFreindList, setVirtualFriendList] = useState( + [] + ); + + useEffect(() => { + const fetchFriendList = async () => { + try { + const res: AxiosResponse<{ data: VirtualFriend[] }> = + await authInstance.get("/api/virtual-friend"); + setVirtualFriendList(res.data.data); + } catch (err) { + console.error("친구 목록을 불러오지 못했습니다.", err); + navigate("/error"); + } + }; + + if (isLoggedIn) fetchFriendList(); + }, [isLoggedIn]); return ( -
-
+
+
-
+
- +
-
+
- + {isLoggedIn && virtualFreindList.length > 0 ? ( + + ) : ( + + )}