196 lines
4.9 KiB
JavaScript
196 lines
4.9 KiB
JavaScript
import React from 'react-native';
|
|
import GlobalStyles from '../../settings/styles';
|
|
import TokenHelp from '../login/token-help';
|
|
import RouteMaster from './RouteMaster';
|
|
import { checkToken } from '../../api/CircleCI';
|
|
import { set } from '../../api/token';
|
|
import loaderHandler from 'react-native-busy-indicator/LoaderHandler';
|
|
import BusyIndicator from 'react-native-busy-indicator';
|
|
|
|
const dismissKeyboard = require('dismissKeyboard');
|
|
|
|
const {
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
TextInput,
|
|
TouchableHighlight,
|
|
ScrollView,
|
|
BackAndroid,
|
|
TouchableWithoutFeedback,
|
|
Alert,
|
|
Image
|
|
} = React;
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
flex: 1,
|
|
backgroundColor: GlobalStyles.get('CIRCLE_BG'),
|
|
},
|
|
contentWrapper: {
|
|
flex: 0.85,
|
|
justifyContent: 'flex-start',
|
|
alignItems: 'center'
|
|
},
|
|
input: {
|
|
marginHorizontal: 10,
|
|
fontSize: 15,
|
|
textAlign: 'center'
|
|
},
|
|
loginButton: {
|
|
flex: 0.92,
|
|
backgroundColor: GlobalStyles.get('CIRCLE_GREEN'),
|
|
borderColor: GlobalStyles.get('CIRCLE_GREEN'),
|
|
},
|
|
button: {
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
padding: 10,
|
|
borderWidth: 2,
|
|
borderRadius: 12,
|
|
margin: 5
|
|
},
|
|
buttonText: {
|
|
fontSize: 18,
|
|
color: '#FFF'
|
|
},
|
|
buttonContainer: {
|
|
flexDirection: 'row',
|
|
justifyContent: 'center',
|
|
margin: 5
|
|
},
|
|
heading: {
|
|
fontSize: 18,
|
|
margin: 8,
|
|
marginTop: 20
|
|
},
|
|
helpText: {
|
|
fontSize: 11,
|
|
fontStyle: 'italic',
|
|
padding: 5,
|
|
marginBottom: 50
|
|
},
|
|
text: {
|
|
color: GlobalStyles.get('CIRCLE_TEXT')
|
|
},
|
|
icon: {
|
|
flex: 1,
|
|
margin: 15,
|
|
marginVertical: 35
|
|
}
|
|
});
|
|
|
|
export default class Login extends React.Component {
|
|
constructor() {
|
|
super();
|
|
this.state = {
|
|
token: '',
|
|
showTokenHelp: false
|
|
};
|
|
this.validate = this.validate.bind(this);
|
|
this.textChanged = this.textChanged.bind(this);
|
|
this.showTokenHelp = this.showTokenHelp.bind(this);
|
|
this.hideTokenHelp = this.hideTokenHelp.bind(this);
|
|
this.showAbout = this.showAbout.bind(this);
|
|
this.proceed = this.proceed.bind(this);
|
|
}
|
|
|
|
textChanged(token) {
|
|
this.setState({ token });
|
|
}
|
|
|
|
proceed() {
|
|
this.hideTokenHelp();
|
|
this.props.nav.push(RouteMaster.get('HOME'));
|
|
}
|
|
|
|
validate() {
|
|
loaderHandler.showLoader('Loading');
|
|
dismissKeyboard();
|
|
checkToken(this.state.token).then(function (isValid) {
|
|
if (isValid) {
|
|
Alert.alert('Valid',
|
|
'Token accepted!',
|
|
[ {text: 'OK' }]);
|
|
set(this.state.token).then(this.proceed);
|
|
} else {
|
|
Alert.alert('Invalid',
|
|
'Token not accepted!',
|
|
[ {text: 'Return' }]);
|
|
this.setState({
|
|
token: ''
|
|
});
|
|
}
|
|
loaderHandler.hideLoader();
|
|
}.bind(this));
|
|
}
|
|
|
|
showAbout() {
|
|
this.props.nav.push(RouteMaster.get('INFO'));
|
|
}
|
|
|
|
showTokenHelp() {
|
|
dismissKeyboard();
|
|
this.setState({
|
|
showTokenHelp: true
|
|
});
|
|
}
|
|
|
|
hideTokenHelp() {
|
|
this.setState({
|
|
showTokenHelp: false
|
|
});
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
BackAndroid.removeEventListener('hardwareBackPress', () => { return true; }); // Re-enable back button
|
|
}
|
|
|
|
componentWillMount() {
|
|
BackAndroid.addEventListener('hardwareBackPress', () => { return true; }); // Disable back button
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<View style={styles.container}>
|
|
<ScrollView keyboardShouldPersistTaps={false} contentContainerStyle={styles.container}>
|
|
<View style={styles.contentWrapper}>
|
|
<Image
|
|
style={styles.icon}
|
|
source={require('../../img/circle-ci-logo@light.png')}
|
|
resizeMode="contain"/>
|
|
<Text style={[styles.heading, styles.text]}>
|
|
Please enter your CircleCI token
|
|
</Text>
|
|
<TextInput
|
|
style={[styles.input, styles.text]}
|
|
onChangeText={this.textChanged}
|
|
value={this.state.token}
|
|
onSubmitEditing={this.validate}
|
|
placeholder="CircleCI Token"
|
|
placeholderTextColor="#A7A7A7"
|
|
autoCorrect={false}
|
|
/>
|
|
<TouchableWithoutFeedback onPress={this.showTokenHelp}>
|
|
<Text style={[styles.helpText, styles.text]}>
|
|
What's this?
|
|
</Text>
|
|
</TouchableWithoutFeedback>
|
|
</View>
|
|
</ScrollView>
|
|
<View style={styles.buttonContainer}>
|
|
<TouchableHighlight
|
|
style={[styles.button, styles.loginButton]}
|
|
onPress={this.validate}
|
|
underlayColor={GlobalStyles.get('CIRCLE_BG')}>
|
|
<Text style={[styles.text, styles.buttonText]}>
|
|
Authenticate
|
|
</Text>
|
|
</TouchableHighlight>
|
|
</View>
|
|
<TokenHelp open={this.state.showTokenHelp} onClose={this.hideTokenHelp} />
|
|
<BusyIndicator />
|
|
</View>
|
|
);
|
|
}
|
|
};
|