11import { useEffect , useState } from 'react' ;
2- import { useLocation , useNavigate } from 'react-router' ;
32import { twMerge } from 'tailwind-merge' ;
43
54import {
65 getSharePostDetail ,
7- postShareProposalApproval ,
86 SharePost ,
97 postSharePostLike ,
108 getSharePostLikeCount ,
119} from '@/apis/share' ;
12- import BlurImg from '@/assets/images/landing-blur.png' ;
1310import ReportModal from '@/components/ReportModal' ;
1411
1512import Header from './components/Header' ;
1613import Letter from './components/Letter' ;
14+ import { useParams } from 'react-router' ;
15+ import useAuthStore from '@/stores/authStore' ;
1716
18- interface ShareLetterPreviewProps {
19- confirmDisabled ?: boolean ;
20- children ?: React . ReactNode ;
21- }
22-
23- const LetterBoardDetailPage = ( { confirmDisabled } : ShareLetterPreviewProps ) => {
24- const [ likeCount , setLikeCount ] = useState ( 122 ) ;
17+ const LetterBoardDetailPage = ( ) => {
18+ const [ likeCount , setLikeCount ] = useState ( 0 ) ;
2519 const [ isLike , setIsLike ] = useState ( false ) ;
26- const isWriter = false ;
27- const [ activeReportModal , setActiveReportModal ] = useState ( false ) ;
28- const sharePostId : string = location . pathname . split ( '/' ) [ 3 ] ;
29- // const location = useLocation();
30- const navigate = useNavigate ( ) ;
31- // const isShareLetterPreview = location.state?.isShareLetterPreview || false;
32- const isShareLetterPreview = false ;
20+ const [ isWriter , setIsWriter ] = useState ( false ) ;
3321 const [ postDetail , setPostDetail ] = useState < SharePost > ( ) ;
22+ const [ activeReportModal , setActiveReportModal ] = useState ( false ) ;
23+
24+ const { id } = useParams ( ) ;
25+
26+ const myZipCode = useAuthStore . getState ( ) . zipCode ;
3427
35- const postLike = async ( ) => {
28+ const postLike = async ( sharePostId : string ) => {
3629 try {
3730 const response = await postSharePostLike ( sharePostId ) ;
3831 if ( ! response ) throw new Error ( 'error while fetching like count' ) ;
@@ -43,24 +36,13 @@ const LetterBoardDetailPage = ({ confirmDisabled }: ShareLetterPreviewProps) =>
4336 }
4437 } ;
4538
46- const handleToggleLike = ( ) => {
39+ const handleToggleLike = ( sharePostId : string ) => {
40+ if ( sharePostId === 'error' ) {
41+ return ;
42+ }
4743 setLikeCount ( ( prev ) => prev + ( isLike ? - 1 : 1 ) ) ;
4844 setIsLike ( ( prev ) => ! prev ) ;
49- postLike ( ) ;
50- } ;
51-
52- const handleProposalApproval = async (
53- action : 'approve' | 'reject' ,
54- shareProposalId : number = location . state ?. postDetail ?. sharePostId ,
55- ) => {
56- try {
57- const result = await postShareProposalApproval ( shareProposalId , action ) ;
58- console . log ( `✅ 편지 공유 ${ action === 'approve' ? '수락' : '거절' } 됨:` , result ) ;
59-
60- navigate ( '/' ) ;
61- } catch ( error ) {
62- console . error ( error ) ;
63- }
45+ postLike ( sharePostId ) ;
6446 } ;
6547
6648 useEffect ( ( ) => {
@@ -80,27 +62,31 @@ const LetterBoardDetailPage = ({ confirmDisabled }: ShareLetterPreviewProps) =>
8062 console . log ( '✅ 편지 좋아요 갯수:' , response ) ;
8163 setLikeCount ( response . likeCount ) ;
8264 setIsLike ( response . liked ) ;
65+ console . log ( 'myZip' , myZipCode ) ;
66+ console . log ( 'responseZip' , response . zipCode ) ;
67+ console . log ( 'responseZip' , response ) ;
68+
69+ if ( myZipCode === response . zipCode || ! response . zipCode ) {
70+ setIsWriter ( true ) ;
71+ }
8372 } catch ( error ) {
8473 console . error ( '❌ 편지 좋아요 갯수를 가져오는 중 에러가 발생했습니다' , error ) ;
8574 throw new Error ( '편지 좋아요 갯수 가져오기 실패' ) ;
8675 }
8776 } ;
8877
89- // if (location.state?.postDetail) {
90- fetchPostDetail ( sharePostId ) ;
91- fetchLikeCounts ( sharePostId ) ;
92- // } else {
93- // console.warn('postDetail not found in location.state');
94- // }
95- // }, [location.state]);
78+ if ( id ) {
79+ fetchPostDetail ( id ) ;
80+ fetchLikeCounts ( id ) ;
81+ }
9682 } , [ ] ) ;
9783
9884 return (
9985 < >
10086 { activeReportModal && (
10187 < ReportModal
102- reportType = "POST "
103- letterId = { parseInt ( sharePostId ) }
88+ reportType = "SHARE_POST "
89+ letterId = { id ? parseInt ( id ) : 0 }
10490 onClose = { ( ) => setActiveReportModal ( false ) }
10591 />
10692 ) }
@@ -109,9 +95,8 @@ const LetterBoardDetailPage = ({ confirmDisabled }: ShareLetterPreviewProps) =>
10995 likeCount = { likeCount }
11096 isLike = { isLike }
11197 isWriter = { isWriter }
112- onToggleLike = { handleToggleLike }
98+ onToggleLike = { ( ) => ( id ? handleToggleLike ( id ) : handleToggleLike ( 'error' ) ) }
11399 onOpenReportModal = { ( ) => setActiveReportModal ( true ) }
114- isShareLetterPreview = { isShareLetterPreview }
115100 />
116101 < main className = "px-5 pt-18 pb-3" >
117102 < p className = "body-b mb-6 px-5" > FROM. { postDetail ?. zipCode } </ p >
@@ -132,34 +117,6 @@ const LetterBoardDetailPage = ({ confirmDisabled }: ShareLetterPreviewProps) =>
132117 />
133118 ) ) }
134119 </ section >
135-
136- { isShareLetterPreview && (
137- < >
138- < img
139- src = { BlurImg }
140- alt = "landing blur"
141- className = "fixed bottom-0 left-0 z-10 w-screen"
142- />
143- < section className = "fixed bottom-[30px] left-1/2 z-20 flex w-73 translate-x-[-50%] gap-6" >
144- < button
145- type = "button"
146- className = "body-m secondary-btn h-10 flex-1 basis-1/2"
147- onClick = { ( ) => handleProposalApproval ( 'reject' , postDetail ?. sharePostId ) }
148- >
149- 거부하기
150- </ button >
151-
152- < button
153- type = "button"
154- className = "primary-btn body-m h-10 flex-1 basis-1/2"
155- disabled = { confirmDisabled }
156- onClick = { ( ) => handleProposalApproval ( 'approve' , postDetail ?. sharePostId ) }
157- >
158- 승인하기
159- </ button >
160- </ section >
161- </ >
162- ) }
163120 </ main >
164121 </ div >
165122 </ >
0 commit comments