1- import React , { useState } from "react" ;
1+ import React , { useState , useEffect } from "react" ;
22import { Link , useLocation , useParams } from "react-router-dom" ;
33import { useAuth } from "../../../../contexts/AuthContext" ;
44import { CommunitypageWrapper } from "../../../../components/CommunitypageWrapper" ;
5- import { PageButton } from "../../../../components/PageButton" ;
65import "./style.css" ;
76
8- const ALL_POSTS = [
9- // TODO: Backend Integration: Replace with API call to fetch all community posts
7+ // Default posts for fallback if localStorage is empty
8+ const DEFAULT_POSTS = [
109 { id : 1 , title : "돼지고기 100g 나눔합니다." , author : "test3User" , date : "2025-11-03" , category : "나눔" , content : "돼지고기를 너무 많이 샀네요 남는 돼지고기 나눔해요" } ,
1110 { id : 2 , title : "양파 2개 나눔해요" , author : "user123" , date : "2025-11-02" , category : "나눔" , content : "양파 2개 필요하신 분 가져가세요" } ,
1211 { id : 3 , title : "닭고기 500g 나눔" , author : "foodlover" , date : "2025-11-01" , category : "나눔" , content : "신선한 닭고기 나눔합니다" } ,
@@ -24,35 +23,45 @@ export const Communitycontentpage = () => {
2423 const { id } = useParams ( ) ;
2524 const { user, isAuthenticated } = useAuth ( ) ;
2625 const [ newComment , setNewComment ] = useState ( "" ) ;
27-
28- // Load comments from localStorage for this specific post
29- const [ comments , setComments ] = useState ( ( ) => {
26+ const [ allCommunityPosts , setAllCommunityPosts ] = useState ( [ ] ) ;
27+ const [ comments , setComments ] = useState ( [ ] ) ;
28+
29+ // Load comments from localStorage for this specific post, and re-load when 'id' changes
30+ useEffect ( ( ) => {
3031 const storedComments = localStorage . getItem ( `comments_${ id } ` ) ;
31- return storedComments ? JSON . parse ( storedComments ) : [ ] ;
32- } ) ;
33-
34- const post = location . state ?. post || {
35- // TODO: Backend Integration: Fetch post details by ID if not available in state
36- id : id ,
37- title : "돼지고기 100g 나눔합니다." ,
38- author : "test3User" ,
39- date : "2025-11-03" ,
40- category : "나눔" ,
41- content : "돼지고기를 너무 많이 샀네요 남는 돼지고기 나눔해요"
42- } ;
32+ setComments ( storedComments ? JSON . parse ( storedComments ) : [ ] ) ;
33+ } , [ id ] ) ;
34+
35+ useEffect ( ( ) => {
36+ // Load all community posts from localStorage to find related posts
37+ const storedPosts = JSON . parse ( localStorage . getItem ( "communityPosts" ) || "[]" ) ;
38+ const combined = [ ...storedPosts , ...DEFAULT_POSTS ] ;
39+ setAllCommunityPosts ( combined ) ;
40+ } , [ ] ) ;
41+
42+ const post = location . state ?. post ||
43+ allCommunityPosts . find ( p => p . id === parseInt ( id ) ) ||
44+ {
45+ id : id ,
46+ title : "게시글을 찾을 수 없습니다." ,
47+ author : "알 수 없음" ,
48+ date : "" ,
49+ category : "" ,
50+ content : "해당 게시글을 불러오는 데 실패했습니다."
51+ } ;
4352
4453 // Get related posts (2 before and 2 after current post)
45- const currentIndex = ALL_POSTS . findIndex ( p => p . id === parseInt ( id ) ) ;
54+ const currentIndex = allCommunityPosts . findIndex ( p => p . id === parseInt ( id ) ) ;
4655 const relatedPosts = [ ] ;
4756
4857 // Get 2 posts before
4958 for ( let i = Math . max ( 0 , currentIndex - 2 ) ; i < currentIndex ; i ++ ) {
50- if ( ALL_POSTS [ i ] ) relatedPosts . push ( ALL_POSTS [ i ] ) ;
59+ if ( allCommunityPosts [ i ] ) relatedPosts . push ( allCommunityPosts [ i ] ) ;
5160 }
5261
5362 // Get 2 posts after
54- for ( let i = currentIndex + 1 ; i <= Math . min ( ALL_POSTS . length - 1 , currentIndex + 2 ) ; i ++ ) {
55- if ( ALL_POSTS [ i ] ) relatedPosts . push ( ALL_POSTS [ i ] ) ;
63+ for ( let i = currentIndex + 1 ; i <= Math . min ( allCommunityPosts . length - 1 , currentIndex + 2 ) ; i ++ ) {
64+ if ( allCommunityPosts [ i ] ) relatedPosts . push ( allCommunityPosts [ i ] ) ;
5665 }
5766
5867 const handleCommentSubmit = ( e ) => {
@@ -81,13 +90,8 @@ export const Communitycontentpage = () => {
8190 setNewComment ( "" ) ;
8291 } ;
8392
84-
8593 return (
8694 < div className = "communitycontentpage" >
87- < Link className = "communitycontentpage-wrapper" to = "/communitypage" >
88- < div className = "text-wrapper-5" > 재료 나눔 게시판</ div >
89- </ Link >
90-
9195 < div className = "div-3" >
9296 < div className = "content-category" >
9397 < img
@@ -106,7 +110,7 @@ export const Communitycontentpage = () => {
106110 < div className = "navbar" >
107111 < div className = "text-wrapper-6" > 작성자</ div >
108112
109- < div className = "text-wrapper-6" > { post . author } </ div >
113+ < div className = "text-wrapper-6" > { post . author } 님 </ div >
110114
111115 < div className = "text-wrapper-6" > 작성일</ div >
112116
@@ -127,7 +131,7 @@ export const Communitycontentpage = () => {
127131 < div className = "comments-list" >
128132 { comments . map ( ( comment ) => (
129133 < div key = { comment . id } className = "comment-item" >
130- < div className = "comment-author" > { comment . author } </ div >
134+ < div className = "comment-author" > { comment . author } 님 </ div >
131135 < div className = "comment-content" > { comment . content } </ div >
132136 < div className = "comment-date" > { comment . date } </ div >
133137 </ div >
0 commit comments