|
1 | | -import { useState } from 'react' |
2 | | -import { kunFetchPut } from '~/utils/kunFetch' |
3 | | -import { Button } from '@heroui/button' |
4 | | -import { Tooltip } from '@heroui/tooltip' |
5 | | -import { Heart } from 'lucide-react' |
6 | | -import { useUserStore } from '~/store/userStore' |
7 | | -import toast from 'react-hot-toast' |
8 | | -import { kunErrorHandler } from '~/utils/kunErrorHandler' |
9 | | -import { cn } from '~/utils/cn' |
10 | | -import { PatchResourceHtml } from '~/types/api/patch' |
11 | | - |
12 | | -interface Props { |
13 | | - resource: PatchResourceHtml |
14 | | -} |
15 | | - |
16 | | -export const ResourceLikeButton = ({ resource }: Props) => { |
17 | | - const { user } = useUserStore((state) => state) |
18 | | - |
19 | | - const [liked, setLiked] = useState(resource.isLike) |
20 | | - const [likeCount, setLikeCount] = useState(resource.likeCount) |
21 | | - const [loading, setLoading] = useState(false) |
22 | | - |
23 | | - const toggleLike = async () => { |
24 | | - if (!user.uid) { |
25 | | - toast.error('请登录以点赞') |
26 | | - return |
27 | | - } |
28 | | - |
29 | | - if (resource.user.id === user.uid) { |
30 | | - toast.error('您不能给自己点赞') |
31 | | - return |
32 | | - } |
33 | | - |
34 | | - setLoading(true) |
35 | | - |
36 | | - const res = await kunFetchPut<KunResponse<boolean>>( |
37 | | - '/patch/resource/like', |
38 | | - { resourceId: resource.id } |
39 | | - ) |
40 | | - |
41 | | - setLoading(false) |
42 | | - kunErrorHandler(res, (value) => { |
43 | | - setLiked(value) |
44 | | - setLikeCount((prev) => (value ? prev + 1 : prev - 1)) |
45 | | - }) |
46 | | - } |
47 | | - |
48 | | - return ( |
49 | | - <Tooltip key="like" color="default" content="点赞" placement="bottom"> |
50 | | - <Button |
51 | | - variant="light" |
52 | | - disabled={loading} |
53 | | - onPress={toggleLike} |
54 | | - className="min-w-0 px-2" |
55 | | - > |
56 | | - <Heart |
57 | | - fill={liked ? '#f31260' : '#00000000'} |
58 | | - className={cn('w-4 h-4', liked ? 'text-danger-500' : '')} |
59 | | - /> |
60 | | - {likeCount} |
61 | | - </Button> |
62 | | - </Tooltip> |
63 | | - ) |
64 | | -} |
| 1 | +import { useState } from 'react' |
| 2 | +import { kunFetchPut } from '~/utils/kunFetch' |
| 3 | +import { Button } from '@heroui/button' |
| 4 | +import { Tooltip } from '@heroui/tooltip' |
| 5 | +import { Heart } from 'lucide-react' |
| 6 | +import { useUserStore } from '~/store/userStore' |
| 7 | +import toast from 'react-hot-toast' |
| 8 | +import { kunErrorHandler } from '~/utils/kunErrorHandler' |
| 9 | +import { cn } from '~/utils/cn' |
| 10 | +import { PatchResourceHtml } from '~/types/api/patch' |
| 11 | + |
| 12 | +interface Props { |
| 13 | + resource: PatchResourceHtml |
| 14 | +} |
| 15 | + |
| 16 | +export const ResourceLikeButton = ({ resource }: Props) => { |
| 17 | + const { user } = useUserStore((state) => state) |
| 18 | + |
| 19 | + const [liked, setLiked] = useState(resource.isLike) |
| 20 | + const [likeCount, setLikeCount] = useState(resource.likeCount) |
| 21 | + const [loading, setLoading] = useState(false) |
| 22 | + |
| 23 | + const toggleLike = async () => { |
| 24 | + if (!user.uid) { |
| 25 | + toast.error('请登录以点赞') |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + if (resource.user.id === user.uid) { |
| 30 | + toast.error('您不能给自己点赞') |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + setLoading(true) |
| 35 | + |
| 36 | + const res = await kunFetchPut<KunResponse<boolean>>( |
| 37 | + '/patch/resource/like', |
| 38 | + { resourceId: resource.id } |
| 39 | + ) |
| 40 | + |
| 41 | + setLoading(false) |
| 42 | + kunErrorHandler(res, (value) => { |
| 43 | + setLiked(value) |
| 44 | + setLikeCount((prev) => (value ? prev + 1 : prev - 1)) |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <Tooltip key="like" color="default" content="点赞" placement="bottom"> |
| 50 | + <Button |
| 51 | + variant="flat" |
| 52 | + disabled={loading} |
| 53 | + onPress={toggleLike} |
| 54 | + className="min-w-0 px-2" |
| 55 | + > |
| 56 | + <Heart |
| 57 | + fill={liked ? '#f31260' : '#00000000'} |
| 58 | + className={cn('w-4 h-4', liked ? 'text-danger-500' : '')} |
| 59 | + /> |
| 60 | + {likeCount} |
| 61 | + </Button> |
| 62 | + </Tooltip> |
| 63 | + ) |
| 64 | +} |
0 commit comments