1
1
import React from 'react' ;
2
- import { useModel , useHistory } from 'umi' ;
2
+ import { useModel , useHistory , useParams , useRequest } from 'umi' ;
3
3
import { Form , Input , Select , Button , Space } from 'antd' ;
4
4
import { TABS_MAP } from '@/constants' ;
5
5
@@ -8,24 +8,59 @@ import Markdown from '@/component/Markdown';
8
8
import * as API from '@/service/topic' ;
9
9
import * as styles from './index.less' ;
10
10
11
- const CreateTopic : React . FC < Props > = ( props ) => {
11
+ const TopicEditPage : React . FC < Props > = ( props ) => {
12
12
const history = useHistory ( ) ;
13
13
const [ form ] = Form . useForm ( ) ;
14
14
const { initialState } = useModel ( '@@initialState' ) ;
15
+ const { user } = useModel ( 'user' ) ;
15
16
16
17
const token = initialState ?. token ;
17
18
19
+ const { id } = useParams < { id ?: string } > ( ) ;
20
+
21
+ useRequest (
22
+ async ( ) => {
23
+ if ( ! id ) return ;
24
+ const { data } = await API . readTopic ( {
25
+ id,
26
+ mdrender : false ,
27
+ } ) ;
28
+
29
+ if ( data . author_id !== user ?. id ) {
30
+ history . push ( location . pathname . replace ( / \/ e d i t $ / , '' ) ) ;
31
+ return ;
32
+ }
33
+
34
+ form . setFieldsValue ( {
35
+ title : data . title ,
36
+ content : data . content ,
37
+ tab : data . tab ,
38
+ } ) ;
39
+ } ,
40
+ {
41
+ ready : ! ! id ,
42
+ } ,
43
+ ) ;
44
+
18
45
const onFinish = async ( values : any ) => {
19
46
console . debug ( '===create.values' , values ) ;
20
47
21
48
if ( ! token ) {
22
49
return ;
23
50
}
24
51
25
- await API . postTopic ( {
26
- ...values ,
27
- accesstoken : token ,
28
- } ) ;
52
+ if ( id ) {
53
+ await API . updateTopic ( {
54
+ topic_id : id ,
55
+ ...values ,
56
+ accesstoken : token ,
57
+ } ) ;
58
+ } else {
59
+ await API . createTopic ( {
60
+ ...values ,
61
+ accesstoken : token ,
62
+ } ) ;
63
+ }
29
64
30
65
onReset ( ) ;
31
66
@@ -88,6 +123,6 @@ const CreateTopic: React.FC<Props> = (props) => {
88
123
) ;
89
124
} ;
90
125
91
- export default CreateTopic ;
126
+ export default TopicEditPage ;
92
127
93
128
interface Props { }
0 commit comments