|
1 | | -import { useState, useEffect } from 'react'; |
2 | | -import { useParams } from 'react-router-dom'; |
| 1 | +import { useParams } from 'react-router'; |
| 2 | +import { useEffect, useState, useRef } from 'react'; |
| 3 | +import getRecipient from '../../api/getRecipient'; |
| 4 | +import getMessages from '../../api/getMessages'; |
3 | 5 | import HeaderService from '../../components/recipient/HeaderService/HeaderService'; |
4 | | -import fetchRecipient from '../../api/fetchRecipient'; |
| 6 | +import Card from '../../components/Card/Card'; |
| 7 | +import styles from './Recipient.module.scss'; |
5 | 8 |
|
6 | 9 | export default function Recipient() { |
7 | | - const [recipient, setRecipient] = useState(null); |
8 | 10 | const { id } = useParams(); |
| 11 | + const [postData, setPostData] = useState(null); |
| 12 | + const [messages, setMessages] = useState([]); |
| 13 | + const [offset, setOffset] = useState(0); |
| 14 | + const [loading, setLoading] = useState(false); |
| 15 | + const [hasNextMessage, setHasNextMessage] = useState(false); |
| 16 | + const observerRef = useRef(); |
9 | 17 |
|
10 | 18 | useEffect(() => { |
11 | | - if (!id) { |
12 | | - console.error('Recipient ID가 제공되지 않았습니다'); |
13 | | - return; |
14 | | - } |
15 | | - |
16 | | - async function loadData() { |
| 19 | + const fetchRecipient = async () => { |
17 | 20 | try { |
18 | | - const recipientData = await fetchRecipient(id); |
19 | | - |
20 | | - setRecipient(recipientData); |
| 21 | + const recipient = await getRecipient(id); |
| 22 | + setPostData(recipient); |
| 23 | + setHasNextMessage(recipient.messageCount > 0); |
| 24 | + setLoading(false); |
21 | 25 | } catch (error) { |
22 | | - console.error('데이터 가져오기 실패:', error); |
| 26 | + console.error('데이터 로딩 실패:', error.response.data); |
23 | 27 | } |
24 | | - } |
| 28 | + }; |
25 | 29 |
|
26 | | - loadData(); |
| 30 | + fetchRecipient(); |
27 | 31 | }, [id]); |
28 | 32 |
|
| 33 | + useEffect(() => { |
| 34 | + setLoading(true); |
| 35 | + const fetchMessages = async () => { |
| 36 | + try { |
| 37 | + const limit = offset === 0 ? 8 : 9; |
| 38 | + const newMessages = await getMessages(id, offset, limit); |
| 39 | + offset === 0 |
| 40 | + ? setMessages(newMessages.results) |
| 41 | + : setMessages((prev) => [...prev, ...newMessages.results]); |
| 42 | + if (!postData) return; |
| 43 | + setHasNextMessage(offset < postData.messageCount); |
| 44 | + setLoading(false); |
| 45 | + } catch (error) { |
| 46 | + console.error( |
| 47 | + '데이터 로딩 실패:', |
| 48 | + error.response?.data || error.message, |
| 49 | + ); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + fetchMessages(); |
| 54 | + }, [id, offset]); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + const observer = new IntersectionObserver((entries) => { |
| 58 | + const firstEntry = entries[0]; |
| 59 | + if (firstEntry.isIntersecting && hasNextMessage && !loading) { |
| 60 | + loadMoreMessages(); |
| 61 | + } |
| 62 | + }); |
| 63 | + if (observerRef.current) observer.observe(observerRef.current); |
| 64 | + return () => { |
| 65 | + if (observerRef.current) observer.unobserve(observerRef.current); |
| 66 | + }; |
| 67 | + }, [observerRef.current, hasNextMessage, loading]); |
| 68 | + |
| 69 | + const loadMoreMessages = () => { |
| 70 | + const limit = offset === 0 ? 8 : 9; |
| 71 | + setOffset((prev) => prev + limit); |
| 72 | + }; |
| 73 | + |
| 74 | + if (!postData || messages.length < 0) return <div>Loading...</div>; |
| 75 | + |
29 | 76 | return ( |
30 | | - <div> |
31 | | - <HeaderService recipient={recipient} /> |
32 | | - </div> |
| 77 | + <> |
| 78 | + <HeaderService /> |
| 79 | + <div |
| 80 | + className={`${styles['post-container']} ${!postData.backgroundImageURL ? styles[`background--${postData.backgroundColor}`] : ''}`} |
| 81 | + style={ |
| 82 | + postData.backgroundImageURL |
| 83 | + ? { backgroundImage: `url(${postData.backgroundImageURL})` } |
| 84 | + : {} |
| 85 | + } |
| 86 | + > |
| 87 | + <div className={styles['card-container']}> |
| 88 | + <Card recipientId={id} empty={true} /> |
| 89 | + {messages.map((msg) => ( |
| 90 | + <Card |
| 91 | + key={msg.id} |
| 92 | + image={msg.profileImageURL} |
| 93 | + sender={msg.sender} |
| 94 | + relationship={msg.relationship} |
| 95 | + createdAt={msg.createdAt.slice(0, 10).split('-').join('.')} |
| 96 | + > |
| 97 | + {msg.content} |
| 98 | + </Card> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + {hasNextMessage && <div ref={observerRef} className="load"></div>} |
| 102 | + </div> |
| 103 | + </> |
33 | 104 | ); |
34 | 105 | } |
0 commit comments