SentCanRead.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { Button, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { ReloadOutlined } from "@ant-design/icons";
  4. import { get } from "../../../request";
  5. import { TChannelType } from "../../api/Channel";
  6. import { ISentenceListResponse } from "../../api/Corpus";
  7. import { ISentence } from "../SentEdit";
  8. import SentCell from "./SentCell";
  9. import SentAdd from "./SentAdd";
  10. import { useAppSelector } from "../../../hooks";
  11. import { currentUser as _currentUser } from "../../../reducers/current-user";
  12. import { IChannel } from "../../channel/Channel";
  13. interface IWidget {
  14. book: number;
  15. para: number;
  16. wordStart: number;
  17. wordEnd: number;
  18. type: TChannelType;
  19. channelsId?: string[];
  20. reload?: boolean;
  21. onReload?: Function;
  22. onCreate?: Function;
  23. }
  24. const SentCanReadWidget = ({
  25. book,
  26. para,
  27. wordStart,
  28. wordEnd,
  29. type,
  30. channelsId,
  31. reload = false,
  32. onReload,
  33. onCreate,
  34. }: IWidget) => {
  35. const [sentData, setSentData] = useState<ISentence[]>([]);
  36. const [channels, setChannels] = useState<string[]>();
  37. const user = useAppSelector(_currentUser);
  38. const load = () => {
  39. const sentId = `${book}-${para}-${wordStart}-${wordEnd}`;
  40. let url = `/v2/sentence?view=sent-can-read&sentence=${sentId}&type=${type}&mode=edit&html=true`;
  41. url += channelsId ? `&channels=${channelsId.join()}` : "";
  42. console.log("url", url);
  43. get<ISentenceListResponse>(url)
  44. .then((json) => {
  45. if (json.ok) {
  46. console.log("sent load", json.data.rows);
  47. const channels: string[] = json.data.rows.map(
  48. (item) => item.channel.id
  49. );
  50. setChannels(channels);
  51. const newData: ISentence[] = json.data.rows.map((item) => {
  52. return {
  53. id: item.id,
  54. content: item.content,
  55. html: item.html,
  56. book: item.book,
  57. para: item.paragraph,
  58. wordStart: item.word_start,
  59. wordEnd: item.word_end,
  60. editor: item.editor,
  61. studio: item.studio,
  62. channel: item.channel,
  63. suggestionCount: item.suggestionCount,
  64. translationChannels: channelsId,
  65. updateAt: item.updated_at,
  66. };
  67. });
  68. console.log("new data", newData);
  69. setSentData(newData);
  70. } else {
  71. message.error(json.message);
  72. }
  73. })
  74. .finally(() => {
  75. if (reload && typeof onReload !== "undefined") {
  76. onReload();
  77. }
  78. });
  79. };
  80. useEffect(() => {
  81. load();
  82. }, []);
  83. useEffect(() => {
  84. if (reload) {
  85. load();
  86. }
  87. }, [reload]);
  88. return (
  89. <div>
  90. <div style={{ display: "flex", justifyContent: "space-between" }}>
  91. <span></span>
  92. <Button
  93. type="link"
  94. shape="round"
  95. icon={<ReloadOutlined />}
  96. onClick={() => load()}
  97. />
  98. </div>
  99. <SentAdd
  100. disableChannels={channels}
  101. type={type}
  102. onSelect={(channel: IChannel) => {
  103. if (typeof user === "undefined") {
  104. return;
  105. }
  106. const newSent: ISentence = {
  107. content: "",
  108. contentType: "markdown",
  109. html: "",
  110. book: book,
  111. para: para,
  112. wordStart: wordStart,
  113. wordEnd: wordEnd,
  114. editor: {
  115. id: user.id,
  116. nickName: user.nickName,
  117. userName: user.realName,
  118. },
  119. channel: channel,
  120. translationChannels: channelsId,
  121. updateAt: "",
  122. openInEditMode: true,
  123. };
  124. setSentData((origin) => {
  125. return [newSent, ...origin];
  126. });
  127. setChannels((origin) => {
  128. if (origin) {
  129. if (!origin.includes(newSent.channel.id)) {
  130. origin.push(newSent.channel.id);
  131. return origin;
  132. }
  133. } else {
  134. return [newSent.channel.id];
  135. }
  136. });
  137. if (typeof onCreate !== "undefined") {
  138. onCreate();
  139. }
  140. }}
  141. />
  142. {sentData.map((item, id) => {
  143. return (
  144. <SentCell
  145. value={item}
  146. key={id}
  147. isPr={false}
  148. editMode={item.openInEditMode}
  149. onChange={(value: ISentence) => {
  150. console.log("onChange", value);
  151. setSentData((origin) => {
  152. origin.forEach((value1, index, array) => {
  153. if (value1.id === value.id) {
  154. array[index] = value;
  155. }
  156. });
  157. return origin;
  158. });
  159. }}
  160. />
  161. );
  162. })}
  163. </div>
  164. );
  165. };
  166. export default SentCanReadWidget;