| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import { useIntl } from "react-intl";
- import {
- ProForm,
- type ProFormInstance,
- ProFormSelect,
- ProFormText,
- } from "@ant-design/pro-components";
- import { Alert, Space, message } from "antd";
- import { get, post } from "../../request";
- import type {
- IAnthologyListResponse,
- IArticleCreateRequest,
- IArticleDataResponse,
- IArticleResponse,
- } from "../../api/Article";
- import LangSelect from "../general/LangSelect";
- import { useEffect, useRef, useState } from "react";
- interface IFormData {
- title: string;
- lang: string;
- studio: string;
- anthologyId?: string;
- parentId?: string;
- }
- interface IWidget {
- studio?: string;
- anthologyId?: string;
- parentId?: string | null;
- compact?: boolean;
- onSuccess?: (data: IArticleDataResponse) => void;
- }
- const ArticleCreateWidget = ({
- studio,
- parentId,
- compact = true,
- onSuccess,
- }: IWidget) => {
- const intl = useIntl();
- const formRef = useRef<ProFormInstance | undefined>(undefined);
- const [parent, setParent] = useState<IArticleDataResponse>();
- console.log("parentId", parentId);
- useEffect(() => {
- if (parentId) {
- get<IArticleResponse>(`/v2/article/${parentId}`).then((json) => {
- console.log("article", json);
- if (json.ok) {
- setParent(json.data);
- }
- });
- }
- }, []);
- return (
- <Space orientation="vertical">
- {parentId ? (
- <Alert
- title={`从文章 ${parent?.title} 创建子文章`}
- type="info"
- closable
- />
- ) : undefined}
- <ProForm<IFormData>
- formRef={formRef}
- onFinish={async (values: IFormData) => {
- console.log(values);
- if (typeof studio === "undefined") {
- return;
- }
- values.studio = studio;
- values.parentId = parentId ? parentId : undefined;
- const res = await post<IArticleCreateRequest, IArticleResponse>(
- `/v2/article`,
- values
- );
- console.log(res);
- if (res.ok) {
- message.success(intl.formatMessage({ id: "flashes.success" }));
- if (typeof onSuccess !== "undefined") {
- onSuccess(res.data);
- formRef.current?.resetFields(["title"]);
- }
- } else {
- message.error(res.message);
- }
- }}
- >
- <ProForm.Group>
- <ProFormText
- width="md"
- name="title"
- required
- label={intl.formatMessage({ id: "channel.name" })}
- rules={[
- {
- required: true,
- message: intl.formatMessage({
- id: "channel.create.message.noname",
- }),
- },
- ]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <LangSelect />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormSelect
- name={"anthologyId"}
- label={"加入文集"}
- hidden={compact}
- width={"md"}
- showSearch
- debounceTime={300}
- request={async ({ keyWords }) => {
- console.log("keyWord", keyWords);
- let url = `/v2/anthology?view=studio&view2=my&name=${studio}`;
- url += keyWords ? "&search=" + keyWords : "";
- const res = await get<IAnthologyListResponse>(url);
- const result = res.data.rows.map((item) => {
- return {
- value: item.uid,
- label: item.title,
- };
- });
- console.log("json", result);
- return result;
- }}
- />
- </ProForm.Group>
- </ProForm>
- </Space>
- );
- };
- export default ArticleCreateWidget;
|