| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- import { useEffect, useState } from "react";
- import { useIntl } from "react-intl";
- import { Button, message, Typography } from "antd";
- import { SaveOutlined } from "@ant-design/icons";
- import { post, put } from "../../../request";
- import type {
- ISentencePrRequest,
- ISentencePrResponse,
- ISentenceRequest,
- ISentenceResponse,
- } from "../../../api/Corpus";
- import { type ISentence, toISentence } from "../SentEdit";
- import TermTextArea from "../../general/TermTextArea";
- import { useAppSelector } from "../../../hooks";
- import { wordList } from "../../../reducers/sent-word";
- import Builder from "../Builder/Builder";
- const { Text } = Typography;
- export const sentSave = (
- data: ISentence,
- ok: (res: ISentence) => void,
- finish: () => void
- ) => {
- let url = `/v2/sentence/${data.book}_${data.para}_${data.wordStart}_${data.wordEnd}_${data.channel.id}`;
- url += "?mode=edit&html=true";
- const body = {
- book: data.book,
- para: data.para,
- wordStart: data.wordStart,
- wordEnd: data.wordEnd,
- channel: data.channel.id,
- content: data.content,
- channels: data.translationChannels?.join(),
- token: sessionStorage.getItem(data.channel.id),
- };
- console.log("save url", url, body);
- put<ISentenceRequest, ISentenceResponse>(url, body)
- .then((json) => {
- if (json.ok) {
- console.debug("sent save ok", json.data);
- const newData: ISentence = toISentence(json.data);
- ok(newData);
- } else {
- message.error(json.message);
- }
- })
- .finally(() => {
- finish();
- })
- .catch((e) => {
- console.error("catch", e);
- message.error(e.message);
- });
- };
- interface IWidget {
- data: ISentence;
- isPr?: boolean;
- isCreatePr?: boolean;
- onSave?: Function;
- onClose?: Function;
- onCreate?: Function;
- }
- const SentCellEditableWidget = ({
- data,
- onSave,
- onClose,
- onCreate,
- isPr = false,
- isCreatePr = false,
- }: IWidget) => {
- const intl = useIntl();
- const [value, setValue] = useState(data.content);
- const [saving, setSaving] = useState<boolean>(false);
- const [termList, setTermList] = useState<string[]>();
- const sentWords = useAppSelector(wordList);
- useEffect(() => {
- const sentId = `${data.book}-${data.para}-${data.wordStart}-${data.wordEnd}`;
- setTermList(sentWords.find((value) => value.sentId === sentId)?.words);
- }, [data.book, data.para, data.wordEnd, data.wordStart, sentWords]);
- const savePr = () => {
- setSaving(true);
- if (!value) {
- return;
- }
- if (isCreatePr) {
- post<ISentencePrRequest, ISentencePrResponse>(`/v2/sentpr`, {
- book: data.book,
- para: data.para,
- begin: data.wordStart,
- end: data.wordEnd,
- channel: data.channel.id,
- text: value,
- })
- .then((json) => {
- setSaving(false);
- if (json.ok) {
- message.success(intl.formatMessage({ id: "flashes.success" }));
- if (typeof onCreate !== "undefined") {
- onCreate();
- }
- } else {
- message.error(json.message);
- }
- })
- .catch((e) => {
- setSaving(false);
- console.error("catch", e);
- message.error(e.message);
- });
- } else {
- const url = `/v2/sentpr/${data.id}`;
- console.log("url", url);
- put<ISentencePrRequest, ISentencePrResponse>(url, {
- text: value,
- })
- .then((json) => {
- if (json.ok) {
- message.success(intl.formatMessage({ id: "flashes.success" }));
- if (typeof onSave !== "undefined") {
- onSave();
- }
- } else {
- message.error(json.message);
- }
- })
- .finally(() => {
- setSaving(false);
- })
- .catch((e) => {
- console.error("catch", e);
- message.error(e.message);
- });
- }
- };
- const save = () => {
- setSaving(true);
- let url = `/v2/sentence/${data.book}_${data.para}_${data.wordStart}_${data.wordEnd}_${data.channel.id}`;
- url += "?mode=edit&html=true";
- const body = {
- book: data.book,
- para: data.para,
- wordStart: data.wordStart,
- wordEnd: data.wordEnd,
- channel: data.channel.id,
- content: value,
- channels: data.translationChannels?.join(),
- token: sessionStorage.getItem(data.channel.id),
- };
- console.debug("save url", url, body);
- put<ISentenceRequest, ISentenceResponse>(url, body)
- .then((json) => {
- if (json.ok) {
- message.success(intl.formatMessage({ id: "flashes.success" }));
- if (typeof onSave !== "undefined") {
- const newData: ISentence = toISentence(json.data);
- onSave(newData);
- }
- } else {
- message.error(json.message);
- }
- })
- .finally(() => {
- setSaving(false);
- })
- .catch((e) => {
- console.error("catch", e);
- message.error(e.message);
- });
- };
- return (
- <Typography.Paragraph style={{ width: "100%" }}>
- <TermTextArea
- value={value ? value : ""}
- menuOptions={termList}
- onChange={(value: string) => {
- setValue(value);
- }}
- placeholder={intl.formatMessage({
- id: "labels.input",
- })}
- onClose={() => {
- if (typeof onClose !== "undefined") {
- onClose();
- }
- }}
- onSave={(value: string) => {
- setValue(value);
- isPr ? savePr() : save();
- }}
- />
- <div style={{ display: "flex", justifyContent: "space-between" }}>
- <div>
- <span>
- <Text keyboard>esc</Text>=
- <Button
- size="small"
- type="link"
- onClick={(_e) => {
- if (typeof onClose !== "undefined") {
- onClose();
- }
- }}
- >
- {intl.formatMessage({ id: "buttons.cancel" })}
- </Button>
- </span>
- <span>
- <Text keyboard>enter</Text>=
- <Button size="small" type="link">
- new line
- </Button>
- </span>
- <Text keyboard style={{ cursor: "pointer" }}>
- <Builder trigger={"<t>"} />
- </Text>
- </div>
- <div>
- <Text keyboard>Ctrl/⌘</Text>➕<Text keyboard>enter</Text>=
- <Button
- size="small"
- type="primary"
- icon={<SaveOutlined />}
- loading={saving}
- onClick={() => (isPr ? savePr() : save())}
- >
- {intl.formatMessage({ id: "buttons.save" })}
- </Button>
- </div>
- </div>
- </Typography.Paragraph>
- );
- };
- export default SentCellEditableWidget;
|