2
0

SentWbwEdit.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { useEffect, useState } from "react";
  2. import { type IntlShape, useIntl } from "react-intl";
  3. import { Button, message } from "antd";
  4. import { EyeOutlined } from "@ant-design/icons";
  5. import { put } from "../../../request";
  6. import type {
  7. ISentenceData,
  8. ISentenceRequest,
  9. ISentenceResponse,
  10. } from "../../../api/Corpus";
  11. import type { ISentence } from "../SentEdit";
  12. import { WbwSentCtl } from "../WbwSent";
  13. import type { IWbw } from "../Wbw/WbwWord";
  14. import store from "../../../store";
  15. import { statusChange } from "../../../reducers/net-status";
  16. export const sentSave = async (
  17. sent: ISentence,
  18. intl: IntlShape
  19. ): Promise<ISentenceData> => {
  20. store.dispatch(statusChange({ status: "loading" }));
  21. const url = `/v2/sentence/${sent.book}_${sent.para}_${sent.wordStart}_${sent.wordEnd}_${sent.channel.id}`;
  22. console.info("SentWbwEdit url", url);
  23. const res = await put<ISentenceRequest, ISentenceResponse>(url, {
  24. book: sent.book,
  25. para: sent.para,
  26. wordStart: sent.wordStart,
  27. wordEnd: sent.wordEnd,
  28. channel: sent.channel.id,
  29. content: sent.content,
  30. contentType: sent.contentType,
  31. token: sessionStorage.getItem(sent.channel.id),
  32. });
  33. if (res.ok) {
  34. store.dispatch(
  35. statusChange({
  36. status: "success",
  37. message: intl.formatMessage({ id: "flashes.success" }),
  38. })
  39. );
  40. } else {
  41. message.error(res.message);
  42. store.dispatch(
  43. statusChange({
  44. status: "fail",
  45. message: res.message,
  46. })
  47. );
  48. }
  49. return res.data;
  50. };
  51. interface IWidget {
  52. data: ISentence;
  53. onSave?: Function;
  54. onClose?: Function;
  55. }
  56. const SentWbwEditWidget = ({ data, onSave, onClose }: IWidget) => {
  57. const intl = useIntl();
  58. const [wbwData, setWbwData] = useState<IWbw[]>([]);
  59. useEffect(() => {
  60. if (data.contentType === "json" && data.content) {
  61. setWbwData(JSON.parse(data.content));
  62. }
  63. }, [data.content, data.contentType]);
  64. return (
  65. <div style={{ width: "100%" }}>
  66. <WbwSentCtl
  67. book={data.book}
  68. para={data.para}
  69. wordStart={data.wordStart}
  70. wordEnd={data.wordEnd}
  71. data={wbwData}
  72. refreshable={true}
  73. display="list"
  74. layoutDirection="v"
  75. fields={{
  76. real: true,
  77. meaning: true,
  78. factors: false,
  79. factorMeaning: false,
  80. factorMeaning2: true,
  81. case: false,
  82. }}
  83. channelId={data.channel.id}
  84. channelType={data.channel.type}
  85. channelLang={data.channel.lang}
  86. onChange={async (wbwData: IWbw[]) => {
  87. const newSent = data;
  88. newSent.content = JSON.stringify(wbwData);
  89. const newData = await sentSave(newSent, intl);
  90. newSent.html = newData.html;
  91. if (typeof onSave !== "undefined") {
  92. onSave(newSent);
  93. }
  94. }}
  95. />
  96. <div>
  97. <Button
  98. size="small"
  99. type="primary"
  100. icon={<EyeOutlined />}
  101. onClick={() => {
  102. if (typeof onClose !== "undefined") {
  103. onClose();
  104. }
  105. }}
  106. >
  107. {intl.formatMessage({ id: "buttons.preview" })}
  108. </Button>
  109. </div>
  110. </div>
  111. );
  112. };
  113. export default SentWbwEditWidget;