Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component, Fragment } from 'react';
- import { View, Text } from 'react-native';
- import { Input, TextLink, Loading, Button } from './common';
- import axios from 'axios';
- import deviceStorage from '../services/deviceStorage';
- class Login extends Component {
- constructor(props){
- super(props);
- this.state = {
- username: '',
- password: '',
- company: '',
- error: '',
- loading: false
- };
- this.loginUser = this.loginUser.bind(this);
- this.onLoginFail = this.onLoginFail.bind(this);
- }
- loginUser() {
- const { username, password, company } = this.state;
- this.setState({ error: '', loading: true });
- axios.post("https://importador.rsui.com.br/api/v1/login/",{
- username: username,
- password: password,
- company: company
- })
- .then((response) => {
- console.log(response.data.jwt);
- deviceStorage.saveKey("token", response.data.jwt);
- this.props.newJWT(response.data.jwt);
- })
- .catch((error) => {
- console.log(error);
- this.onLoginFail();
- });
- }
- onLoginFail() {
- this.setState({
- error: 'Login Failed',
- loading: false
- });
- }
- render() {
- const { username, password, company, error, loading } = this.state;
- const { form, section, errorTextStyle } = styles;
- return (
- <Fragment>
- <View style={form}>
- <View style={section}>
- <Input
- placeholder="username"
- label="Username"
- value={username}
- onChangeText={username => this.setState({ username })}
- />
- </View>
- <View style={section}>
- <Input
- secureTextEntry
- placeholder="password"
- label="Password"
- value={password}
- onChangeText={password => this.setState({ password })}
- />
- </View>
- <View style={section}>
- <Input
- placeholder="company"
- label="Company"
- value={company}
- onChangeText={company => this.setState({ company })}
- />
- </View>
- <Text style={errorTextStyle}>
- {error}
- </Text>
- {!loading ?
- <Button onPress={this.loginUser}>
- Login
- </Button>
- :
- <Loading size={'large'} />
- }
- </View>
- </Fragment>
- );
- }
- }
- const styles = {
- form: {
- width: '100%',
- borderTopWidth: 1,
- borderColor: '#ddd',
- },
- section: {
- flexDirection: 'row',
- borderBottomWidth: 1,
- backgroundColor: '#fff',
- borderColor: '#ddd',
- },
- errorTextStyle: {
- alignSelf: 'center',
- fontSize: 18,
- color: 'red'
- }
- };
- export { Login };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement