1
1
import ProForm , {
2
- ProFormSelect ,
3
- ProFormSwitch ,
4
- ProFormText
2
+ ProFormSelect ,
3
+ ProFormSwitch ,
4
+ ProFormText
5
5
} from '@ant-design/pro-form'
6
6
import { useRequest } from 'ahooks'
7
7
import { Form , message } from 'antd'
@@ -15,115 +15,115 @@ import type { Problem, ProblemCreate, ProblemEdit } from 'utils/service'
15
15
import { ErrorCode , Horse } from 'utils/service'
16
16
17
17
export interface IProps {
18
- domainUrl : string
19
- initialValues ?: Partial < Problem >
20
- onCreateSuccess ?: ( problem : Problem ) => void
21
- onUpdateSuccess ?: ( problem : Problem ) => void
18
+ domainUrl : string
19
+ initialValues ?: Partial < Problem >
20
+ onCreateSuccess ?: ( problem : Problem ) => void
21
+ onUpdateSuccess ?: ( problem : Problem ) => void
22
22
}
23
23
24
24
export const UpsertProblemForm : React . FC < IProps > = props => {
25
- const { domainUrl, initialValues, onCreateSuccess, onUpdateSuccess } = props
26
- const { t } = useTranslation ( )
27
- const navigate = useNavigate ( )
28
- const msg = useMessage ( )
25
+ const { domainUrl, initialValues, onCreateSuccess, onUpdateSuccess } = props
26
+ const { t } = useTranslation ( )
27
+ const navigate = useNavigate ( )
28
+ const msg = useMessage ( )
29
29
30
- const languageOptions = SUPPORT_PROGRAMMING_LANGUAGE . map ( lang => ( {
31
- label : lang ,
32
- value : lang
33
- } ) )
30
+ const languageOptions = SUPPORT_PROGRAMMING_LANGUAGE . map ( lang => ( {
31
+ label : lang ,
32
+ value : lang
33
+ } ) )
34
34
35
- const { run : createProblem } = useRequest (
36
- async ( problem : ProblemCreate ) =>
37
- Horse . problem . v1CreateProblem ( domainUrl , problem ) ,
38
- {
39
- manual : true ,
40
- onSuccess : res => {
41
- if ( res . data . errorCode === ErrorCode . IntegrityError ) {
42
- message . error ( t ( 'msg.errorMsg.problemUrlNotUnique' ) )
43
- } else if ( res . data . data ?. id ) {
44
- msg . success . create ( )
45
- if ( onCreateSuccess ) onCreateSuccess ( res . data . data )
46
- navigate (
47
- `/domain/${ domainUrl } /problem/${
48
- res . data . data . url ?? res . data . data . id
49
- } `
50
- )
51
- }
52
- } ,
53
- onError : ( ) => {
54
- msg . error . create ( )
55
- }
56
- }
57
- )
35
+ const { run : createProblem } = useRequest (
36
+ async ( problem : ProblemCreate ) =>
37
+ Horse . problem . v1CreateProblem ( domainUrl , problem ) ,
38
+ {
39
+ manual : true ,
40
+ onSuccess : res => {
41
+ if ( res . data . errorCode === ErrorCode . IntegrityError ) {
42
+ message . error ( t ( 'msg.errorMsg.problemUrlNotUnique' ) )
43
+ } else if ( res . data . data ?. id ) {
44
+ msg . success . create ( )
45
+ if ( onCreateSuccess ) onCreateSuccess ( res . data . data )
46
+ navigate (
47
+ `/domain/${ domainUrl } /problem/${
48
+ res . data . data . url ?? res . data . data . id
49
+ } `
50
+ )
51
+ }
52
+ } ,
53
+ onError : ( ) => {
54
+ msg . error . create ( )
55
+ }
56
+ }
57
+ )
58
58
59
- const { run : updateProblem } = useRequest (
60
- async ( id : string , problem : ProblemEdit ) =>
61
- Horse . problem . v1UpdateProblem ( domainUrl , id , problem ) ,
62
- {
63
- manual : true ,
64
- onSuccess : res => {
65
- if ( res . data . errorCode === ErrorCode . IntegrityError ) {
66
- message . error ( t ( 'msg.errorMsg.problemUrlNotUnique' ) )
67
- } else if ( res . data . data ) {
68
- msg . success . update ( )
69
- if ( onUpdateSuccess ) onUpdateSuccess ( res . data . data )
70
- }
71
- } ,
72
- onError : ( ) => {
73
- msg . error . update ( )
74
- }
75
- }
76
- )
59
+ const { run : updateProblem } = useRequest (
60
+ async ( id : string , problem : ProblemEdit ) =>
61
+ Horse . problem . v1UpdateProblem ( domainUrl , id , problem ) ,
62
+ {
63
+ manual : true ,
64
+ onSuccess : res => {
65
+ if ( res . data . errorCode === ErrorCode . IntegrityError ) {
66
+ message . error ( t ( 'msg.errorMsg.problemUrlNotUnique' ) )
67
+ } else if ( res . data . data ) {
68
+ msg . success . update ( )
69
+ if ( onUpdateSuccess ) onUpdateSuccess ( res . data . data )
70
+ }
71
+ } ,
72
+ onError : ( ) => {
73
+ msg . error . update ( )
74
+ }
75
+ }
76
+ )
77
77
78
- const onFinish = async ( values : Partial < Problem > ) : Promise < void > =>
79
- initialValues ?. id
80
- ? updateProblem ( initialValues . id , values )
81
- : createProblem ( values as ProblemCreate )
78
+ const onFinish = async ( values : Partial < Problem > ) : Promise < void > =>
79
+ initialValues ?. id
80
+ ? updateProblem ( initialValues . id , values )
81
+ : createProblem ( values as ProblemCreate )
82
82
83
- return (
84
- < ProForm < ProblemCreate | ProblemEdit >
85
- layout = 'vertical'
86
- onFinish = { onFinish }
87
- initialValues = { initialValues }
88
- dateFormatter = 'number'
89
- omitNil
90
- >
91
- < ProForm . Group >
92
- < ProFormText
93
- width = 'lg'
94
- name = 'title'
95
- label = { t ( 'UpsertProblemForm.title' ) }
96
- rules = { [ { required : true } ] }
97
- />
98
- < ProFormSwitch
99
- name = 'hidden'
100
- label = { t ( 'UpsertProblemForm.hidden' ) }
101
- rules = { [ { required : true } ] }
102
- />
103
- </ ProForm . Group >
83
+ return (
84
+ < ProForm < ProblemCreate | ProblemEdit >
85
+ layout = 'vertical'
86
+ onFinish = { onFinish }
87
+ initialValues = { initialValues }
88
+ dateFormatter = 'number'
89
+ omitNil
90
+ >
91
+ < ProForm . Group >
92
+ < ProFormText
93
+ width = 'lg'
94
+ name = 'title'
95
+ label = { t ( 'UpsertProblemForm.title' ) }
96
+ rules = { [ { required : true } ] }
97
+ />
98
+ < ProFormSwitch
99
+ name = 'hidden'
100
+ label = { t ( 'UpsertProblemForm.hidden' ) }
101
+ rules = { [ { required : true } ] }
102
+ />
103
+ </ ProForm . Group >
104
104
105
- < ProFormText
106
- width = 'lg'
107
- name = 'url'
108
- label = { t ( 'UpsertProblemForm.url' ) }
109
- tooltip = 'The url of a problem must be unique within a domain.'
110
- />
105
+ < ProFormText
106
+ width = 'lg'
107
+ name = 'url'
108
+ label = { t ( 'UpsertProblemForm.url' ) }
109
+ tooltip = 'The url of a problem must be unique within a domain.'
110
+ />
111
111
112
- < ProFormSelect
113
- width = 'lg'
114
- name = 'languages'
115
- label = { t ( 'UpsertProblemForm.languages' ) }
116
- fieldProps = { {
117
- showArrow : true ,
118
- allowClear : true ,
119
- mode : 'multiple' ,
120
- options : languageOptions
121
- } }
122
- />
112
+ < ProFormSelect
113
+ width = 'lg'
114
+ name = 'languages'
115
+ label = { t ( 'UpsertProblemForm.languages' ) }
116
+ fieldProps = { {
117
+ showArrow : true ,
118
+ allowClear : true ,
119
+ mode : 'multiple' ,
120
+ options : languageOptions
121
+ } }
122
+ />
123
123
124
- < Form . Item name = 'content' label = { t ( 'UpsertProblemForm.content' ) } >
125
- < MarkdownEditor />
126
- </ Form . Item >
127
- </ ProForm >
128
- )
124
+ < Form . Item name = 'content' label = { t ( 'UpsertProblemForm.content' ) } >
125
+ < MarkdownEditor />
126
+ </ Form . Item >
127
+ </ ProForm >
128
+ )
129
129
}
0 commit comments