This repository has been archived by the owner on Aug 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
ProductDialog.js
116 lines (109 loc) · 3.02 KB
/
ProductDialog.js
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
import { CATEGORIES } from "constants/product";
import React from "react";
import PropTypes from "prop-types";
import { Formik, Field } from "formik";
import { compose } from "redux";
import { withStyles } from "@material-ui/core/styles";
import Dialog from "@material-ui/core/Dialog";
import DialogContent from "@material-ui/core/DialogContent";
import MenuItem from "@material-ui/core/MenuItem";
import { TextField } from "formik-material-ui";
import Typography from "@material-ui/core/Typography";
import withMobileDialog from "@material-ui/core/withMobileDialog";
import Slide from "@material-ui/core/Slide";
import DialogToolbar from "./DialogToolbar";
const styles = theme => ({
root: {}
});
function Transition(props) {
return <Slide direction="up" {...props} />;
}
const ProductDialog = ({
classes,
open,
fullScreen,
onClose,
initialValue,
onSubmit
}) => (
<Dialog
className={classes.root}
open={open}
onClose={onClose}
aria-labelledby="form-dialog-title"
fullWidth
fullScreen={fullScreen}
TransitionComponent={Transition}
>
<Formik
initialValues={initialValue || { category: "" }}
validate={values => {
let errors = {};
if (!values.category) {
errors.category = "Select a category.";
}
if (!values.name) {
errors.name = "Enter a name.";
}
return errors;
}}
onSubmit={onSubmit}
render={({
handleSubmit,
handleChange,
isSubmitting,
values,
errors
}) => (
<form onSubmit={handleSubmit}>
<DialogToolbar
title="Add Product"
onClose={onClose}
buttonText="Done"
buttonIsLoading={isSubmitting}
onClickButton={handleSubmit}
/>
<DialogContent>
{/* TODO: style errors */}
{errors.form ? <Typography>{errors.form}</Typography> : null}
<Field
label="Category"
name="category"
component={TextField}
select
value={values.category}
onChange={handleChange}
fullWidth
margin="dense"
>
{CATEGORIES.map(category => (
<MenuItem key={category} id="category" value={category}>
{category}
</MenuItem>
))}
</Field>
<Field
label="Name"
name="name"
component={TextField}
fullWidth
margin="dense"
helperText="Enter a name, like “T-Shirt” or “Socks”. Don’t reuse the category
name here."
/>
</DialogContent>
</form>
)}
/>
</Dialog>
);
ProductDialog.propTypes = {
classes: PropTypes.object.isRequired,
open: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
fullScreen: PropTypes.bool.isRequired
};
export default compose(
withStyles(styles),
withMobileDialog()
)(ProductDialog);