1- /** @jsxImportSource @emotion /react */
2- import { jsx , css } from '@emotion/react' ;
1+ import { css } from '@emotion/react' ;
32import { useCallback , useState , useEffect } from 'react' ;
43import { useNavigate } from 'react-router-dom' ;
54import styled from '@emotion/styled' ;
@@ -13,6 +12,9 @@ import { useUserContext } from 'contexts/UserContext';
1312import { IMAGE_URLS } from 'utils/constants/images' ;
1413import displayedAt from 'utils/functions/displayedAt' ;
1514import IconButton from 'components/basic/Icon/IconButton' ;
15+ import { COMMENT , HEART , HEART_RED } from 'utils/constants/icons/names' ;
16+ import { LIKE } from 'utils/constants/notificationTypes' ;
17+ import LoginRequireModal from 'components/Modal/customs/LoginRequireModal' ;
1618
1719const PostBody = ( { index, post, isDetailPage = false } ) => {
1820 const { _id : postId , image, likes, comments, createdAt, author } = post || { } ;
@@ -21,7 +23,8 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
2123 const [ heartCount , setHeartCount ] = useState ( likes . length ) ;
2224 const [ likeId , setLikeId ] = useState ( '' ) ;
2325 const [ isShown , setIsShown ] = useState ( false ) ;
24- const [ localToken ] = useLocalToken ( ) ;
26+ const [ modalOn , setModalOn ] = useState ( false ) ;
27+ const [ token ] = useLocalToken ( ) ;
2528 const { currentUser } = useUserContext ( ) ;
2629 const [ , setCurrentPostIndex ] = useScrollPosition ( ) ;
2730 const navigate = useNavigate ( ) ;
@@ -55,24 +58,29 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
5558 } , [ setCurrentPostIndex , index , postId , post , isDetailPage , navigate ] ) ;
5659
5760 const handleClickHeart = useCallback ( async ( ) => {
61+ if ( ! token ) {
62+ setModalOn ( true ) ;
63+ return ;
64+ }
65+
5866 setOnHeart ( ! onHeart ) ;
5967 if ( ! onHeart ) {
6068 setHeartCount ( heartCount + 1 ) ;
61- if ( localToken && postId ) {
62- const like = await setLike ( localToken , postId ) . then ( ( res ) => res . data ) ;
69+ if ( token && postId ) {
70+ const like = await setLike ( token , postId ) . then ( ( res ) => res . data ) ;
6371 setLikeId ( like . _id ) ;
6472 if ( currentUser . id !== author . _id ) {
65- await setNotification ( localToken , ' LIKE' , like . _id , author . _id , postId ) ;
73+ await setNotification ( token , LIKE , like . _id , author . _id , postId ) ;
6674 }
6775 }
6876 } else {
6977 setHeartCount ( heartCount - 1 ) ;
70- if ( localToken && likeId ) {
71- await setDisLike ( localToken , likeId ) . then ( ( res ) => res . data ) ;
78+ if ( token && likeId ) {
79+ await setDisLike ( token , likeId ) . then ( ( res ) => res . data ) ;
7280 setLikeId ( '' ) ;
7381 }
7482 }
75- } , [ onHeart , heartCount , postId , likeId , author . _id , currentUser , localToken ] ) ;
83+ } , [ onHeart , heartCount , postId , likeId , author . _id , currentUser , token ] ) ;
7684
7785 const handleClickTag = useCallback (
7886 ( tag ) => {
@@ -86,9 +94,13 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
8694 [ index , setCurrentPostIndex , navigate ] ,
8795 ) ;
8896
89- const handleClickMore = ( ) => {
97+ const handleClickMore = useCallback ( ( ) => {
9098 setIsShown ( true ) ;
91- } ;
99+ } , [ ] ) ;
100+
101+ const handleCloseModal = useCallback ( ( ) => {
102+ setModalOn ( false ) ;
103+ } , [ ] ) ;
92104
93105 return (
94106 < Container >
@@ -103,7 +115,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
103115 < Contents >
104116 < IconButtons >
105117 < IconButton
106- name = { onHeart ? ' HEART_RED' : ' HEART' } // Todo: 상수화
118+ name = { onHeart ? HEART_RED : HEART }
107119 size = { 22 }
108120 style = { IconButtonStyle }
109121 onClick = { handleClickHeart }
@@ -112,7 +124,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
112124 { heartCount }
113125 </ Text >
114126 </ IconButton >
115- < IconButton name = " COMMENT" size = { 22 } onClick = { navigateToDetailPage } >
127+ < IconButton name = { COMMENT } size = { 22 } onClick = { navigateToDetailPage } >
116128 < Text fontSize = { 16 } style = { IconButtonTextStyle } >
117129 { comments . length }
118130 </ Text >
@@ -135,6 +147,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
135147 </ Tags >
136148 < DateText > { displayedAt ( createdAt ) } </ DateText >
137149 </ Contents >
150+ < LoginRequireModal visible = { modalOn } onClose = { handleCloseModal } />
138151 </ Container >
139152 ) ;
140153} ;
0 commit comments