Skip to content

Commit 0edd5b5

Browse files
committed
feat(item category): edit item category
closes #126
1 parent 01da5dc commit 0edd5b5

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import {
2+
postItemCategory,
3+
putItemCategory,
4+
} from '@codetanzania/emis-api-states';
5+
import { Button, Form, Input, Col, Row } from 'antd';
6+
import PropTypes from 'prop-types';
7+
import React, { Component } from 'react';
8+
import ColorPicker from 'rc-color-picker';
9+
import { notifyError, notifySuccess } from '../../../../util';
10+
import 'rc-color-picker/assets/index.css';
11+
import './styles.css';
12+
13+
/**
14+
* @class
15+
* @value ItemCategoryForm
16+
* @description Render Item Category form for creating/editing Item Categories
17+
*
18+
* @version 0.1.0
19+
* @since 0.1.0
20+
*/
21+
22+
class ItemCategoryForm extends Component {
23+
static propTypes = {
24+
isEditForm: PropTypes.bool.isRequired,
25+
itemCategory: PropTypes.shape({
26+
_id: PropTypes.string,
27+
value: PropTypes.string,
28+
color: PropTypes.string,
29+
abbreviation: PropTypes.string,
30+
description: PropTypes.string,
31+
}),
32+
form: PropTypes.shape({ getFieldDecorator: PropTypes.func }).isRequired,
33+
posting: PropTypes.bool.isRequired,
34+
onCancel: PropTypes.func.isRequired,
35+
};
36+
37+
static defaultProps = {
38+
itemCategory: null,
39+
};
40+
41+
/**
42+
* @function
43+
* @name onChangeColor
44+
* @description Handle changing of color
45+
*
46+
* @param {string} color event object
47+
* @version 0.1.0
48+
* @since 0.1.0
49+
*/
50+
onChangeColor = ({ color }) => {
51+
const {
52+
form: { setFieldsValue },
53+
} = this.props;
54+
setFieldsValue({ color });
55+
};
56+
57+
/**
58+
* @function
59+
* @name handleSubmit
60+
* @description Handle create/edit action
61+
*
62+
* @param {Object} e event object
63+
* @version 0.1.0
64+
* @since 0.1.0
65+
*/
66+
handleSubmit = e => {
67+
e.preventDefault();
68+
69+
const {
70+
form: { validateFieldsAndScroll },
71+
itemCategory,
72+
isEditForm,
73+
} = this.props;
74+
75+
validateFieldsAndScroll((error, values) => {
76+
console.log({
77+
...values,
78+
key: 'category',
79+
});
80+
if (!error) {
81+
if (isEditForm) {
82+
const updateItemCategory = Object.assign({}, itemCategory, {
83+
...values,
84+
key: 'category',
85+
});
86+
putItemCategory(
87+
updateItemCategory,
88+
() => {
89+
notifySuccess('Item Category was updated successfully');
90+
},
91+
() => {
92+
notifyError(
93+
`Something occurred while updating Item Category,
94+
please try again!`
95+
);
96+
}
97+
);
98+
} else {
99+
postItemCategory(
100+
values,
101+
() => {
102+
notifySuccess('Item Category was created successfully');
103+
},
104+
() => {
105+
notifyError(
106+
'Something occurred while saving Item Category, please try again!'
107+
);
108+
}
109+
);
110+
}
111+
}
112+
});
113+
};
114+
115+
render() {
116+
const {
117+
isEditForm,
118+
onCancel,
119+
itemCategory,
120+
posting,
121+
form: { getFieldDecorator },
122+
} = this.props;
123+
124+
const formItemLayout = {
125+
labelCol: {
126+
xs: { span: 24 },
127+
sm: { span: 24 },
128+
md: { span: 24 },
129+
lg: { span: 24 },
130+
xl: { span: 24 },
131+
xxl: { span: 24 },
132+
},
133+
wrapperCol: {
134+
xs: { span: 24 },
135+
sm: { span: 24 },
136+
md: { span: 24 },
137+
lg: { span: 24 },
138+
xl: { span: 24 },
139+
xxl: { span: 24 },
140+
},
141+
};
142+
143+
return (
144+
<Form onSubmit={this.handleSubmit} autoComplete="off">
145+
{/* Item Categories value */}
146+
<Form.Item {...formItemLayout} label="Name ">
147+
{getFieldDecorator('value', {
148+
initialValue: isEditForm ? itemCategory.value : undefined,
149+
rules: [{ required: true, message: 'name is required' }],
150+
})(<Input />)}
151+
</Form.Item>
152+
{/* end Item Categories value */}
153+
154+
{/* Item Categories abbreviation */}
155+
<Form.Item {...formItemLayout} label="Abbreviation">
156+
{getFieldDecorator('abbreviation', {
157+
initialValue: isEditForm ? itemCategory.value : undefined,
158+
rules: [{ required: true, message: 'Abbreviation is required' }],
159+
})(<Input />)}
160+
</Form.Item>
161+
{/* end Item Categories abbreviation */}
162+
163+
{/* Item Categories value */}
164+
<Form.Item {...formItemLayout} label="Description ">
165+
{getFieldDecorator('description', {
166+
initialValue: isEditForm ? itemCategory.value : undefined,
167+
rules: [{ required: true, message: 'Description is required' }],
168+
})(<Input />)}
169+
</Form.Item>
170+
{/* end Item Categories value */}
171+
172+
<Row>
173+
<Col span={19}>
174+
<Form.Item {...formItemLayout} label="Color Code">
175+
{getFieldDecorator('color', {
176+
initialValue: isEditForm ? itemCategory.color : undefined,
177+
})(<Input title="Click button to select color" />)}
178+
</Form.Item>
179+
</Col>
180+
<Col span={4} offset={1} className="ItemCategoryFormColor">
181+
<ColorPicker animation="slide-up" onChange={this.onChangeColor} />
182+
</Col>
183+
</Row>
184+
{/* end Item Categories color code */}
185+
186+
{/* form actions */}
187+
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
188+
<Button onClick={onCancel}>Cancel</Button>
189+
<Button
190+
style={{ marginLeft: 8 }}
191+
type="primary"
192+
htmlType="submit"
193+
loading={posting}
194+
>
195+
Save
196+
</Button>
197+
</Form.Item>
198+
{/* end form actions */}
199+
</Form>
200+
);
201+
}
202+
}
203+
204+
export default Form.create()(ItemCategoryForm);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.rc-color-picker-trigger {
2+
width: 80px;
3+
height: 33px;
4+
}
5+
6+
.ItemCategoryFormColor {
7+
margin-top: 32px;
8+
}

0 commit comments

Comments
 (0)