QaList.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { useEffect, useState } from "react";
  2. import type { TResType } from "./DiscussionListCard";
  3. import { get } from "../../request";
  4. import type { ICommentListResponse } from "../../api/Comment";
  5. import DiscussionItem, { type IComment } from "./DiscussionItem";
  6. interface IWidget {
  7. resId?: string;
  8. resType?: TResType;
  9. onSelect?: Function;
  10. onReply?: Function;
  11. }
  12. const QaListWidget = ({ resId, resType, onSelect, _____onReply }: IWidget) => {
  13. const [data, setData] = useState<IComment[]>();
  14. useEffect(() => {
  15. if (!resType || !resType) {
  16. return;
  17. }
  18. let url: string = `/v2/discussion?res_type=${resType}&view=res_id&id=${resId}`;
  19. url += "&dir=asc&type=qa&status=active,close";
  20. console.info("api request", url);
  21. get<ICommentListResponse>(url).then((json) => {
  22. if (json.ok) {
  23. console.debug("discussion api response", json);
  24. const items: IComment[] = json.data.rows.map((item, _id) => {
  25. return {
  26. id: item.id,
  27. resId: item.res_id,
  28. resType: item.res_type,
  29. type: item.type,
  30. user: item.editor,
  31. title: item.title,
  32. parent: item.parent,
  33. tplId: item.tpl_id,
  34. content: item.content,
  35. summary: item.summary,
  36. status: item.status,
  37. childrenCount: item.children_count,
  38. createdAt: item.created_at,
  39. updatedAt: item.updated_at,
  40. };
  41. });
  42. setData(items);
  43. }
  44. });
  45. }, []);
  46. return (
  47. <>
  48. {data
  49. ?.filter((value) => !value.parent)
  50. .map((question, index) => {
  51. return (
  52. <div key={`div_${index}`}>
  53. <DiscussionItem
  54. key={index}
  55. data={question}
  56. onSelect={(
  57. e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
  58. value: IComment
  59. ) => {
  60. if (typeof onSelect !== "undefined") {
  61. onSelect(e, value);
  62. }
  63. }}
  64. />
  65. <div
  66. style={{
  67. marginLeft: 16,
  68. borderLeft: "2px solid gray",
  69. padding: 4,
  70. }}
  71. >
  72. {data
  73. ?.filter((value) => value.parent === question.id)
  74. .map((item, id) => {
  75. return <DiscussionItem key={id} data={item} />;
  76. })}
  77. </div>
  78. </div>
  79. );
  80. })}
  81. </>
  82. );
  83. };
  84. export default QaListWidget;