Skip to content

Commit 662cd44

Browse files
committed
refactor(settings): migrate event status form to ant v4
ref #250
1 parent e519d9c commit 662cd44

File tree

3 files changed

+135
-170
lines changed

3 files changed

+135
-170
lines changed

src/Events/EventStatuses/Form/index.js

-165
This file was deleted.

src/Events/EventStatuses/index.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import PropTypes from 'prop-types';
88
import React, { Component } from 'react';
99
import NotificationForm from '../../components/NotificationForm';
1010
import Topbar from '../../components/Topbar';
11-
import EventStatusForm from './Form';
11+
import SettingForm from '../../components/SettingForm';
1212
import ListItemActions from '../../components/ListItemActions';
1313
import ListItem from '../../components/ListItem';
1414
import ItemList from '../../components/List';
@@ -33,6 +33,8 @@ const {
3333
refreshEventStatuses,
3434
paginateEventStatuses,
3535
deleteEventStatus,
36+
postEventStatus,
37+
putEventStatus,
3638
} = reduxActions;
3739

3840
const nameSpan = { xxl: 4, xl: 5, lg: 6, md: 7, sm: 0, xs: 0 };
@@ -202,6 +204,7 @@ class EventStatuses extends Component {
202204
* @since 0.1.0
203205
*/
204206
handleAfterCloseForm = () => {
207+
selectEventStatus(null);
205208
this.setState({ isEditForm: false });
206209
};
207210

@@ -383,11 +386,12 @@ class EventStatuses extends Component {
383386
maskClosable={false}
384387
afterClose={this.handleAfterCloseForm}
385388
>
386-
<EventStatusForm
389+
<SettingForm
387390
posting={posting}
388-
isEditForm={isEditForm}
389-
eventStatus={eventStatus}
390-
onCancel={this.closeEventStatusForm}
391+
setting={eventStatus}
392+
onCancel={this.handleAfterCloseForm}
393+
onCreate={postEventStatus}
394+
onUpdate={putEventStatus}
391395
/>
392396
</Modal>
393397
{/* end create/edit form modal */}

src/components/SettingForm/index.js

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { Button, Input, Form } from 'antd';
4+
import get from 'lodash/get';
5+
6+
import { notifyError, notifySuccess } from '../../util';
7+
8+
const { TextArea } = Input;
9+
const formItemLayout = {
10+
labelCol: {
11+
xs: { span: 24 },
12+
sm: { span: 24 },
13+
md: { span: 24 },
14+
lg: { span: 24 },
15+
xl: { span: 24 },
16+
xxl: { span: 24 },
17+
},
18+
wrapperCol: {
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+
};
27+
28+
/**
29+
* @function
30+
* @name SettingForm
31+
* @description Form for creating and editing settings
32+
* @param {object} props Properties object
33+
* @param {object|null} props.setting Setting object to be edited
34+
* @param {boolean} props.posting Flag for showing spinner while posting
35+
* @param {Function} props.onCancel On Cancel form callback
36+
* @param {Function} props.onCreate On Create setting callback function
37+
* @param {Function} props.onUpdate onUpdate setting callback function
38+
* @returns {object} Setting Form
39+
* @version 0.1.0
40+
* @since 0.1.0
41+
*/
42+
const SettingForm = ({ setting, posting, onCancel, onCreate, onUpdate }) => {
43+
const onFinish = (values) => {
44+
if (get(setting, '_id')) {
45+
const updatedSetting = { ...setting, ...values };
46+
onUpdate(
47+
updatedSetting,
48+
() => notifySuccess('Setting was updated successfully'),
49+
() =>
50+
notifyError(
51+
'An error occurred while updating setting, please contact your system administrator'
52+
)
53+
);
54+
55+
return;
56+
}
57+
58+
onCreate(
59+
values,
60+
() => notifySuccess('Setting was created successfully'),
61+
() =>
62+
notifyError(
63+
'An error occurred while saving setting, please contact your system administrator'
64+
)
65+
);
66+
};
67+
68+
return (
69+
<Form
70+
{...formItemLayout} // eslint-disable-line
71+
autoComplete="off"
72+
onFinish={onFinish}
73+
initialValues={{ ...setting }}
74+
>
75+
{/* setting name */}
76+
<Form.Item
77+
label="Name"
78+
name={['strings', 'name', 'en']}
79+
rules={[{ required: true, message: 'This field is required' }]}
80+
>
81+
<Input />
82+
</Form.Item>
83+
{/* end setting name */}
84+
85+
{/* setting description */}
86+
<Form.Item label="Description" name={['strings', 'description', 'en']}>
87+
<TextArea autoSize={{ minRows: 3, maxRows: 10 }} />
88+
</Form.Item>
89+
{/* end setting description */}
90+
91+
{/* form actions */}
92+
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
93+
<Button onClick={onCancel}>Cancel</Button>
94+
<Button
95+
style={{ marginLeft: 8 }}
96+
type="primary"
97+
htmlType="submit"
98+
loading={posting}
99+
>
100+
Save
101+
</Button>
102+
</Form.Item>
103+
{/* end form actions */}
104+
</Form>
105+
);
106+
};
107+
108+
SettingForm.propTypes = {
109+
setting: PropTypes.shape({
110+
strings: PropTypes.shape({
111+
name: PropTypes.shape({ en: PropTypes.string }),
112+
abbreviation: PropTypes.shape({ en: PropTypes.string }),
113+
description: PropTypes.shape({ en: PropTypes.string }),
114+
}),
115+
}),
116+
onCancel: PropTypes.func.isRequired,
117+
onCreate: PropTypes.func.isRequired,
118+
onUpdate: PropTypes.func.isRequired,
119+
posting: PropTypes.bool.isRequired,
120+
};
121+
122+
SettingForm.defaultProps = {
123+
setting: null,
124+
};
125+
126+
export default SettingForm;

0 commit comments

Comments
 (0)