Guest User

Login.js

a guest
Oct 14th, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 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 Login extends React.Component {
  21. constructor(props) {
  22. super(props);
  23.  
  24. this.state = {
  25. form: {
  26. error: null,
  27. username: {
  28. value: 'george',
  29. error: null,
  30. },
  31. password: {
  32. value: 'password',
  33. error: null,
  34. },
  35. },
  36. };
  37.  
  38. this.onUsernameChanged = this.onUsernameChanged.bind(this);
  39. this.onPasswordChanged = this.onPasswordChanged.bind(this);
  40. this.onSubmit = this.onSubmit.bind(this);
  41. }
  42.  
  43. onUsernameChanged(event) {
  44. const form = {
  45. ...this.state.form,
  46. error: null,
  47. username: {
  48. value: event.target.value,
  49. error: null
  50. }
  51. }
  52. this.setState({ form });
  53. }
  54.  
  55. onPasswordChanged(event) {
  56. const form = {
  57. ...this.state.form,
  58. error: null,
  59. password: {
  60. value: event.target.value,
  61. error: null
  62. }
  63. }
  64. this.setState({ form });
  65. }
  66.  
  67. onSubmit(event) {
  68. event.preventDefault();
  69.  
  70. const { form: { username, password } } = this.state;
  71. const params = {
  72. username: username.value,
  73. password: password.value,
  74. }
  75.  
  76. api.post('/api-token-auth/', params).then((response) => {
  77. const { token } = response.data;
  78.  
  79. localStorage.setItem('token', token);
  80.  
  81. this.props.onLogin({ username: params.username }); // TODO: fixit
  82. }).catch((error) => {
  83. const { detail, non_field_errors, password, username } = error.response.data;
  84.  
  85. const prevForm = this.state.form
  86. const form = {
  87. ...prevForm,
  88. error: detail || non_field_errors,
  89. username: {
  90. ...prevForm.username,
  91. error: username,
  92. },
  93. password: {
  94. ...prevForm.password,
  95. error: password,
  96. }
  97. }
  98.  
  99. this.setState({ form });
  100. })
  101. }
  102.  
  103. render() {
  104. const { form: { error, username, password} } = this.state;
  105.  
  106. return (
  107. <form autoComplete="off" onSubmit={this.onSubmit} noValidate className={this.props.classes.root}>
  108. <Card >
  109. <CardContent>
  110. <Typography variant="h5" component="h2">Login</Typography>
  111. <Typography color="textSecondary">Enter your data to login</Typography>
  112.  
  113. <Collapse in={!!error}>
  114. <Alert severity="error">{error}</Alert>
  115. </Collapse>
  116.  
  117. <TextField
  118. label="Username"
  119. value={username.value}
  120. onChange={this.onUsernameChanged}
  121. autoComplete="off"
  122. autoFocus
  123. required
  124. fullWidth
  125. margin="normal"
  126. error={!!username.error}
  127. helperText={username.error}
  128. />
  129. <TextField
  130. label="Password"
  131. value={password.value}
  132. onChange={this.onPasswordChanged}
  133. autoComplete="off"
  134. fullWidth
  135. required
  136. type="password"
  137. error={!!password.error}
  138. helperText={password.error}
  139. />
  140.  
  141. </CardContent>
  142. <CardActions>
  143. <Grid container justify="flex-end">
  144. <Button
  145. startIcon={<ArrowRight />}
  146. type="submit"
  147. variant="contained"
  148. color="default"
  149. disableElevation
  150. disabled={!!error}
  151. >
  152. Login
  153. </Button>
  154.  
  155. <Button
  156. startIcon={<ArrowRight />}
  157. type="submit"
  158. variant="contained"
  159. color="default"
  160. disableElevation
  161. disabled={!!error}
  162. >
  163. Signup
  164. </Button>
  165. </Grid>
  166.  
  167. </CardActions>
  168. </Card>
  169. </form>
  170. );
  171. }
  172. }
  173.  
  174.  
  175. const styles = {
  176. root: {
  177. width: 400,
  178. margin: "0 auto"
  179. },
  180. };
  181.  
  182. export default withStyles(styles)(Login);
  183.  
  184.  
Advertisement
Add Comment
Please, Sign In to add comment