Advertisement
Guest User

login

a guest
Jul 22nd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. /* global require */
  2. /* global console */
  3. import React, { Component } from 'react';
  4. import PropTypes from 'prop-types';
  5. import {
  6. StyleSheet,
  7. View,
  8. ScrollView,
  9. Image,
  10. } from 'react-native';
  11. import { ScrollableTabBar } from 'react-native-scrollable-tab-view';
  12. import { Tabs, Toast } from 'native-base';
  13.  
  14. import deviceStorage from '../services/deviceStorage';
  15.  
  16. import { Form } from 'native-base';
  17. import axios from 'axios';
  18.  
  19. import Text from '../elements/Text';
  20.  
  21. import TextInput from '../elements/TextInput';
  22. import PrimeButton from '../elements/PrimeButton';
  23.  
  24. import SignUp from './login/SignUp';
  25.  
  26. import CommonStyles from '../styles/CommonStyles';
  27. import {
  28. responsiveWidth,
  29. responsiveHeight,
  30. marginHorizontal,
  31. spaceVertical,
  32. NAV_HEIGHT,
  33. STATUSBAR_HEIGHT,
  34. deviceHeight,
  35. scrollableTabHeight,
  36. colors,
  37. fontFamily,
  38. fontSize,
  39. bottomTxtHeight,
  40. btnWidth,
  41. btnHeight,
  42. inputHeight,
  43. } from '../styles/variables';
  44.  
  45. import { userIc, passIc } from '../styles/icon-variables';
  46.  
  47. const logoWidth = responsiveWidth(22.67);
  48. const logoHeight = logoWidth * 0.258;
  49.  
  50. export default class LoginScreen extends Component {
  51. constructor(props) {
  52. super(props);
  53. this.state = {
  54. signinLayout: {
  55. width: null,
  56. height: null,
  57. },
  58. signupLayout: {
  59. width: null,
  60. height: null,
  61. },
  62. logoContLayout: {
  63. width: null,
  64. height: null,
  65. }
  66. };
  67. state = {
  68. username: '',
  69. password: '',
  70. auth_token: ''
  71. }
  72. }
  73.  
  74. loginUser() {
  75. const { email, password } = this.state;
  76.  
  77. this.setState({ error: '', loading: true });
  78.  
  79. // NOTE Post to HTTPS only in production
  80. axios.post("https://api.hellobeauty.id/token",{
  81. username: email,
  82. password: password
  83. })
  84. .then((response) => {
  85. console.log(response.token);
  86. deviceStorage.saveKey("token", response.token);
  87. console.log("Logged In");
  88. this.props.navigation.navigate('InboxScreen',
  89. {jwt : response.token}
  90. );
  91. })
  92. .catch((error) => {
  93. console.log(error);
  94. this.setState({
  95. error: 'Login Failed',
  96. loading: false
  97. });
  98. });
  99. }
  100.  
  101.  
  102.  
  103. render() {
  104. const { email, password, error, loading } = this.state;
  105. const logoContHeight = this.state.logoContLayout.height;
  106. const signinHeight = this.state.signinLayout.height;
  107. const signinBottomHeight = deviceHeight - (scrollableTabHeight + signinHeight + logoContHeight);
  108.  
  109. const signInBtnSetting = {
  110. btnWidth: btnWidth.normal,
  111. btnHeight: btnHeight,
  112. style: {
  113. marginBottom: spaceVertical.semiSmall,
  114. }
  115. };
  116.  
  117. const signUpBtnSetting = {
  118. btnWidth: btnWidth.normal,
  119. btnHeight: btnHeight,
  120. backgroundColor: colors.blue,
  121. borderColor: colors.blue,
  122. };
  123.  
  124. const signupHeight = this.state.signupLayout.height;
  125. const signupBottomHeight = deviceHeight - (scrollableTabHeight + signupHeight + logoContHeight);
  126.  
  127. return (
  128. <View style={CommonStyles.container}>
  129. {/* logo start here */}
  130. <View
  131. style={styles.logo}
  132. onLayout={this.onLogoContLayout.bind(this)}
  133. >
  134. <Image
  135. source={require('../../img/imba/logo-hb.png')}
  136. style={{width: logoWidth, height: logoHeight, alignItems: "center"}}
  137. />
  138. </View>
  139. {/* logo end here */}
  140.  
  141. <Tabs
  142. initialPage={0}
  143. renderTabBar={() =>
  144. <ScrollableTabBar
  145. style={{
  146. height: scrollableTabHeight,
  147. }}
  148. tabsContainerStyle={{
  149. height: scrollableTabHeight,
  150. alignItems: 'flex-end',
  151. }}
  152. />
  153. }
  154. ref={(tabView) => { this.tabView = tabView; }}
  155. tabBarUnderlineStyle={{
  156. backgroundColor: colors.black
  157. }}
  158. tabBarBackgroundColor={colors.white}
  159. tabBarActiveTextColor={colors.black}
  160. tabBarInactiveTextColor={colors.gray}
  161. tabBarTextStyle={{
  162. fontFamily: fontFamily.medium,
  163. fontSize: fontSize.medium
  164. }}
  165. >
  166. {/* sign in form start here */}
  167. <ScrollView heading='SIGN IN'>
  168. <View onLayout={this.onSigninLayout.bind(this)}>
  169. <View style={styles.container}>
  170. <Form style={styles.form}>
  171. <TextInput
  172. inputHeight={inputHeight}
  173. onChangeText={email => this.setState({ email })}
  174. label='Email'
  175. value={email}
  176. leftIcon={require('../../img/icons/ic_user.png')}
  177. leftIconStyles={{
  178. width: userIc.width,
  179. height: userIc.height
  180. }}/>
  181. <TextInput
  182. inputHeight={inputHeight}
  183. error={this.state.errorPassword}
  184. onChangeText={password => this.setState({ password })}
  185. label='Password'
  186. value={password}
  187. leftIcon={require('../../img/icons/ic_lock.png')}
  188. leftIconStyles={{
  189. width: passIc.width,
  190. height: passIc.height
  191. }}
  192. secureTextEntry={true}/>
  193. <Text style={styles.errorTextStyle}>
  194. {error}
  195. </Text>
  196. </Form>
  197. <View style={styles.btnCont}>
  198. <PrimeButton
  199. navigation={this.props.navigation}
  200. setting={signInBtnSetting}
  201. btnText='SIGN IN'
  202. onPressButton={this.loginUser.bind(this)}
  203. />
  204. <PrimeButton
  205. navigation={this.props.navigation}
  206. setting={signUpBtnSetting}
  207. btnText='Login with Facebook'
  208. onPressButton={this.props.onPressFacebook}
  209. />
  210. </View>
  211. <View style={{paddingBottom: responsiveHeight(11.54)}}>
  212. <Text black normal regular
  213. style={{textDecorationLine: 'underline'}}
  214. onPress={this.props.onPressForgotPass}
  215. >
  216. Forgot Password?
  217. </Text>
  218. </View>
  219. </View>
  220. </View>
  221. <View style={[
  222. styles.bottomCont,
  223. signinBottomHeight > (bottomTxtHeight + spaceVertical.semiSmall)
  224. ? {height: signinBottomHeight, paddingBottom: spaceVertical.semiSmall}
  225. : {marginBottom: spaceVertical.semiSmall}
  226. ]}>
  227. <Text darkGray normal regular
  228. style={styles.bottomTxt}
  229. onPress={() => this.props.navigation.navigate('HomeScreen')}
  230. >
  231. Sign In as Guest
  232. </Text>
  233. </View>
  234. </ScrollView>
  235. {/* sign in form end here */}
  236.  
  237. {/* sign up form start here */}
  238. <ScrollView heading='SIGN UP'>
  239. <View onLayout={this.onSignupLayout.bind(this)}>
  240. <SignUp
  241. isLoading={false}
  242. onPressSignUp={this.handleSignUpWithEmailAndPassword.bind(this)}
  243. onPressSignIn={() => this.tabView.goToPage(0)}
  244. />
  245. </View>
  246. </ScrollView>
  247. {/* sign up form end here */}
  248. </Tabs>
  249. </View>
  250. );
  251. }
  252.  
  253. /**
  254. * Get size of logo container
  255. */
  256. onLogoContLayout = (e) => {
  257. this.setState({
  258. logoContLayout: {
  259. width: e.nativeEvent.layout.width,
  260. height: e.nativeEvent.layout.height,
  261. }
  262. });
  263. }
  264.  
  265. /**
  266. * Get size of sign in form
  267. */
  268. onSigninLayout = (e) => {
  269. this.setState({
  270. signinLayout: {
  271. width: e.nativeEvent.layout.width,
  272. height: e.nativeEvent.layout.height,
  273. }
  274. });
  275. }
  276.  
  277. /**
  278. * Get size of sign up form
  279. */
  280. onSignupLayout = (e) => {
  281. this.setState({
  282. signupLayout: {
  283. width: e.nativeEvent.layout.width,
  284. height: e.nativeEvent.layout.height,
  285. }
  286. });
  287. }
  288.  
  289. /**
  290. * Handle when click sign in facebook
  291. * Login facebook get token
  292. */
  293. handleSignInFacebook() {
  294. this.props.navigation.navigate('MainDrawer');
  295. }
  296.  
  297. /**
  298. * Handle when click sign in google
  299. * Login google get idToken, accessToken
  300. */
  301. handleSignInGoogle() {
  302. this.props.navigation.navigate('MainDrawer');
  303. }
  304.  
  305. /**
  306. * Handle sign in with email and password
  307. *
  308. * @param {String} email
  309. * @param {String} password
  310. */
  311. handleSignInWithEmailAndPassword(email, password) {
  312. this.props.navigation.navigate('MainDrawer');
  313. }
  314.  
  315. /**
  316. * Handle sign up with email and password
  317. *
  318. * @param {String} email
  319. * @param {String} password
  320. */
  321. handleSignUpWithEmailAndPassword(email, password) {
  322. this.props.navigation.navigate('HomeScreen');
  323. }
  324. }
  325.  
  326. const styles = StyleSheet.create({
  327. logo: {
  328. paddingTop: NAV_HEIGHT + STATUSBAR_HEIGHT,
  329. paddingBottom: responsiveHeight(10.64),
  330. marginHorizontal: marginHorizontal.large,
  331. },
  332. bottomCont: {
  333. justifyContent: 'flex-end',
  334. },
  335. bottomTxt: {
  336. textAlign: 'center',
  337. textDecorationLine: 'underline'
  338. },
  339. container: {
  340. marginHorizontal: marginHorizontal.large,
  341. },
  342. btnCont: {
  343. alignItems: 'center',
  344. paddingBottom: spaceVertical.semiSmall,
  345. },
  346. });
  347.  
  348. LoginScreen.propTypes = {
  349. navigation: PropTypes.any,
  350. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement