Skip to content

Commit

Permalink
refactor: delete like state
Browse files Browse the repository at this point in the history
  • Loading branch information
seoulyego committed Oct 31, 2024
1 parent 86116f7 commit eb5e8eb
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/components/Button/PreviewLikeButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React from 'react'
import PropTypes from 'prop-types'
import { FaHeart, FaRegHeart } from 'react-icons/fa6'
import useProductLike from '@hooks/useProductLike'
Expand All @@ -11,7 +11,6 @@ function PreviewLikeButton({
likeContent = false,
iconSize = '1.3rem',
}) {
const [isLiked, setIsLiked] = useState(likeContent)
const { likeProduct, unlikeProduct } = useProductLike(productId)
const popLoginModal = useLoginModal()

Expand All @@ -22,28 +21,23 @@ function PreviewLikeButton({
}

try {
if (!isLiked) await likeProduct()
if (!likeContent) await likeProduct()
else await unlikeProduct()
setIsLiked((prev) => !prev)
} catch (error) {
console.error(error)
alert('다시 시도해 주세요')
}
}

useEffect(() => {
setIsLiked(likeContent)
}, [likeContent])

const iconValue = {
color: isLiked ? 'red' : 'darkgray',
color: likeContent ? 'red' : 'darkgray',
size: iconSize,
}

return (
<IconTextButton
value={iconValue}
icon={isLiked ? <FaHeart /> : <FaRegHeart />}
icon={likeContent ? <FaHeart /> : <FaRegHeart />}
handleClick={handleLike}
/>
)
Expand Down

0 comments on commit eb5e8eb

Please sign in to comment.