Guest User

Signup.js

a guest
Oct 14th, 2020
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. import React from 'react';
  2.  
  3. import Card from '@material-ui/core/Card';
  4. import CardActions from '@material-ui/core/CardActions';
  5. import CardContent from '@material-ui/core/CardContent';
  6. import Button from '@material-ui/core/Button';
  7. import Typography from '@material-ui/core/Typography';
  8. import TextField from '@material-ui/core/TextField';
  9. import ArrowRight from '@material-ui/icons/ArrowRight';
  10. import Alert from '@material-ui/lab/Alert';
  11. import Collapse from '@material-ui/core/Collapse';
  12. import Grid from '@material-ui/core/Grid';
  13.  
  14.  
  15. import { withStyles } from '@material-ui/core/styles';
  16.  
  17. import api from '../services/api';
  18.  
  19.  
  20. class Signup extends React.Component {
  21. constructor(props) {
  22. super(props);
  23.  
  24. this.state = {
  25. form: {
  26. error: null,
  27. username: {
  28. value: '',
  29. error: null,
  30. },
  31. password: {
  32. value: '',
  33. error: null,
  34. },
  35.  
  36. passwordConfirm: {
  37. value: '',
  38. error: null,
  39. },
  40.  
  41. },
  42. };
  43.  
  44. this.onUsernameChanged = this.onUsernameChanged.bind(this);
  45. this.onPasswordChanged = this.onPasswordChanged.bind(this);
  46. this.onPasswordConfirmChanged = this.onPasswordConfirmChanged.bind(this);
  47. this.onSubmit = this.onSubmit.bind(this);
  48. }
  49.  
  50. onUsernameChanged(event) {
  51. const form = {
  52. ...this.state.form,
  53. error: null,
  54. username: {
  55. value: event.target.value,
  56. error: null
  57. }
  58. }
  59. this.setState({ form });
  60. }
  61.  
  62. onPasswordChanged(event) {
  63. const form = {
  64. ...this.state.form,
  65. error: null,
  66. password: {
  67. value: event.target.value,
  68. error: null
  69. }
  70. }
  71. this.setState({ form });
  72. }
  73.  
  74. onPasswordConfirmChanged(event) {
  75. const form = {
  76. ...this.state.form,
  77. error: null,
  78. passwordConfirm: {
  79. value: event.target.value,
  80. error: null
  81. }
  82. }
  83. this.setState({ form });
  84. }
  85.  
  86. onSubmit(event) {
  87. event.preventDefault();
  88.  
  89. const { form: { username, password, passwordConfirm } } = this.state;
  90. if(username.value.length< 4 ){
  91. const form = {
  92. ...this.state.form,
  93.  
  94. username: {
  95. ...this.state.form.username,
  96. error: 'username is too short',
  97. }
  98. }
  99.  
  100. this.setState({ form });
  101. return
  102. }
  103.  
  104. if(password.value.length < 4 ){
  105. const form = {
  106. ...this.state.form,
  107.  
  108. passwordConfirm: {
  109. ...this.state.form.passwordConfirm,
  110. error: 'password is too short',
  111. }
  112. }
  113.  
  114. this.setState({ form });
  115. return
  116. }
  117. if (password.value !== passwordConfirm.value){
  118. const form = {
  119. ...this.state.form,
  120.  
  121. passwordConfirm: {
  122. ...this.state.form.passwordConfirm,
  123. error: 'Passwords do not match',
  124. }
  125. }
  126.  
  127. this.setState({ form });
  128. return
  129. }
  130.  
  131. const params = {
  132. username: username.value,
  133. password: password.value,
  134. }
  135.  
  136. api.post('/signup/', params).then((response) => {
  137. debugger
  138. const { token } = response.data;
  139.  
  140. localStorage.setItem('token', token);
  141.  
  142. this.props.onLogin({ username: params.username }); // TODO: fixit
  143. }).catch((error) => {
  144. debugger
  145. const { detail, non_field_errors, password, username } = error.response.data;
  146.  
  147. const prevForm = this.state.form
  148. const form = {
  149. ...prevForm,
  150. error: detail || non_field_errors,
  151. username: {
  152. ...prevForm.username,
  153. error: username,
  154. },
  155. password: {
  156. ...prevForm.password,
  157. error: password,
  158. }
  159. }
  160.  
  161. this.setState({ form });
  162. })
  163. }
  164.  
  165. render() {
  166. const { form: { error, username, password, passwordConfirm} } = this.state;
  167.  
  168. return (
  169. <form autoComplete="off" onSubmit={this.onSubmit} noValidate className={this.props.classes.root}>
  170. <Card >
  171. <CardContent>
  172. <Typography variant="h5" component="h2">Signup</Typography>
  173. <Typography color="textSecondary">Enter your data to Signup</Typography>
  174.  
  175. <Collapse in={!!error}>
  176. <Alert severity="error">{error}</Alert>
  177. </Collapse>
  178.  
  179. <TextField
  180. label="Username"
  181. value={username.value}
  182. onChange={this.onUsernameChanged}
  183. autoComplete="off"
  184. autoFocus
  185. required
  186. fullWidth
  187. margin="normal"
  188. error={!!username.error}
  189. helperText={username.error}
  190. />
  191. <TextField
  192. label="Password"
  193. value={password.value}
  194. onChange={this.onPasswordChanged}
  195. autoComplete="off"
  196. fullWidth
  197. required
  198. type="password"
  199. error={!!password.error}
  200. helperText={password.error}
  201. />
  202. <TextField
  203. label="Password confirm"
  204. value={passwordConfirm.value}
  205. onChange={this.onPasswordConfirmChanged}
  206. autoComplete="off"
  207. fullWidth
  208. required
  209. type="password"
  210. error={!!passwordConfirm.error}
  211. helperText={passwordConfirm.error}
  212. />
  213.  
  214. </CardContent>
  215. <CardActions>
  216. <Grid container justify="flex-end">
  217.  
  218. <Button
  219. startIcon={<ArrowRight />}
  220. type="submit"
  221. variant="contained"
  222. color="default"
  223. disableElevation
  224. disabled={!!error}
  225. >
  226. Signup
  227. </Button>
  228. </Grid>
  229.  
  230. </CardActions>
  231. </Card>
  232. </form>
  233. );
  234. }
  235. }
  236.  
  237.  
  238. const styles = {
  239. root: {
  240. width: 400,
  241. margin: "0 auto"
  242. },
  243. };
  244.  
  245. export default withStyles(styles)(Signup);
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment