| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import { useEffect, useState } from "react";
- import { type IntlShape, useIntl } from "react-intl";
- import { Button, message } from "antd";
- import { EyeOutlined } from "@ant-design/icons";
- import { put } from "../../../request";
- import type {
- ISentenceData,
- ISentenceRequest,
- ISentenceResponse,
- } from "../../../api/Corpus";
- import type { ISentence } from "../SentEdit";
- import { WbwSentCtl } from "../WbwSent";
- import type { IWbw } from "../Wbw/WbwWord";
- import store from "../../../store";
- import { statusChange } from "../../../reducers/net-status";
- export const sentSave = async (
- sent: ISentence,
- intl: IntlShape
- ): Promise<ISentenceData> => {
- store.dispatch(statusChange({ status: "loading" }));
- const url = `/v2/sentence/${sent.book}_${sent.para}_${sent.wordStart}_${sent.wordEnd}_${sent.channel.id}`;
- console.info("SentWbwEdit url", url);
- const res = await put<ISentenceRequest, ISentenceResponse>(url, {
- book: sent.book,
- para: sent.para,
- wordStart: sent.wordStart,
- wordEnd: sent.wordEnd,
- channel: sent.channel.id,
- content: sent.content,
- contentType: sent.contentType,
- token: sessionStorage.getItem(sent.channel.id),
- });
- if (res.ok) {
- store.dispatch(
- statusChange({
- status: "success",
- message: intl.formatMessage({ id: "flashes.success" }),
- })
- );
- } else {
- message.error(res.message);
- store.dispatch(
- statusChange({
- status: "fail",
- message: res.message,
- })
- );
- }
- return res.data;
- };
- interface IWidget {
- data: ISentence;
- onSave?: Function;
- onClose?: Function;
- }
- const SentWbwEditWidget = ({ data, onSave, onClose }: IWidget) => {
- const intl = useIntl();
- const [wbwData, setWbwData] = useState<IWbw[]>([]);
- useEffect(() => {
- if (data.contentType === "json" && data.content) {
- setWbwData(JSON.parse(data.content));
- }
- }, [data.content, data.contentType]);
- return (
- <div style={{ width: "100%" }}>
- <WbwSentCtl
- book={data.book}
- para={data.para}
- wordStart={data.wordStart}
- wordEnd={data.wordEnd}
- data={wbwData}
- refreshable={true}
- display="list"
- layoutDirection="v"
- fields={{
- real: true,
- meaning: true,
- factors: false,
- factorMeaning: false,
- factorMeaning2: true,
- case: false,
- }}
- channelId={data.channel.id}
- channelType={data.channel.type}
- channelLang={data.channel.lang}
- onChange={async (wbwData: IWbw[]) => {
- const newSent = data;
- newSent.content = JSON.stringify(wbwData);
- const newData = await sentSave(newSent, intl);
- newSent.html = newData.html;
- if (typeof onSave !== "undefined") {
- onSave(newSent);
- }
- }}
- />
- <div>
- <Button
- size="small"
- type="primary"
- icon={<EyeOutlined />}
- onClick={() => {
- if (typeof onClose !== "undefined") {
- onClose();
- }
- }}
- >
- {intl.formatMessage({ id: "buttons.preview" })}
- </Button>
- </div>
- </div>
- );
- };
- export default SentWbwEditWidget;
|