Like.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { useEffect, useState } from "react";
  2. import { Button, Space, Tooltip } from "antd";
  3. import {
  4. LikeOutlined,
  5. LikeFilled,
  6. StarOutlined,
  7. StarFilled,
  8. EyeOutlined,
  9. EyeFilled,
  10. } from "@ant-design/icons";
  11. import { delete_, get, post } from "../../request";
  12. import type {
  13. ILikeCount,
  14. ILikeCountListResponse,
  15. ILikeCountResponse,
  16. ILikeRequest,
  17. TLikeType,
  18. } from "../../api/like";
  19. interface IWidget {
  20. resId?: string;
  21. resType?: string;
  22. }
  23. const Like = ({ resId, resType }: IWidget) => {
  24. const [like, setLike] = useState<ILikeCount>();
  25. const [favorite, setFavorite] = useState<ILikeCount>();
  26. const [watch, setWatch] = useState<ILikeCount>();
  27. useEffect(() => {
  28. if (!resId) {
  29. return;
  30. }
  31. const url = `/v2/like?view=count&target_id=${resId}`;
  32. console.info("api request", url);
  33. get<ILikeCountListResponse>(url).then((json) => {
  34. console.info("api response", json);
  35. if (json.ok) {
  36. setLike(json.data.find((value) => value.type === "like"));
  37. setFavorite(json.data.find((value) => value.type === "favorite"));
  38. setWatch(json.data.find((value) => value.type === "watch"));
  39. }
  40. });
  41. }, [resId]);
  42. const setStatus = (data: ILikeCount) => {
  43. switch (data.type) {
  44. case "like":
  45. setLike(data);
  46. break;
  47. case "favorite":
  48. setFavorite(data);
  49. break;
  50. case "watch":
  51. setWatch(data);
  52. break;
  53. }
  54. };
  55. const add = (type: TLikeType) => {
  56. if (!resId || !resType) {
  57. return;
  58. }
  59. const url = `/v2/like`;
  60. post<ILikeRequest, ILikeCountResponse>(url, {
  61. type: type,
  62. target_id: resId,
  63. target_type: resType,
  64. }).then((json) => {
  65. if (json.ok) {
  66. setStatus(json.data);
  67. }
  68. });
  69. };
  70. const remove = (id?: string) => {
  71. if (!resId || !resType || !id) {
  72. return;
  73. }
  74. const url = `/v2/like/${id}`;
  75. console.info("api request", url);
  76. delete_<ILikeCountResponse>(url).then((json) => {
  77. console.info("api response", json);
  78. if (json.ok) {
  79. setStatus(json.data);
  80. }
  81. });
  82. };
  83. return (
  84. <Space>
  85. <Button
  86. type="text"
  87. icon={like?.selected ? <LikeFilled /> : <LikeOutlined />}
  88. onClick={() => {
  89. if (like?.selected) {
  90. remove(like.my_id);
  91. } else {
  92. add("like");
  93. }
  94. }}
  95. >
  96. {like?.count === 0 ? <></> : like?.count}
  97. </Button>
  98. <Button
  99. type="text"
  100. icon={favorite?.selected ? <StarFilled /> : <StarOutlined />}
  101. onClick={() => {
  102. if (favorite?.selected) {
  103. remove(favorite.my_id);
  104. } else {
  105. add("favorite");
  106. }
  107. }}
  108. >
  109. {favorite?.count === 0 ? <></> : favorite?.count}
  110. </Button>
  111. <Tooltip title="关注">
  112. <Button
  113. type="text"
  114. icon={watch?.selected ? <EyeFilled /> : <EyeOutlined />}
  115. onClick={() => {
  116. if (watch?.selected) {
  117. remove(watch.my_id);
  118. } else {
  119. add("watch");
  120. }
  121. }}
  122. >
  123. {watch?.count === 0 ? <></> : watch?.count}
  124. </Button>
  125. </Tooltip>
  126. </Space>
  127. );
  128. };
  129. export default Like;