Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. import React from 'react';
  2. import {StyleSheet, Text, View, TextInput, ImageBackground, Image, AsyncStorage, Alert, Keyboard, TouchableOpacity} from 'react-native';
  3.  
  4. import {Card, Button, FormInput, Icon} from 'react-native-elements';
  5. import { Ionicons } from '@expo/vector-icons';
  6. import Welcome from "../Welcome";
  7.  
  8.  
  9. export default class Login extends React.Component {
  10.  
  11. constructor(props){
  12. super(props);
  13. this.state = {
  14. accounts: [{
  15. accountId: '',
  16. username: '',
  17. password: '',
  18. status: '',
  19. accountNumber: ''
  20.  
  21. }]
  22. }
  23. }
  24.  
  25. updateValue(text, field) {
  26. if (field == 'username') {
  27. this.setState({
  28. username: text,
  29. })
  30. } else if (field == 'password') {
  31. this.setState({
  32. password: text
  33. })
  34. }else if (field == 'accountId') {
  35. this.setState({
  36. accountId: text
  37. })
  38. }
  39. }
  40.  
  41. submit()
  42. {
  43. let accounts={}
  44. accounts.accountId=this.state.accountId,
  45. accounts.username=this.state.username,
  46. accounts.password=this.state.password
  47.  
  48.  
  49. var url = 'http://192.168.1.22:8080/login';
  50.  
  51. fetch(url, {
  52. method: 'POST', // or 'PUT'
  53. body: JSON.stringify(accounts), // data can be `string` or {object}!
  54. headers:{
  55. 'Content-Type': 'application/json'
  56. }
  57. }).then(res => res.json())
  58. .then((response) =>{
  59. console.log('Status:', JSON.stringify(response.status));
  60. if (response.status == '200') {
  61. console.log(response.values);
  62. AsyncStorage.setItem('accountId', response.values.accountId+'');
  63. AsyncStorage.setItem('status', response.status);
  64. AsyncStorage.setItem('username', response.values.username);
  65. AsyncStorage.setItem('balance', response.values.balance+'');
  66. AsyncStorage.setItem('accountNumber', response.values.accountNumber+'');
  67.  
  68. this.props.navigation.navigate('Welcome');
  69. }
  70. else if(response.status == '303'){
  71. this.setState({spinner: false});
  72. setTimeout(()=> {
  73. Alert.alert('Warning', 'Sorry, your account has not been confirmed !')
  74. })
  75. }
  76. else{
  77.  
  78. this.setState({ spinner: false });
  79. setTimeout(() => {
  80. Alert.alert('Warning','Username / Password Salah!');
  81. }, 10);
  82. }
  83. })
  84. .catch(error => Alert.alert("Error", "Sorry, Network Error or server is busy, Please Try Again"));
  85. }
  86.  
  87.  
  88. render() {
  89.  
  90. return (
  91. <View style={styles.container}>
  92. <Image style={styles.logo}
  93. source={require ('../image/logo.jpg')}/>
  94. <View title={"Login Account"}>
  95. <Text style={styles.text}>Sign-In</Text>
  96. <TextInput
  97. style={styles.input}
  98. label='Email'
  99. labelActiveColor={'#fff'}
  100. placeholder={'Enter Your Email'}
  101. onChangeText={(text) => this.updateValue(text, 'username')}
  102. keyboardType={"email-address"}
  103. />
  104. <TextInput
  105. style={styles.input}
  106. label='Password'
  107. labelActiveColor={'#fff'}
  108. placeholder={'Enter Your Password'}
  109. onChangeText={(text) => this.updateValue(text, 'password')}
  110. secureTextEntry={true}
  111. />
  112. <Button buttonStyle={styles.submitButton}
  113. title={'Login'}
  114. onPress={() => this.submit()}
  115. />
  116. </View>
  117. <Text style={styles.text1}>Don't have an Account ? </Text>
  118. <TouchableOpacity
  119. onPress={()=> this.props.navigation.navigate('Register')}>
  120. <Text style={styles.text2}>Sign-Up</Text>
  121. </TouchableOpacity>
  122. </View>
  123. )
  124. }
  125. }
  126.  
  127. const styles = StyleSheet.create({
  128. container: {
  129. flex: 1,
  130. backgroundColor: '#EAFFDA',
  131. padding: 2,
  132. },
  133. logo: {
  134. margin: 10,
  135. marginTop: 40,
  136. backgroundColor: '#f8fff9',
  137. borderRadius: 100,
  138. borderColor: '#0A917F',
  139. width: 200,
  140. height: 200,
  141. alignSelf: 'center',
  142. opacity: 50,
  143. borderWidth: 4
  144. },
  145. input: {
  146. height: 45,
  147. borderRadius: 45,
  148. margin: 10,
  149. paddingLeft: 35,
  150. paddingRight: 35,
  151. backgroundColor: '#fff',
  152. color: '#0A917F',
  153. opacity: 40,
  154. fontSize: 17,
  155. },
  156. submitButton: {
  157. backgroundColor: '#0A917F',
  158. padding: 10,
  159. marginTop: 15,
  160. height: 45,
  161. alignItems: 'flex-end',
  162. borderRadius: 25,
  163. justifyContent: 'center'
  164. },
  165. submitButtonText: {
  166. color: 'white',
  167. textAlign: 'center'
  168. },
  169. picker: {
  170. height: 45,
  171. borderRadius: 45,
  172. margin: 10,
  173. paddingLeft: 35,
  174. paddingRight: 35,
  175. backgroundColor: '#fff',
  176. color: '#0A917F',
  177. marginTop: 30
  178. },
  179. text: {
  180. fontSize: 30,
  181. color: '#0A917F',
  182. alignSelf: 'center',
  183. marginTop: 20,
  184. marginBottom: 25
  185. },
  186. text1: {
  187. fontSize: 20,
  188. color: '#0A917F',
  189. alignSelf: 'center',
  190. marginTop: 40,
  191. },
  192. text2: {
  193. fontSize: 25,
  194. color: '#0A917F',
  195. alignSelf: 'center',
  196. },
  197. searchSection: {
  198. flex: 1,
  199. flexDirection: 'row',
  200. justifyContent: 'center',
  201. alignItems: 'center',
  202. backgroundColor: '#fff',
  203. },
  204. searchIcon: {
  205. padding: 10,
  206. },
  207. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement