|
1 | | -import React from "react"; |
| 1 | +import React, {useState} from "react"; |
| 2 | +import {Mutation} from "react-apollo"; |
| 3 | +import {gql} from "apollo-boost"; |
2 | 4 | import withStyles from "@material-ui/core/styles/withStyles"; |
3 | | -// import Typography from "@material-ui/core/Typography"; |
4 | | -// import Avatar from "@material-ui/core/Avatar"; |
5 | | -// import FormControl from "@material-ui/core/FormControl"; |
6 | | -// import Paper from "@material-ui/core/Paper"; |
7 | | -// import Input from "@material-ui/core/Input"; |
8 | | -// import InputLabel from "@material-ui/core/InputLabel"; |
9 | | -// import Button from "@material-ui/core/Button"; |
10 | | -// import Dialog from "@material-ui/core/Dialog"; |
11 | | -// import DialogActions from "@material-ui/core/DialogActions"; |
12 | | -// import DialogContent from "@material-ui/core/DialogContent"; |
13 | | -// import DialogContentText from "@material-ui/core/DialogContentText"; |
14 | | -// import DialogTitle from "@material-ui/core/DialogTitle"; |
15 | | -// import Slide from "@material-ui/core/Slide"; |
16 | | -// import Gavel from "@material-ui/icons/Gavel"; |
17 | | -// import VerifiedUserTwoTone from "@material-ui/icons/VerifiedUserTwoTone"; |
| 5 | +import Typography from "@material-ui/core/Typography"; |
| 6 | +import Avatar from "@material-ui/core/Avatar"; |
| 7 | +import FormControl from "@material-ui/core/FormControl"; |
| 8 | +import Paper from "@material-ui/core/Paper"; |
| 9 | +import Input from "@material-ui/core/Input"; |
| 10 | +import InputLabel from "@material-ui/core/InputLabel"; |
| 11 | +import Button from "@material-ui/core/Button"; |
| 12 | +import Dialog from "@material-ui/core/Dialog"; |
| 13 | +import DialogActions from "@material-ui/core/DialogActions"; |
| 14 | +import DialogContent from "@material-ui/core/DialogContent"; |
| 15 | +import DialogContentText from "@material-ui/core/DialogContentText"; |
| 16 | +import DialogTitle from "@material-ui/core/DialogTitle"; |
| 17 | +import Slide from "@material-ui/core/Slide"; |
| 18 | +import VerifiedUserTwoTone from "@material-ui/icons/VerifiedUserTwoTone"; |
| 19 | +import ImportContacts from "@material-ui/icons/ImportContacts"; |
18 | 20 |
|
19 | | -const Register = ({ classes }) => { |
20 | | - return <div>Register</div>; |
| 21 | +function Transition(props) { |
| 22 | + return <Slide direction="up" {...props}/> |
| 23 | +} |
| 24 | + |
| 25 | +const Register = ({ classes , setNewUser }) => { |
| 26 | + const [username, setUsername] = useState("") |
| 27 | + const [email, setEmail] = useState("") |
| 28 | + const [password, setPassword] = useState("") |
| 29 | + const [open, setOpen] = useState(false) |
| 30 | + |
| 31 | + |
| 32 | + const handleSubmit = (e, createUser) => { |
| 33 | + e.preventDefault() |
| 34 | + createUser() |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + return <div className={classes.root}> |
| 39 | + <Paper className={classes.paper}> |
| 40 | + <Avatar className={classes.avatar}> |
| 41 | + <ImportContacts/> |
| 42 | + </Avatar> |
| 43 | + <Typography variant="headline"> |
| 44 | + Register |
| 45 | + </Typography> |
| 46 | + |
| 47 | + <Mutation mutation={REGISTER_MUTATION} |
| 48 | + variables={{username, email, password}} |
| 49 | + onCompleted={data => { |
| 50 | + console.log({data}) |
| 51 | + setOpen(true) |
| 52 | + }} |
| 53 | + > |
| 54 | + {(createUser, { loading, error}) => { |
| 55 | + return ( |
| 56 | + <form onSubmit={e => handleSubmit(e, createUser)} className={classes.form}> |
| 57 | + <FormControl margin="normal" required fullWidth> |
| 58 | + <InputLabel htmlFor="username">Username</InputLabel> |
| 59 | + <Input id="username" onChange={e => setUsername(e.target.value)}/> |
| 60 | + </FormControl> |
| 61 | + <FormControl margin="normal" required fullWidth> |
| 62 | + <InputLabel htmlFor="email">Email</InputLabel> |
| 63 | + <Input id="email" type="email" onChange={e => setEmail(e.target.value)}/> |
| 64 | + </FormControl> |
| 65 | + <FormControl margin="normal" required fullWidth> |
| 66 | + <InputLabel htmlFor="password">Password</InputLabel> |
| 67 | + <Input id="password" onChange={e => setPassword(e.target.value)}/> |
| 68 | + </FormControl> |
| 69 | + <Button type="submit" fullWidth variant="contained" color="secondary" className={classes.submit} disabled={loading || !username.trim() || !email.trim() || !password.trim()}> |
| 70 | + {loading ? "Registering" : "Register"} |
| 71 | + </Button> |
| 72 | + <Button onClick={() => setNewUser(false)} color="primary" fullWidth variant="outlined">Already Registered</Button> |
| 73 | + {error && <div>Error</div>} |
| 74 | + </form> |
| 75 | + ) |
| 76 | + }} |
| 77 | + </Mutation> |
| 78 | + </Paper> |
| 79 | + {/* Success Dialog */} |
| 80 | + <Dialog |
| 81 | + open={open} |
| 82 | + disableBackdropClick={true} |
| 83 | + TransitionComponent={Transition} |
| 84 | + > |
| 85 | + <DialogTitle> |
| 86 | + <VerifiedUserTwoTone className={classes.icon}/> |
| 87 | + Create Account</DialogTitle> |
| 88 | + <DialogContent> |
| 89 | + <DialogContentText>User Successfully Created</DialogContentText> |
| 90 | + </DialogContent> |
| 91 | + <DialogActions> |
| 92 | + <Button color="primary" variant="contained" onClick={() => setNewUser(false)}>Log In</Button> |
| 93 | + </DialogActions> |
| 94 | + </Dialog> |
| 95 | + </div>; |
21 | 96 | }; |
22 | 97 |
|
| 98 | +const REGISTER_MUTATION = gql ` |
| 99 | +mutation ($username: String!, $email: String!, $password: String!) { |
| 100 | + createUser(email: $email, username: $username, password: $password) { |
| 101 | + user { |
| 102 | + username |
| 103 | + email |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +` |
| 108 | + |
23 | 109 | const styles = theme => ({ |
24 | 110 | root: { |
25 | 111 | width: "auto", |
|
0 commit comments