Changeset 99c1e45 for chapterx-frontend/src/components/story/LikeButton.tsx
- Timestamp:
- 06/24/26 16:28:50 (11 days ago)
- Branches:
- main
- Children:
- a8f4a2d
- Parents:
- 0b502c2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
chapterx-frontend/src/components/story/LikeButton.tsx
r0b502c2 r99c1e45 9 9 const API = 'https://localhost:7125/api' 10 10 11 function getAuthHeaders() {12 try {13 const token = JSON.parse(localStorage.getItem('chapterx-auth') || '{}')?.state?.token14 return token ? { Authorization: `Bearer ${token}` } : {}15 } catch { return {} }16 }17 18 11 interface LikeButtonProps { 19 12 storyId: number … … 25 18 export const LikeButton: React.FC<LikeButtonProps> = ({ storyId, authorUserId, totalLikes, onCountChange }) => { 26 19 const navigate = useNavigate() 27 const { currentUser } = useAuthStore()20 const { currentUser, token } = useAuthStore() 28 21 const { addNotification } = useNotificationStore() 22 const authHeaders = token ? { Authorization: `Bearer ${token}` } : {} 29 23 const { addToast } = useUIStore() 30 24 const [liked, setLiked] = useState(false) … … 55 49 try { 56 50 if (liked) { 57 await axios.delete(`${API}/likes/user/${currentUser.user_id}/story/${storyId}`, { headers: getAuthHeaders()})51 await axios.delete(`${API}/likes/user/${currentUser.user_id}/story/${storyId}`, { headers: authHeaders }) 58 52 setLiked(false) 59 53 setCount(c => { const n = c - 1; onCountChange?.(n); return n }) 60 54 addToast('Removed from likes', 'info') 61 55 } else { 62 await axios.post(`${API}/likes`, { userId: currentUser.user_id, storyId }, { headers: getAuthHeaders()})56 await axios.post(`${API}/likes`, { userId: currentUser.user_id, storyId }, { headers: authHeaders }) 63 57 setLiked(true) 64 58 setCount(c => { const n = c + 1; onCountChange?.(n); return n })
Note:
See TracChangeset
for help on using the changeset viewer.
