|
| 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