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' ;
@@ -15,6 +14,7 @@ import displayedAt from 'utils/functions/displayedAt';
1514import IconButton from 'components/basic/Icon/IconButton' ;
1615import { COMMENT , HEART , HEART_RED } from 'utils/constants/icons/names' ;
1716import { LIKE } from 'utils/constants/notificationTypes' ;
17+ import LoginRequireModal from 'components/Modal/customs/LoginRequireModal' ;
1818
1919const PostBody = ( { index, post, isDetailPage = false } ) => {
2020 const { _id : postId , image, likes, comments, createdAt, author } = post || { } ;
@@ -23,7 +23,8 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
2323 const [ heartCount , setHeartCount ] = useState ( likes . length ) ;
2424 const [ likeId , setLikeId ] = useState ( '' ) ;
2525 const [ isShown , setIsShown ] = useState ( false ) ;
26- const [ localToken ] = useLocalToken ( ) ;
26+ const [ modalOn , setModalOn ] = useState ( false ) ;
27+ const [ token ] = useLocalToken ( ) ;
2728 const { currentUser } = useUserContext ( ) ;
2829 const [ , setCurrentPostIndex ] = useScrollPosition ( ) ;
2930 const navigate = useNavigate ( ) ;
@@ -57,24 +58,29 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
5758 } , [ setCurrentPostIndex , index , postId , post , isDetailPage , navigate ] ) ;
5859
5960 const handleClickHeart = useCallback ( async ( ) => {
61+ if ( ! token ) {
62+ setModalOn ( true ) ;
63+ return ;
64+ }
65+
6066 setOnHeart ( ! onHeart ) ;
6167 if ( ! onHeart ) {
6268 setHeartCount ( heartCount + 1 ) ;
63- if ( localToken && postId ) {
64- 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 ) ;
6571 setLikeId ( like . _id ) ;
6672 if ( currentUser . id !== author . _id ) {
67- await setNotification ( localToken , LIKE , like . _id , author . _id , postId ) ;
73+ await setNotification ( token , LIKE , like . _id , author . _id , postId ) ;
6874 }
6975 }
7076 } else {
7177 setHeartCount ( heartCount - 1 ) ;
72- if ( localToken && likeId ) {
73- await setDisLike ( localToken , likeId ) . then ( ( res ) => res . data ) ;
78+ if ( token && likeId ) {
79+ await setDisLike ( token , likeId ) . then ( ( res ) => res . data ) ;
7480 setLikeId ( '' ) ;
7581 }
7682 }
77- } , [ onHeart , heartCount , postId , likeId , author . _id , currentUser , localToken ] ) ;
83+ } , [ onHeart , heartCount , postId , likeId , author . _id , currentUser , token ] ) ;
7884
7985 const handleClickTag = useCallback (
8086 ( tag ) => {
@@ -88,9 +94,13 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
8894 [ index , setCurrentPostIndex , navigate ] ,
8995 ) ;
9096
91- const handleClickMore = ( ) => {
97+ const handleClickMore = useCallback ( ( ) => {
9298 setIsShown ( true ) ;
93- } ;
99+ } , [ ] ) ;
100+
101+ const handleCloseModal = useCallback ( ( ) => {
102+ setModalOn ( false ) ;
103+ } , [ ] ) ;
94104
95105 return (
96106 < Container >
@@ -137,6 +147,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
137147 </ Tags >
138148 < DateText > { displayedAt ( createdAt ) } </ DateText >
139149 </ Contents >
150+ < LoginRequireModal visible = { modalOn } onClose = { handleCloseModal } />
140151 </ Container >
141152 ) ;
142153} ;
0 commit comments