No Description

ReplyCard.tsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useState, useContext } from "react";
  2. import { Divider } from "antd";
  3. import ItemToolBar, { TOOL_ACTION_TYPE } from "./ItemToolBar";
  4. import styles from "./ReplyCard.less";
  5. import { CommentContext } from "../context";
  6. import { getUserType } from "../utils";
  7. import { toggleFavor } from "../utils/commentAjaxLogi";
  8. export const ReplyCard = ({
  9. replyContent: {
  10. user_name,
  11. user_id,
  12. user_avatar,
  13. reply_target = null,
  14. id,
  15. favored = false,
  16. favor_count,
  17. created,
  18. content,
  19. },
  20. commentId,
  21. locale,
  22. renderTime = () => null,
  23. onChangeListItem,
  24. }: {
  25. replyContent: {
  26. user_name: string;
  27. user_id: number;
  28. user_avatar: string;
  29. reply_target: null;
  30. id: string;
  31. favored: boolean;
  32. favor_count: number;
  33. created: number;
  34. content: string;
  35. };
  36. commentId: string;
  37. locale: "zh" | "en" | string;
  38. renderTime: (created: number) => any;
  39. onChangeListItem?({
  40. replyId,
  41. changeProps,
  42. }: {
  43. replyId: string;
  44. changeProps: any;
  45. }): Promise<boolean> | boolean;
  46. }) => {
  47. const [showReplyEditor, setShowReplyEditor] = useState(false);
  48. const context = useContext(CommentContext);
  49. const { currentUser }: { currentUser: any } = context;
  50. return (
  51. <article className={styles.wrapper}>
  52. <div className={styles.avatar}>
  53. <div
  54. className={styles.avatarImg}
  55. style={{
  56. backgroundImage: `url(${user_avatar})`,
  57. }}
  58. />
  59. </div>
  60. <section className={styles.content}>
  61. <header>
  62. <h4>{user_name}</h4>
  63. {renderTime(created)}
  64. </header>
  65. <div>{content}</div>
  66. <ItemToolBar
  67. authType={getUserType(currentUser, user_id)}
  68. handleReplyClick={() => {
  69. return true;
  70. }}
  71. handleNormalClick={async (actionType: string) => {
  72. console.log("actionType: ", actionType);
  73. if (actionType === TOOL_ACTION_TYPE.REPLY) {
  74. setShowReplyEditor(!showReplyEditor);
  75. }
  76. if (actionType === TOOL_ACTION_TYPE.FAVOR) {
  77. const res = await toggleFavor({
  78. favored,
  79. id: commentId,
  80. commentId: id,
  81. userId: currentUser.id,
  82. });
  83. if (res && onChangeListItem) {
  84. onChangeListItem({
  85. replyId: id,
  86. changeProps: {
  87. favor_count: res.favor_count,
  88. favored: !favored,
  89. },
  90. });
  91. }
  92. }
  93. return true;
  94. }}
  95. handlePrivateClick={(actionType: string) => {
  96. return true;
  97. }}
  98. replyCount={0}
  99. favorCount={favor_count}
  100. favored={favored}
  101. />
  102. {showReplyEditor ? (
  103. <div className={styles.replyEditorWrapper}>Editor</div>
  104. ) : null}
  105. </section>
  106. </article>
  107. );
  108. };