SentWbwEdit.tsx 3.2 KB

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