({ createItem, exportCsv, onSubmit })
| 20 | import { OptionsRows } from './styles'; |
| 21 | |
| 22 | const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }) => { |
| 23 | const [uploadVisibility, showUploadModal] = React.useState<boolean>(false); |
| 24 | const app = React.useContext(AppContext); |
| 25 | if (!app.userInfo) { |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | function exportItems() { |
| 30 | exportCsv().then(data => FileDownload(data, 'inventory.csv')); |
| 31 | } |
| 32 | |
| 33 | return ( |
| 34 | <Formik |
| 35 | initialValues={{ |
| 36 | name: '', |
| 37 | categoryId: undefined, |
| 38 | product_name: '', |
| 39 | weight: undefined, |
| 40 | weight_unit: app.userInfo.default_weight_unit, |
| 41 | product_url: '', |
| 42 | newCategory: false |
| 43 | }} |
| 44 | validationSchema={Yup.object().shape({ |
| 45 | name: Yup.string().required('Item name is required'), |
| 46 | categoryId: Yup.string().required('Category is required') |
| 47 | })} |
| 48 | onSubmit={(values, formikActions) => { |
| 49 | createItem(values) |
| 50 | .then(() => { |
| 51 | onSubmit(); |
| 52 | formikActions.setSubmitting(false); |
| 53 | formikActions.resetForm(); |
| 54 | alertSuccess({ message: 'Item added' }); |
| 55 | if (values.newCategory) { |
| 56 | app.fetchUser(); |
| 57 | } |
| 58 | }) |
| 59 | .catch(() => { |
| 60 | formikActions.setSubmitting(false); |
| 61 | alertError({ message: 'An error occurred' }); |
| 62 | }); |
| 63 | }} |
| 64 | > |
| 65 | {(props: FormikProps<CreateItem>) => { |
| 66 | const { values, setFieldValue, errors, submitCount, submitForm, isSubmitting } = props; |
| 67 | const wasSubmitted = submitCount > 0; |
| 68 | const weightUnit = values.weight_unit; |
| 69 | const categoryValue = categorySelectValue(app.categories, values.categoryId); |
| 70 | |
| 71 | return ( |
| 72 | <SidebarContainer> |
| 73 | <Input |
| 74 | value={values.name} |
| 75 | label="Item Type" |
| 76 | error={wasSubmitted && !!errors.name} |
| 77 | placeholder="Backpack, Compass, etc..." |
| 78 | onChange={v => setFieldValue('name', v)} |
| 79 | /> |
nothing calls this directly
no test coverage detected