解决文章回显bug

This commit is contained in:
宇阳
2024-10-12 14:10:45 +08:00
parent 2d4e7193da
commit ab92ceb396
2 changed files with 20 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate, useSearchParams } from "react-router-dom";
import { Form, Input, Button, Select, DatePicker, Cascader, FormProps, message } from "antd"; import { Form, Input, Button, Select, DatePicker, Cascader, FormProps, message } from "antd";
import TextArea from "antd/es/input/TextArea"; import TextArea from "antd/es/input/TextArea";
@@ -27,6 +27,9 @@ interface FieldType {
} }
const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => void }) => { const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => void }) => {
const [params] = useSearchParams()
const id = +params.get('id')!
const [form] = Form.useForm() const [form] = Form.useForm()
const navigate = useNavigate() const navigate = useNavigate()
@@ -34,7 +37,7 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo
const [tagList, setTagList] = useState<Tag[]>([]) const [tagList, setTagList] = useState<Tag[]>([])
useEffect(() => { useEffect(() => {
if (!data.id) return if (!id) return form.resetFields()
// 把数据处理成[[1], [4, 5], [4, 6]]格式 // 把数据处理成[[1], [4, 5], [4, 6]]格式
const cateIds = data?.cateList?.flatMap(item => { const cateIds = data?.cateList?.flatMap(item => {
@@ -53,11 +56,11 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo
tagIds, tagIds,
createTime: dayjs(+data.createTime!) createTime: dayjs(+data.createTime!)
}) })
}, [data]) }, [data, id])
const getCateList = async () => { const getCateList = async () => {
const { data } = await getCateListAPI() const { data } = await getCateListAPI()
setCateList(data.filter(item=>item.type === "cate") as Cate[]) setCateList(data.filter(item => item.type === "cate") as Cate[])
} }
const getTagList = async () => { const getTagList = async () => {
@@ -76,16 +79,19 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo
}; };
const onSubmit: FormProps<FieldType>['onFinish'] = async (values) => { const onSubmit: FormProps<FieldType>['onFinish'] = async (values) => {
console.log(values, 777);
values.createTime = values.createTime.valueOf() values.createTime = values.createTime.valueOf()
values.cateIds = [...new Set(values.cateIds?.flat())] values.cateIds = [...new Set(values.cateIds?.flat())]
values.tagIds = values.tagIds ? (values.tagIds as number[]).join(',') : "" values.tagIds = values.tagIds ? (values.tagIds as number[]).join(',') : ""
if (data.id) { if (id) {
await editArticleDataAPI({ id: data.id, ...values, content: data.content } as any) await editArticleDataAPI({ id, ...values, content: data.content } as any)
message.success("🎉 编辑成功") message.success("🎉 编辑成功")
} else { } else {
await addArticleDataAPI({ id: data.id, ...values, content: data.content } as any) await addArticleDataAPI({ id, ...values, content: data.content } as any)
message.success("🎉 发布成功") message.success("🎉 发布成功")
} }
@@ -137,9 +143,10 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo
<Form.Item label="选择标签" name="tagIds"> <Form.Item label="选择标签" name="tagIds">
<Select <Select
allowClear allowClear
mode="multiple" mode="tags"
options={tagList} options={tagList}
fieldNames={{ label: 'name', value: 'id' }} fieldNames={{ label: 'name', value: 'id' }}
filterOption={(input, option) => !!option?.name.includes(input)}
placeholder="请选择文章标签" placeholder="请选择文章标签"
className="w-full" className="w-full"
/> />
@@ -150,7 +157,7 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo
</Form.Item> </Form.Item>
<Form.Item> <Form.Item>
<Button type="primary" htmlType="submit" className="w-full">{data.id ? "编辑文章" : "发布文章"}</Button> <Button type="primary" htmlType="submit" className="w-full">{id ? "编辑文章" : "发布文章"}</Button>
</Form.Item> </Form.Item>
</Form> </Form>
</> </>

View File

@@ -50,6 +50,8 @@ const CreatePage = () => {
// 回显数据 // 回显数据
useEffect(() => { useEffect(() => {
setPublishOpen(false)
// 有Id就回显指定的数据 // 有Id就回显指定的数据
if (id) { if (id) {
getArticleData() getArticleData()
@@ -152,8 +154,7 @@ const CreatePage = () => {
<Title value="创作" /> <Title value="创作" />
<Card className='relative mt-2'> <Card className='relative mt-2'>
{/* <div className='absolute top-[4.5%] right-[5%] z-10 flex space-x-4'> */} <div className='relative z-50 flex justify-end w-full'>
<div className='flex justify-end w-full'>
<div className='flex w-[24%] space-x-4'> <div className='flex w-[24%] space-x-4'>
<Dropdown.Button menu={{ items }}></Dropdown.Button> <Dropdown.Button menu={{ items }}></Dropdown.Button>
@@ -172,7 +173,7 @@ const CreatePage = () => {
</div> </div>
<Drawer <Drawer
title={data.id ? "编辑文章" : "发布文章"} title={id ? "编辑文章" : "发布文章"}
placement="right" placement="right"
size='large' size='large'
closable={false} closable={false}