SentWbwEdit.tsx 3.2 KB

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