SentCanRead.tsx 4.9 KB

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