diff --git a/src/pages/FriendListPage.tsx b/src/pages/FriendListPage.tsx index f8dd699..1a68d09 100644 --- a/src/pages/FriendListPage.tsx +++ b/src/pages/FriendListPage.tsx @@ -9,7 +9,6 @@ interface Friend { } export default function FriendListPage() { - const [friendIds, setFriendIds] = useState([]) const [friends, setFriends] = useState([]) const [showDialog, setShowDialog] = useState(false) const navigate = useNavigate() @@ -17,13 +16,8 @@ export default function FriendListPage() { useEffect(() => { const fetchFriends = async () => { try { - const ids: number[] = await api.get('/friends/list').then(res => res.data) - setFriendIds(ids) - - const friendInfos = await Promise.all( - ids.map(id => api.get(`/users/${id}`).then(res => res.data)) - ) - setFriends(friendInfos) + const res = await api.get('/friends/list') + setFriends(res.data) } catch (e) { console.error('친구 목록 불러오기 실패', e) } diff --git a/src/pages/FriendRequestsPage.tsx b/src/pages/FriendRequestsPage.tsx index 651b7f7..593c141 100644 --- a/src/pages/FriendRequestsPage.tsx +++ b/src/pages/FriendRequestsPage.tsx @@ -5,6 +5,8 @@ interface FriendRequest { id: number from_user_id: number to_user_id: number + from_user_email?: string + to_user_email?: string status: 'pending' | 'accepted' | 'rejected' | 'cancelled' created_at: string } @@ -21,11 +23,17 @@ export default function FriendRequestsPage() { const res = await api.get(url) setRequests(res.data) - const userIds = res.data.map((r: FriendRequest) => - tab === 'received' ? r.from_user_id : r.to_user_id - ) const emails = await Promise.all( - userIds.map(id => api.get(`/users/${id}`).then(res => [id, res.data.email])) + res.data.map(async (r: FriendRequest) => { + const targetId = tab === 'received' ? r.from_user_id : r.to_user_id + const emailFromResponse = + tab === 'received' ? r.from_user_email : r.to_user_email + if (emailFromResponse) { + return [targetId, emailFromResponse] as [number, string] + } + const user = await api.get(`/users/${targetId}`) + return [targetId, user.data.email] as [number, string] + }) ) setEmailMap(Object.fromEntries(emails)) } @@ -35,13 +43,31 @@ export default function FriendRequestsPage() { const handleRespond = async (id: number, accept: boolean) => { await api.post(`/friends/requests/${id}/respond`, null, { params: { accept } }) alert(accept ? '친구 요청을 수락했습니다.' : '친구 요청을 거절했습니다.') - setRequests(requests.filter(r => r.id !== id)) + const req = requests.find(r => r.id === id) + const targetId = req ? (tab === 'received' ? req.from_user_id : req.to_user_id) : null + setRequests(prev => prev.filter(r => r.id !== id)) + if (targetId !== null) { + setEmailMap(prev => { + const newMap = { ...prev } + delete newMap[targetId] + return newMap + }) + } } const handleCancel = async (id: number) => { await api.post(`/friends/requests/${id}/cancel`) alert('요청을 취소했습니다.') - setRequests(requests.filter(r => r.id !== id)) + const req = requests.find(r => r.id === id) + const targetId = req ? (tab === 'received' ? req.from_user_id : req.to_user_id) : null + setRequests(prev => prev.filter(r => r.id !== id)) + if (targetId !== null) { + setEmailMap(prev => { + const newMap = { ...prev } + delete newMap[targetId] + return newMap + }) + } } return (