-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ItemForm.tsx
183 lines (171 loc) · 7.95 KB
/
ItemForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as React from 'react';
import * as Yup from 'yup';
import FileDownload from 'js-file-download';
import { Formik, FormikProps } from "formik";
import { Row, Col, Button } from "antd";
import { Input, Select, SelectCreatable, Option } from '../FormFields';
import { AppContext } from 'AppContext';
import { CreateItem, ItemConstants } from "types/item";
import { FormSpecs } from "./types";
import withApi from 'app/components/higher-order/with-api';
import { categoryOptions, weightUnitOptions } from "lib/utils/form";
import { categorySelectValue } from "lib/utils/categories";
import UploadModal from 'app/Inventory/UploadModal';
import { alertError, alertSuccess } from "../Notifications";
import { SidebarContainer } from "styles/common";
import { OptionsRows } from './styles';
const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }) => {
const [uploadVisibility, showUploadModal] = React.useState<boolean>(false);
const app = React.useContext(AppContext);
if (!app.userInfo) {
return null;
}
function exportItems() {
exportCsv().then(data => FileDownload(data, 'inventory.csv'));
}
const { default_weight_unit } = app.userInfo;
return (
<Formik
initialValues={{
name: '',
categoryId: undefined,
product_name: '',
weight: undefined,
weight_unit: default_weight_unit,
price: undefined,
product_url: '',
newCategory: false,
notes: ''
}}
validationSchema={Yup.object().shape({
name: Yup.string().required('Item name is required'),
categoryId: Yup.string().required('Category is required')
})}
onSubmit={(values, formikActions) => {
createItem(values)
.then(() => {
onSubmit();
formikActions.setSubmitting(false);
formikActions.resetForm();
alertSuccess({ message: 'Item added' });
if (values.newCategory) {
app.fetchUser();
}
})
.catch(() => {
formikActions.setSubmitting(false);
alertError({ message: 'An error occurred' });
});
}}
>
{(props: FormikProps<CreateItem>) => {
const { values, setFieldValue, errors, submitCount, submitForm, isSubmitting } = props;
const wasSubmitted = submitCount > 0;
const weightUnit = values.weight_unit;
const categoryValue = categorySelectValue(app.categories, values.categoryId);
return (
<SidebarContainer>
<Input
value={values.name}
label="Item Type"
error={wasSubmitted && !!errors.name}
placeholder="Backpack, Compass, etc..."
onChange={v => setFieldValue('name', v)}
allowedLength={ItemConstants.name}
/>
<SelectCreatable
label="Category"
placeholder="Select a category..."
options={categoryOptions(app.categories)}
value={categoryValue || null}
onChange={(option: Option<number>) => {
const value = option ? option.value : undefined;
const isNewCategory = Boolean(option && option.__isNew__);
setFieldValue('categoryId', value);
setFieldValue('newCategory', isNewCategory);
}}
error={wasSubmitted && !!errors.categoryId}
errorMsg={errors.categoryId}
clearable={true}
/>
<Input
label="Product Name"
value={values.product_name || ''}
placeholder="Osprey Renn 65"
onChange={v => setFieldValue('product_name', v)}
allowedLength={ItemConstants.product_name}
/>
<Row gutter={8}>
<Col span={16}>
<Input
value={values.weight || ''}
label="Weight"
type="number"
placeholder="0.0"
onChange={v => setFieldValue('weight', v)}
/>
</Col>
<Col span={8}>
<Select
defaultValue={weightUnit && {
value: weightUnit,
label: weightUnit
}}
label="Unit"
tip="Change default weight unit in Settings"
options={weightUnitOptions()}
onChange={(option: Option<string>) => setFieldValue('weight_unit', option.value)}/>
</Col>
</Row>
<Input
label="Price"
value={values.price || ''}
placeholder="0.00"
onChange={v => setFieldValue('price', v)}
/>
<Input
label="Product URL"
value={values.product_url || ''}
type="url"
placeholder="https://osprey.com"
onChange={v => setFieldValue('product_url', v)}
allowedLength={ItemConstants.product_url}
/>
<Input
label="Notes"
value={values.notes || ''}
placeholder="Care instructions, further details, etc..."
onChange={v => setFieldValue('notes', v)}
allowedLength={ItemConstants.notes}
/>
<Button onClick={submitForm}
disabled={isSubmitting}
block={true}
size="large"
type="primary">
Add Item
</Button>
<OptionsRows>
<Button onClick={exportItems} type="link" size="small">
Export items
</Button>
<Button onClick={() => showUploadModal(true)} type="dashed" icon="upload">
Import Items
</Button>
</OptionsRows>
<UploadModal
visible={uploadVisibility}
fetchItems={onSubmit}
hideModal={() => showUploadModal(false)}
/>
</SidebarContainer>
)
}}
</Formik>
)
};
const ItemFormWithProps = withApi<FormSpecs.ApiProps>(api => ({
createItem: api.ItemService.create,
exportCsv: api.ItemService.exportCSV
}))(ItemForm);
export default ItemFormWithProps;