Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import React, { Component, Fragment } from 'react';
  2. import { View, Text } from 'react-native';
  3. import { Input, TextLink, Loading, Button } from './common';
  4. import axios from 'axios';
  5. import deviceStorage from '../services/deviceStorage';
  6.  
  7. class Login extends Component {
  8. constructor(props){
  9. super(props);
  10. this.state = {
  11. username: '',
  12. password: '',
  13. company: '',
  14. error: '',
  15. loading: false
  16. };
  17.  
  18. this.loginUser = this.loginUser.bind(this);
  19. this.onLoginFail = this.onLoginFail.bind(this);
  20. }
  21.  
  22. loginUser() {
  23. const { username, password, company } = this.state;
  24.  
  25. this.setState({ error: '', loading: true });
  26.  
  27. axios.post("https://importador.rsui.com.br/api/v1/login/",{
  28. username: username,
  29. password: password,
  30. company: company
  31. })
  32. .then((response) => {
  33. console.log(response.data.jwt);
  34. deviceStorage.saveKey("token", response.data.jwt);
  35. this.props.newJWT(response.data.jwt);
  36. })
  37. .catch((error) => {
  38. console.log(error);
  39. this.onLoginFail();
  40. });
  41. }
  42.  
  43. onLoginFail() {
  44. this.setState({
  45. error: 'Login Failed',
  46. loading: false
  47. });
  48. }
  49.  
  50. render() {
  51. const { username, password, company, error, loading } = this.state;
  52. const { form, section, errorTextStyle } = styles;
  53.  
  54. return (
  55. <Fragment>
  56. <View style={form}>
  57. <View style={section}>
  58. <Input
  59. placeholder="username"
  60. label="Username"
  61. value={username}
  62. onChangeText={username => this.setState({ username })}
  63. />
  64. </View>
  65.  
  66. <View style={section}>
  67. <Input
  68. secureTextEntry
  69. placeholder="password"
  70. label="Password"
  71. value={password}
  72. onChangeText={password => this.setState({ password })}
  73. />
  74. </View>
  75.  
  76. <View style={section}>
  77. <Input
  78. placeholder="company"
  79. label="Company"
  80. value={company}
  81. onChangeText={company => this.setState({ company })}
  82. />
  83. </View>
  84.  
  85. <Text style={errorTextStyle}>
  86. {error}
  87. </Text>
  88.  
  89. {!loading ?
  90. <Button onPress={this.loginUser}>
  91. Login
  92. </Button>
  93. :
  94. <Loading size={'large'} />
  95. }
  96.  
  97. </View>
  98. </Fragment>
  99. );
  100. }
  101. }
  102.  
  103. const styles = {
  104. form: {
  105. width: '100%',
  106. borderTopWidth: 1,
  107. borderColor: '#ddd',
  108. },
  109. section: {
  110. flexDirection: 'row',
  111. borderBottomWidth: 1,
  112. backgroundColor: '#fff',
  113. borderColor: '#ddd',
  114. },
  115. errorTextStyle: {
  116. alignSelf: 'center',
  117. fontSize: 18,
  118. color: 'red'
  119. }
  120. };
  121.  
  122. export { Login };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement