11import React from 'react' ;
2- import { useModel , useHistory } from 'umi' ;
2+ import { useModel , useHistory , useParams , useRequest } from 'umi' ;
33import { Form , Input , Select , Button , Space } from 'antd' ;
44import { TABS_MAP } from '@/constants' ;
55
@@ -8,24 +8,59 @@ import Markdown from '@/component/Markdown';
88import * as API from '@/service/topic' ;
99import * as styles from './index.less' ;
1010
11- const CreateTopic : React . FC < Props > = ( props ) => {
11+ const TopicEditPage : React . FC < Props > = ( props ) => {
1212 const history = useHistory ( ) ;
1313 const [ form ] = Form . useForm ( ) ;
1414 const { initialState } = useModel ( '@@initialState' ) ;
15+ const { user } = useModel ( 'user' ) ;
1516
1617 const token = initialState ?. token ;
1718
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+
1845 const onFinish = async ( values : any ) => {
1946 console . debug ( '===create.values' , values ) ;
2047
2148 if ( ! token ) {
2249 return ;
2350 }
2451
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+ }
2964
3065 onReset ( ) ;
3166
@@ -88,6 +123,6 @@ const CreateTopic: React.FC<Props> = (props) => {
88123 ) ;
89124} ;
90125
91- export default CreateTopic ;
126+ export default TopicEditPage ;
92127
93128interface Props { }
0 commit comments