1
+ import React from 'react' ;
2
+ import PropTypes from 'prop-types' ;
1
3
import { reduxActions } from '@codetanzania/ewea-api-states' ;
2
4
import { httpActions } from '@codetanzania/ewea-api-client' ;
3
- import { Form } from '@ant-design/compatible' ;
4
- import '@ant-design/compatible/assets/index.css' ;
5
- import { Button , Input } from 'antd' ;
6
- import PropTypes from 'prop-types' ;
7
- import React , { Component } from 'react' ;
5
+ import { Button , Form , Input } from 'antd' ;
6
+ import get from 'lodash/get' ;
7
+
8
8
import SearchableSelectInput from '../../../components/SearchableSelectInput' ;
9
9
import { notifyError , notifySuccess } from '../../../util' ;
10
10
11
- /* constants */
11
+ /* http actions */
12
12
const { getEventGroups } = httpActions ;
13
+ /* redux actions */
13
14
const { putEventType, postEventType } = reduxActions ;
15
+ /* constants */
14
16
const { TextArea } = Input ;
17
+ const formItemLayout = {
18
+ labelCol : {
19
+ xs : { span : 24 } ,
20
+ sm : { span : 24 } ,
21
+ md : { span : 24 } ,
22
+ lg : { span : 24 } ,
23
+ xl : { span : 24 } ,
24
+ xxl : { span : 24 } ,
25
+ } ,
26
+ wrapperCol : {
27
+ xs : { span : 24 } ,
28
+ sm : { span : 24 } ,
29
+ md : { span : 24 } ,
30
+ lg : { span : 24 } ,
31
+ xl : { span : 24 } ,
32
+ xxl : { span : 24 } ,
33
+ } ,
34
+ } ;
15
35
16
36
/**
17
- * @class
37
+ * @function
18
38
* @name EventTypeForm
19
- * @description Render form for creating a new event type
20
- *
21
- * @version 0.1.0
39
+ * @description Form for creating and editing event type
40
+ * @param {object } props Form properties object
41
+ * @param {object|null } props.eventType Event Type object to be edited if null it will be created
42
+ * @param {boolean } props.posting Boolean flag for showing spinner while sending data to the api
43
+ * @param {Function } props.onCancel Callback for closing form
44
+ * @returns {object } Render Event Type form
45
+ * @version 0.2.0
22
46
* @since 0.1.0
23
47
*/
24
- class EventTypeForm extends Component {
25
- /**
26
- * @function
27
- * @name handleSubmit
28
- * @description call back function to handle submit action
29
- *
30
- * @param {object } e event object
31
- *
32
- * @returns {undefined } does not return anything
33
- *
34
- * @version 0.1.0
35
- * @since 0.1.0
36
- */
37
- handleSubmit = ( e ) => {
38
- e . preventDefault ( ) ;
39
- const {
40
- form : { validateFieldsAndScroll } ,
41
- eventType,
42
- isEditForm,
43
- } = this . props ;
44
-
45
- validateFieldsAndScroll ( ( error , values ) => {
46
- if ( ! error ) {
47
- const payload = {
48
- strings : {
49
- name : {
50
- en : values . name ,
51
- } ,
52
- description : {
53
- en : values . description ,
54
- } ,
55
- } ,
56
- relations : {
57
- group : { _id : values . group } ,
58
- } ,
59
- } ;
60
- if ( isEditForm ) {
61
- const updatedContact = { ...eventType , ...payload } ;
62
- putEventType (
63
- updatedContact ,
64
- ( ) => {
65
- notifySuccess ( 'Event Type was updated successfully' ) ;
66
- } ,
67
- ( ) => {
68
- notifyError (
69
- 'Something occurred while updating Event Type, please try again!'
70
- ) ;
71
- }
72
- ) ;
73
- } else {
74
- postEventType (
75
- payload ,
76
- ( ) => {
77
- notifySuccess ( 'Event Type was created successfully' ) ;
78
- } ,
79
- ( ) => {
80
- notifyError (
81
- 'Something occurred while saving Event Type, please try again!'
82
- ) ;
83
- }
48
+ const EventTypeForm = ( { eventType, posting, onCancel } ) => {
49
+ const onFinish = ( values ) => {
50
+ if ( get ( eventType , '_id' ) ) {
51
+ const updatedContact = { ...eventType , ...values } ;
52
+ putEventType (
53
+ updatedContact ,
54
+ ( ) => {
55
+ notifySuccess ( 'Event Type was updated successfully' ) ;
56
+ } ,
57
+ ( ) => {
58
+ notifyError (
59
+ 'Something occurred while updating Event Type, please try again!'
84
60
) ;
85
61
}
86
- }
87
- } ) ;
88
- } ;
89
-
90
- render ( ) {
91
- const {
92
- posting,
93
- onCancel,
94
- isEditForm,
95
- eventType,
96
- form : { getFieldDecorator } ,
97
- } = this . props ;
62
+ ) ;
63
+ return ;
64
+ }
98
65
99
- const formItemLayout = {
100
- labelCol : {
101
- xs : { span : 24 } ,
102
- sm : { span : 24 } ,
103
- md : { span : 24 } ,
104
- lg : { span : 24 } ,
105
- xl : { span : 24 } ,
106
- xxl : { span : 24 } ,
66
+ postEventType (
67
+ values ,
68
+ ( ) => {
69
+ notifySuccess ( 'Event Type was created successfully' ) ;
107
70
} ,
108
- wrapperCol : {
109
- xs : { span : 24 } ,
110
- sm : { span : 24 } ,
111
- md : { span : 24 } ,
112
- lg : { span : 24 } ,
113
- xl : { span : 24 } ,
114
- xxl : { span : 24 } ,
115
- } ,
116
- } ;
71
+ ( ) => {
72
+ notifyError (
73
+ 'Something occurred while saving Event Type, please try again!'
74
+ ) ;
75
+ }
76
+ ) ;
77
+ } ;
117
78
118
- return (
119
- < Form onSubmit = { this . handleSubmit } autoComplete = "off" >
120
- { /* Event Type name */ }
121
- { /* eslint-disable-next-line react/jsx-props-no-spreading */ }
122
- < Form . Item { ...formItemLayout } label = "Name" >
123
- { getFieldDecorator ( 'name' , {
124
- initialValue : isEditForm ? eventType . strings . name . en : undefined ,
125
- rules : [
126
- {
127
- required : true ,
128
- message : ' Event Types organization name is required' ,
129
- } ,
130
- ] ,
131
- } ) ( < Input /> ) }
132
- </ Form . Item >
133
- { /* end Event Type name */ }
79
+ return (
80
+ < Form
81
+ { ...formItemLayout } // eslint-disable-line
82
+ onFinish = { onFinish }
83
+ autoComplete = "off"
84
+ initialValues = { {
85
+ ...eventType ,
86
+ relations : {
87
+ group : get ( eventType , 'relations.group._id' ) ,
88
+ } ,
89
+ } }
90
+ >
91
+ < Form . Item
92
+ name = { [ 'strings' , 'name' , 'en' ] }
93
+ label = "Name"
94
+ rules = { [
95
+ {
96
+ required : true ,
97
+ message : ' Event Types organization name is required' ,
98
+ } ,
99
+ ] }
100
+ >
101
+ < Input />
102
+ </ Form . Item >
103
+ { /* end Event Type name */ }
134
104
135
- { /* Event Type group */ }
136
- { /* eslint-disable-next-line react/jsx-props-no-spreading */ }
137
- < Form . Item { ...formItemLayout } label = "Group" >
138
- { getFieldDecorator ( 'group' , {
139
- initialValue :
140
- isEditForm && eventType . relations . group // eslint-disable-line
141
- ? eventType . relations . group . _id // eslint-disable-line
142
- : undefined ,
143
- rules : [ { message : 'Event Type group is required' } ] ,
144
- } ) (
145
- < SearchableSelectInput
146
- onSearch = { getEventGroups }
147
- optionLabel = { ( group ) => group . strings . name . en }
148
- optionValue = "_id"
149
- initialValue = {
150
- isEditForm && eventType . relations . group
151
- ? eventType . relations . group
152
- : undefined
153
- }
154
- />
155
- ) }
156
- </ Form . Item >
157
- { /* end Event Type group */ }
105
+ { /* Event Type group */ }
106
+ < Form . Item
107
+ name = { [ 'relations' , 'group' ] }
108
+ label = "Group"
109
+ rules = { [ { message : 'Event Type group is required' } ] }
110
+ >
111
+ < SearchableSelectInput
112
+ onSearch = { getEventGroups }
113
+ optionLabel = { ( group ) => group . strings . name . en }
114
+ optionValue = "_id"
115
+ initialValue = { get ( eventType , 'relations.group' ) }
116
+ />
117
+ </ Form . Item >
118
+ { /* end Event Type group */ }
158
119
159
- { /* Event type */ }
160
- { /* eslint-disable-next-line react/jsx-props-no-spreading */ }
161
- < Form . Item { ...formItemLayout } label = "Description" >
162
- { getFieldDecorator ( 'description' , {
163
- initialValue : isEditForm
164
- ? eventType . strings . description . en
165
- : undefined ,
166
- rules : [ { required : true , message : 'Description is required' } ] ,
167
- } ) ( < TextArea autoSize = { { minRows : 3 , maxRows : 10 } } /> ) }
168
- </ Form . Item >
169
- { /* end Event Type */ }
120
+ { /* Event type */ }
121
+ < Form . Item
122
+ name = { [ 'strings' , 'description' , 'en' ] }
123
+ label = "Description"
124
+ rules = { [ { required : true , message : 'Description is required' } ] }
125
+ >
126
+ < TextArea autoSize = { { minRows : 3 , maxRows : 10 } } />
127
+ </ Form . Item >
128
+ { /* end Event Type */ }
170
129
171
- { /* form actions */ }
172
- < Form . Item wrapperCol = { { span : 24 } } style = { { textAlign : 'right' } } >
173
- < Button onClick = { onCancel } > Cancel</ Button >
174
- < Button
175
- style = { { marginLeft : 8 } }
176
- type = "primary"
177
- htmlType = "submit"
178
- loading = { posting }
179
- >
180
- Save
181
- </ Button >
182
- </ Form . Item >
183
- { /* end form actions */ }
184
- </ Form >
185
- ) ;
186
- }
187
- }
130
+ { /* form actions */ }
131
+ < Form . Item wrapperCol = { { span : 24 } } style = { { textAlign : 'right' } } >
132
+ < Button onClick = { onCancel } > Cancel</ Button >
133
+ < Button
134
+ style = { { marginLeft : 8 } }
135
+ type = "primary"
136
+ htmlType = "submit"
137
+ loading = { posting }
138
+ >
139
+ Save
140
+ </ Button >
141
+ </ Form . Item >
142
+ { /* end form actions */ }
143
+ </ Form >
144
+ ) ;
145
+ } ;
188
146
189
147
EventTypeForm . propTypes = {
190
148
eventType : PropTypes . shape ( {
@@ -210,4 +168,4 @@ EventTypeForm.propTypes = {
210
168
} ) . isRequired ,
211
169
} ;
212
170
213
- export default Form . create ( ) ( EventTypeForm ) ;
171
+ export default EventTypeForm ;
0 commit comments