import { useIntl } from "react-intl"; import { ProForm, ProFormCheckbox, ProFormDependency, type ProFormInstance, ProFormSelect, ProFormSwitch, ProFormText, } from "@ant-design/pro-components"; import LangSelect from "../general/LangSelect"; import ChannelSelect from "../channel/ChannelSelect"; import { Alert, AutoComplete, Button, Form, Input, message, Space, Tag, } from "antd"; import { useEffect, useRef, useState } from "react"; import type { ITermCreateResponse, ITermDataRequest, ITermListResponse, ITermResponse, } from "../../api/Term"; import { get, post, put } from "../../request"; import MDEditor from "@uiw/react-md-editor"; import { useAppSelector } from "../../hooks"; import { currentUser as _currentUser } from "../../reducers/current-user"; import store from "../../store"; import { push } from "../../reducers/term-vocabulary"; interface ValueType { key?: string; label: React.ReactNode; value: string | number; } interface IWidget { id?: string; word?: string; tags?: string[]; studioName?: string; channelId?: string; parentChannelId?: string; parentStudioId?: string; community?: boolean; onUpdate?: Function; } const TermEditWidget = ({ id, word, tags, channelId, studioName, parentChannelId, parentStudioId, community = false, onUpdate, }: IWidget) => { const intl = useIntl(); const [meaningOptions, setMeaningOptions] = useState([]); const [readonly, setReadonly] = useState(false); const [isSaveAs, setIsSaveAs] = useState(false); const [currChannel, setCurrChannel] = useState([]); const user = useAppSelector(_currentUser); const [form] = Form.useForm(); const formRef = useRef(undefined); useEffect(() => { if (word) { const url = `/v2/terms?view=word&word=${word}`; console.info("api request", url); get(url).then((json) => { const meaning = json.data.rows.map((item) => item.meaning); const meaningMap = new Map(); for (const it of meaning) { const count = meaningMap.get(it); if (typeof count === "undefined") { meaningMap.set(it, 1); } else { meaningMap.set(it, count + 1); } } const meaningList: ValueType[] = []; meaningMap.forEach((value, key, _map) => { meaningList.push({ value: key, label: ( {key} {value} ), }); }); setMeaningOptions(meaningList); }); } }, [word]); let channelDisable = false; if (community) { channelDisable = true; } if (readonly) { channelDisable = true; } if (id) { channelDisable = true; } return ( <> {community ? ( 详情 } /> ) : readonly ? ( 详情 } /> ) : undefined} form={form} formRef={formRef} autoFocusFirstInput={true} onFinish={async (values: ITerm) => { console.log("term submit", values); if ( typeof values.word === "undefined" || typeof values.meaning === "undefined" ) { return; } let copy_channel = ""; if (values.copy_channel && values.copy_channel.length > 0) { copy_channel = values.copy_channel[values.copy_channel.length - 1]; } const newValue = { id: values.id, word: values.word, tag: values.tag, meaning: values.meaning, other_meaning: values.meaning2?.join(), note: values.note, channel: values.save_as ? copy_channel : values.channelId, parent_channel_id: parentChannelId, studioName: studioName, studioId: parentStudioId, language: values.save_as ? values.copy_lang : values.lang, pr: values.save_as ? values.pr : undefined, }; console.log("value", newValue); let res: ITermResponse; if (typeof values.id === "undefined" || community || values.save_as) { const url = `/v2/terms?community_summary=1`; console.info("api request", url, newValue); res = await post(url, newValue); } else { const url = `/v2/terms/${values.id}?community_summary=1`; console.info("api request", url, newValue); res = await put(url, newValue); } console.debug("api response", res); if (res.ok) { message.success("提交成功"); store.dispatch( push({ word: res.data.word, meaning: res.data.meaning }) ); if (typeof onUpdate !== "undefined") { onUpdate(res.data); } } else { message.error(res.message); } return true; }} request={async () => { let url: string; let data: ITerm = { word: word ? word : "", tag: tags?.join(), meaning: "", meaning2: [], note: "", lang: "", copy_channel: [], }; if (typeof id !== "undefined") { // 如果是编辑,就从服务器拉取数据。 url = "/v2/terms/" + id; console.info("TermEdit is edit api request", url); const res = await get(url); console.debug("TermEdit is edit api response", res); if (res.ok) { let meaning2: string[] = []; if (res.data.other_meaning) { meaning2 = res.data.other_meaning.split(","); } let realChannelId: string | undefined = ""; if (parentStudioId) { if (user?.id === parentStudioId) { if (community) { realChannelId = ""; } else { realChannelId = res.data.channel?.id; } } else { realChannelId = parentChannelId; } } else { if (res.data.channel) { realChannelId = res.data.channel?.id; setCurrChannel([ { label: res.data.channel?.name, value: res.data.channel?.id, }, ]); } } let copyToChannel: string[] = []; if (parentChannelId) { if (user?.roles?.includes("basic")) { copyToChannel = [parentChannelId]; } else { copyToChannel = [""]; } } data = { id: res.data.guid, word: res.data.word, tag: res.data.tag, meaning: res.data.meaning, meaning2: meaning2, note: res.data.note ? res.data.note : "", lang: res.data.language, channelId: realChannelId, copy_channel: copyToChannel, }; if (res.data.role === "reader" || res.data.role === "unknown") { setReadonly(true); } } } else if (typeof parentChannelId !== "undefined") { /** * 在channel新建 * basic:仅保存在这个版本 * pro: 默认studio通用 */ url = `/v2/terms?view=create-by-channel&channel=${parentChannelId}&word=${word}`; console.info("api request 在channel新建", url); const res = await get(url); console.debug("api response", res); let channelId = ""; let copyToChannel: string[] = []; if (user?.roles?.includes("basic")) { channelId = parentChannelId; copyToChannel = [parentChannelId]; } else { channelId = user?.id === parentStudioId ? "" : parentChannelId; copyToChannel = [res.data.studio.id, parentChannelId]; } data = { word: word ? word : "", tag: tags?.join(), meaning: "", meaning2: [], note: "", lang: res.data.language, channelId: channelId, copy_channel: copyToChannel, }; } else if (typeof studioName !== "undefined") { //在studio新建 url = `/v2/terms?view=create-by-studio&studio=${studioName}&word=${word}`; console.debug("在 studio 新建", url); } return data; }} > {}} maxLength={128} > {({ channelId }) => { const hasChannel = channelId ? channelId === "" ? false : true : false; return ( ); }} ) => { setIsSaveAs(checked); }, }} /> {({ copy_channel }) => { const hasChannel = copy_channel ? copy_channel.length === 0 || copy_channel[0] === "" ? false : true : false; return ( ); }} 同时提交修改建议 ); }; export default TermEditWidget;