@@ -2,10 +2,30 @@ import { useEffect, useState } from "react";
22import Vditor from "vditor" ;
33import style from "./index.module.less" ;
44import "vditor/dist/index.css" ;
5- import { Button } from "antd" ;
5+ import { post } from "@/utils/request" ;
6+ import {
7+ Button ,
8+ Modal ,
9+ Form ,
10+ Input ,
11+ Select ,
12+ List ,
13+ Typography ,
14+ message ,
15+ } from "antd" ;
16+ import axios from "axios" ;
17+ import { articleTypes } from "@/constants/commonTypes" ;
18+ import { useNavigate } from "react-router-dom" ;
619
7- const App = ( ) => {
20+ const { Option } = Select ;
21+ const { Title } = Typography ;
22+
23+ const Write = ( ) => {
824 const [ vd , setVd ] = useState < Vditor > ( ) ;
25+ const [ isModalVisible , setIsModalVisible ] = useState ( false ) ;
26+ const navigate = useNavigate ( ) ;
27+ const [ form ] = Form . useForm ( ) ;
28+
929 useEffect ( ( ) => {
1030 const vditor = new Vditor ( "vditor" , {
1131 mode : "sv" ,
@@ -14,20 +34,95 @@ const App = () => {
1434 setVd ( vditor ) ;
1535 } ,
1636 } ) ;
37+
1738 // Clear the effect
1839 return ( ) => {
1940 vd ?. destroy ( ) ;
2041 setVd ( undefined ) ;
2142 } ;
2243 } , [ ] ) ;
44+
45+ const handlePublish = ( ) => {
46+ setIsModalVisible ( true ) ;
47+ } ;
48+
49+ // 定义 ValidationError 类型
50+
51+ const handleOk = async ( ) => {
52+ // 验证表单字段,如果有错误会抛出异常
53+ await form . validateFields ( ) ;
54+
55+ // 获取所有字段的值
56+ const values = form . getFieldsValue ( ) ;
57+ // 获取富文本编辑器的内容
58+ const content = vd ?. getValue ( ) ;
59+
60+ if ( ! content ) {
61+ return ;
62+ }
63+
64+ // 发送请求
65+ const response : any = await post ( "/user/write" , {
66+ category : values . category ,
67+ title : values . title ,
68+ content : content ,
69+ } ) ;
70+
71+ // 根据响应显示成功或错误模态框
72+ if ( response ?. success ) {
73+ message . success ( "文章已成功发布" ) ;
74+ // 成功后可以关闭弹窗并重置表单
75+ setIsModalVisible ( false ) ;
76+ form . resetFields ( ) ;
77+ setTimeout ( ( ) => {
78+ navigate ( "/" ) ;
79+ } , 0 ) ;
80+ }
81+ } ;
82+
83+ const handleCancel = ( ) => {
84+ setIsModalVisible ( false ) ;
85+ form . resetFields ( ) ;
86+ } ;
87+
2388 return (
2489 < div className = { style . writeWP } >
2590 < div className = { style . operaBtns } >
26- < Button type = "primary" > 发布</ Button >
91+ < Button type = "primary" onClick = { handlePublish } >
92+ 发布
93+ </ Button >
2794 </ div >
2895 < div id = "vditor" className = "vditor" />
96+ < Modal
97+ title = "发布文章"
98+ open = { isModalVisible }
99+ onOk = { handleOk }
100+ onCancel = { handleCancel }
101+ okText = "确定"
102+ cancelText = "取消"
103+ >
104+ < Form form = { form } layout = "vertical" >
105+ < Form . Item
106+ name = "category"
107+ label = "文章分类"
108+ rules = { [ { required : true , message : "请选择文章分类" } ] }
109+ >
110+ < Select
111+ placeholder = "请选择文章分类"
112+ options = { articleTypes }
113+ > </ Select >
114+ </ Form . Item >
115+ < Form . Item
116+ name = "title"
117+ label = "文章标题"
118+ rules = { [ { required : true , message : "请输入文章标题" } ] }
119+ >
120+ < Input placeholder = "请输入文章标题" />
121+ </ Form . Item >
122+ </ Form >
123+ </ Modal >
29124 </ div >
30125 ) ;
31126} ;
32127
33- export default App ;
128+ export default Write ;
0 commit comments