DiscussionItem.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Avatar } from "antd";
  2. import { useEffect, useState } from "react";
  3. import type { IUser } from "../auth/User";
  4. import DiscussionShow from "./DiscussionShow";
  5. import DiscussionEdit from "./DiscussionEdit";
  6. import type { TResType } from "./DiscussionListCard";
  7. import type { TDiscussionType } from "./Discussion";
  8. interface IWidget {
  9. data: IComment;
  10. isFocus?: boolean;
  11. hideTitle?: boolean;
  12. onSelect?: Function;
  13. onCreated?: Function;
  14. onDelete?: Function;
  15. onReply?: Function;
  16. onClose?: Function;
  17. onConvert?: Function;
  18. }
  19. const DiscussionItemWidget = ({
  20. data,
  21. isFocus = false,
  22. hideTitle = false,
  23. onSelect,
  24. onCreated,
  25. onDelete,
  26. onReply,
  27. onClose,
  28. onConvert,
  29. }: IWidget) => {
  30. const [edit, setEdit] = useState(false);
  31. const [currData, setCurrData] = useState<IComment>(data);
  32. useEffect(() => {
  33. setCurrData(data);
  34. }, [data]);
  35. return (
  36. <div
  37. id={`answer-${data.id}`}
  38. style={{
  39. display: "flex",
  40. width: "100%",
  41. border: isFocus ? "2px solid blue" : "unset",
  42. borderRadius: 10,
  43. padding: 5,
  44. }}
  45. >
  46. <div style={{ width: "2em", display: "none" }}>
  47. <Avatar size="small">{data.user?.nickName?.slice(0, 1)}</Avatar>
  48. </div>
  49. <div style={{ width: "100%" }}>
  50. {edit ? (
  51. <DiscussionEdit
  52. data={currData}
  53. onUpdated={(e: IComment) => {
  54. setCurrData(e);
  55. setEdit(false);
  56. }}
  57. onCreated={(e: IComment) => {
  58. if (typeof onCreated !== "undefined") {
  59. onCreated(e);
  60. }
  61. }}
  62. onClose={() => setEdit(false)}
  63. />
  64. ) : (
  65. <DiscussionShow
  66. data={currData}
  67. hideTitle={hideTitle}
  68. onEdit={() => {
  69. setEdit(true);
  70. }}
  71. onSelect={(e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
  72. if (typeof onSelect !== "undefined") {
  73. onSelect(e, currData);
  74. }
  75. }}
  76. onDelete={(_id: string) => {
  77. if (typeof onDelete !== "undefined") {
  78. onDelete();
  79. }
  80. }}
  81. onReply={() => {
  82. if (typeof onReply !== "undefined") {
  83. onReply(currData);
  84. }
  85. }}
  86. onClose={(value: boolean) => {
  87. if (typeof onClose !== "undefined") {
  88. onClose(value);
  89. }
  90. }}
  91. onConvert={(value: TDiscussionType) => {
  92. if (typeof onConvert !== "undefined") {
  93. onConvert(value);
  94. }
  95. }}
  96. />
  97. )}
  98. </div>
  99. </div>
  100. );
  101. };
  102. export default DiscussionItemWidget;