source: chapterx-frontend/src/components/story/LikeButton.tsx@ b62cefc

main
Last change on this file since b62cefc was b62cefc, checked in by kikisrbinoska <srbinoskakristina07@…>, 4 months ago

Added frontend and imporoved AI Suggestions service

  • Property mode set to 100644
File size: 1.9 KB
Line 
1import React from 'react'
2import { Heart } from 'lucide-react'
3import { useNavigate } from 'react-router-dom'
4import { useAuthStore } from '../../store/authStore'
5import { useStoryStore } from '../../store/storyStore'
6import { useNotificationStore } from '../../store/notificationStore'
7import { useUIStore } from '../../store/uiStore'
8
9interface LikeButtonProps {
10 storyId: number
11 authorUserId: number
12 totalLikes: number
13}
14
15export const LikeButton: React.FC<LikeButtonProps> = ({ storyId, authorUserId, totalLikes }) => {
16 const navigate = useNavigate()
17 const { currentUser } = useAuthStore()
18 const { toggleLike, isLiked } = useStoryStore()
19 const { addNotification } = useNotificationStore()
20 const { addToast } = useUIStore()
21
22 const liked = currentUser ? isLiked(currentUser.user_id, storyId) : false
23
24 const handleClick = () => {
25 if (!currentUser) {
26 addToast('Please sign in to like stories.', 'info')
27 navigate('/login')
28 return
29 }
30 toggleLike(currentUser.user_id, storyId)
31 if (!liked && currentUser.user_id !== authorUserId) {
32 addNotification({
33 user_id: authorUserId,
34 type: 'like',
35 title: 'New Like',
36 message: `${currentUser.username} liked your story.`,
37 link: `/story/${storyId}`,
38 })
39 }
40 addToast(liked ? 'Removed from likes' : 'Added to likes!', liked ? 'info' : 'success')
41 }
42
43 return (
44 <button
45 onClick={handleClick}
46 className={`flex items-center gap-2 px-4 py-2 rounded-xl border transition-all duration-200 ${
47 liked
48 ? 'bg-rose-500/20 border-rose-500/40 text-rose-400 hover:bg-rose-500/30'
49 : 'bg-slate-800 border-slate-700 text-slate-400 hover:border-rose-500/40 hover:text-rose-400 hover:bg-rose-500/10'
50 }`}
51 >
52 <Heart size={16} className={liked ? 'fill-rose-400' : ''} />
53 <span className="text-sm font-medium">{totalLikes.toLocaleString()}</span>
54 </button>
55 )
56}
Note: See TracBrowser for help on using the repository browser.