Advertisement
Guest User

signin

a guest
Jul 22nd, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. /* global require */
  2. import React, { Component } from 'react';
  3. import {
  4. StyleSheet,
  5. View,
  6. ActivityIndicator,
  7. Alert
  8. } from 'react-native';
  9. import { NavigationActions } from 'react-navigation';
  10. import { Form } from 'native-base';
  11. import PropTypes from 'prop-types';
  12. import deviceStorage from '../../services/deviceStorage';
  13. import Text from '../../elements/Text';
  14. import PrimeButton from '../../elements/PrimeButton';
  15. import TextInput from '../../elements/TextInput';
  16.  
  17. import axios from 'axios';
  18.  
  19. import {Actions} from 'react-native-router-flux';
  20.  
  21.  
  22. import {
  23. btnWidth,
  24. btnHeight,
  25. inputHeight,
  26. marginHorizontal,
  27. spaceVertical,
  28. responsiveWidth,
  29. responsiveHeight,
  30. colors,
  31. } from '../../styles/variables';
  32.  
  33. import { userIc, passIc } from '../../styles/icon-variables';
  34.  
  35. export default class SignIn extends Component {
  36. constructor(props) {
  37. super(props);
  38. this.state = {
  39. email: '',
  40. password: '',
  41. error: '',
  42. loading: false
  43. };
  44. }
  45.  
  46. loginUser() {
  47. const { email, password } = this.state;
  48.  
  49. this.setState({ error: '', loading: true });
  50.  
  51. // NOTE Post to HTTPS only in production
  52. axios.post("https://api.hellobeauty.id/token",{
  53. username: email,
  54. password: password
  55. })
  56. .then((response) => {
  57. deviceStorage.saveKey("id_token", response.token);
  58. this.props.navigation.navigate('HomeScreen',
  59. {jwt : response.token}
  60. );
  61. })
  62. .catch((error) => {
  63. console.log(error);
  64. this.setState({
  65. error: 'Login Failed',
  66. loading: false
  67. });
  68. });
  69. }
  70.  
  71. render() {
  72. const { email, password, error, loading } = this.state;
  73. const { form, section, errorTextStyle } = styles;
  74.  
  75. const signInBtnSetting = {
  76. btnWidth: btnWidth.normal,
  77. btnHeight: btnHeight,
  78. style: {
  79. marginBottom: spaceVertical.semiSmall,
  80. }
  81. };
  82.  
  83. const signUpBtnSetting = {
  84. btnWidth: btnWidth.normal,
  85. btnHeight: btnHeight,
  86. backgroundColor: colors.blue,
  87. borderColor: colors.blue,
  88. };
  89.  
  90. if (this.props.isLoading) {
  91. return (
  92. <View style={[styles.container, styles.loading]}>
  93. <ActivityIndicator />
  94. </View>
  95. );
  96. }
  97.  
  98. return (
  99. <View style={styles.container}>
  100. <Form style={styles.form}>
  101. <TextInput
  102. inputHeight={inputHeight}
  103. onChangeText={email => this.setState({ email })}
  104. label='Email'
  105. value={email}
  106. leftIcon={require('../../../img/icons/ic_user.png')}
  107. leftIconStyles={{
  108. width: userIc.width,
  109. height: userIc.height
  110. }}
  111.  
  112. />
  113. <TextInput
  114. inputHeight={inputHeight}
  115. error={this.state.errorPassword}
  116. onChangeText={password => this.setState({ password })}
  117. label='Password'
  118. value={password}
  119. leftIcon={require('../../../img/icons/ic_lock.png')}
  120. leftIconStyles={{
  121. width: passIc.width,
  122. height: passIc.height
  123. }}
  124. secureTextEntry={true}
  125. />
  126.  
  127. <Text style={styles.errorTextStyle}>
  128. {error}
  129. </Text>
  130.  
  131. </Form>
  132. <View style={styles.btnCont}>
  133. <PrimeButton
  134. navigation={this.props.navigation}
  135. setting={signInBtnSetting}
  136. btnText='SIGN IN'
  137. onPressButton={this.loginUser.bind(this)}
  138. />
  139. <PrimeButton
  140. navigation={this.props.navigation}
  141. setting={signUpBtnSetting}
  142. btnText='Login with Facebook'
  143. onPressButton={this.props.onPressFacebook}
  144. />
  145. </View>
  146. <View style={{paddingBottom: responsiveHeight(11.54)}}>
  147. <Text black normal regular
  148. style={{textDecorationLine: 'underline'}}
  149. onPress={this.props.onPressForgotPass}
  150. >
  151. Forgot Password?
  152. </Text>
  153. </View>
  154. </View>
  155. );
  156. }
  157.  
  158. /**
  159. * Handle when click sign in button
  160. */
  161. handleOnPressSignIn() {
  162. const {
  163. email,
  164. password,
  165. } = this.state;
  166. let isError = false;
  167. if (email == '') {
  168. this.setState({errorEmail: true});
  169. isError = true;
  170. }
  171. if (password == '') {
  172. this.setState({errorPassword: true});
  173. isError = true;
  174. }
  175. if (isError) {
  176. return;
  177. }
  178. this.props.onPressSignIn(email, password);
  179. }
  180. }
  181.  
  182. const styles = StyleSheet.create({
  183. container: {
  184. marginHorizontal: marginHorizontal.large,
  185. },
  186. form: {
  187. paddingTop: spaceVertical.small,
  188. paddingBottom: responsiveHeight(6)
  189. },
  190. btnCont: {
  191. alignItems: 'center',
  192. paddingBottom: spaceVertical.semiSmall,
  193. },
  194. errorTextStyle: {
  195. alignSelf: 'center',
  196. fontSize: 18,
  197. color: 'red'
  198. }
  199. });
  200.  
  201. SignIn.propTypes = {
  202. navigation: PropTypes.any,
  203. onPressSignIn: PropTypes.func,
  204. onPressSignUp: PropTypes.func,
  205. onPressForgotPass: PropTypes.func,
  206. onPressFacebook: PropTypes.func,
  207. onPressGoogle: PropTypes.func,
  208. isLoading: PropTypes.bool,
  209. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement