Guest User

Untitled

a guest
Dec 5th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.56 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { GlobalStyles } from "../../resources/style/style";
  3. import {
  4. Platform,
  5. StyleSheet,
  6. Text,
  7. View,
  8. Button,
  9. TouchableOpacity,
  10. ImageBackground,
  11. TextInput,
  12. ScrollView,
  13. AsyncStorage,
  14. Keyboard,
  15. PixelRatio,
  16. Dimensions,
  17. ActivityIndicator,
  18. Alert,
  19. Image
  20. } from "react-native";
  21.  
  22. import * as Animatable from "react-native-animatable";
  23. import i18n from "../../AppGlobalConfig/Localization/i18n";
  24. import { Login } from "../../services/UserService.js";
  25.  
  26. import { Form } from "native-base";
  27. import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view";
  28. import Email from "../UtilsComponents/Inputs/Email";
  29. import Password from "../UtilsComponents/Inputs/Password";
  30. import Btn from "../UtilsComponents/Buttons/Button";
  31.  
  32. export default class LoginSiginView extends Component {
  33. constructor(props) {
  34. super(props);
  35.  
  36. this.state = {
  37. inputs: []
  38. };
  39.  
  40. this.state.login_enabled = false;
  41. this.state.formError = "";
  42. }
  43.  
  44. changeInputFocus = index => () => {
  45. if (index === this.state.inputs.length - 1) {
  46. this.Login();
  47. return;
  48. }
  49.  
  50. if (index === 0) {
  51. // console.warn(this.state.inputs[index + 1]);
  52. this.state.inputs[index + 1].state.inputRef.focus(); // eslint-disable-line
  53. } else this.state.inputs[index + 1].state.inputRef.focus();
  54. };
  55.  
  56. updateCanRegisterState = input => {
  57. console.warn("update", input);
  58. };
  59.  
  60. updateCanRegisterStateEmail = input => {
  61. console.warn("email", this.state.inputs[0].state.isCorrect);
  62. console.warn("message", this.state.inputs[0].state.error);
  63. };
  64.  
  65. errorTemplate = () => {
  66. if (this.state.formError != "") {
  67. return (
  68. <View style={[GlobalStyles.containerRow]}>
  69. <Text
  70. style={[
  71. { color: "red", paddingBottom: 18 },
  72. GlobalStyles.mediumText
  73. ]}
  74. >
  75. {this.state.formError}
  76. </Text>
  77. </View>
  78. );
  79. } else {
  80. return null;
  81. }
  82. };
  83.  
  84. noAvaiblable = () => {
  85. alert(i18n.t("globals.no-available"));
  86. };
  87.  
  88. show_loading = () => {
  89. if (this.state.loading) {
  90. return (
  91. <View style={[Activity.container, Activity.horizontal]}>
  92. <ActivityIndicator size="large" color="#FFFFFF" />
  93. </View>
  94. );
  95. }
  96. };
  97.  
  98. goToHome = () => {
  99. this.props.navigation.navigate("AuthLoading");
  100. };
  101. goToRegister = () => {
  102. this.props.navigation.navigate("Register");
  103. };
  104.  
  105. Login = async () => {
  106. Keyboard.dismiss();
  107.  
  108. if (!this.state.inputs[0].state.isCorrect) {
  109. Alert.alert(
  110. i18n.t("login.email_error_title"),
  111. i18n.t("login.email_error"),
  112. [
  113. {
  114. text: "OK",
  115. onPress: () => this.state.inputs[0].state.inputRef.focus()
  116. }
  117. ],
  118. { cancelable: false }
  119. );
  120. return;
  121. }
  122.  
  123. if (
  124. !this.state.inputs[1].state.isCorrect ||
  125. this.state.inputs[1].state.value.length < 8
  126. ) {
  127. Alert.alert(
  128. i18n.t("login.password_error_title"),
  129. i18n.t("login.password_error"),
  130. [
  131. {
  132. text: "OK",
  133. onPress: () => this.state.inputs[1].state.inputRef.focus()
  134. }
  135. ],
  136. { cancelable: false }
  137. );
  138. return;
  139. }
  140.  
  141. if (
  142. !this.state.inputs[0].state.isCorrect ||
  143. !this.state.inputs[1].state.isCorrect
  144. ) {
  145. Alert.alert(
  146. i18n.t("login.fill_form_title"),
  147. i18n.t("login.fill_form"),
  148. [
  149. {
  150. text: "OK",
  151. onPress: () => this.state.inputs[0].state.inputRef.focus()
  152. }
  153. ],
  154. { cancelable: false }
  155. );
  156. } else {
  157. this.setState({ loading: true });
  158.  
  159. try {
  160. const data = {
  161. username: this.state.inputs[0].state.value,
  162. password: this.state.inputs[1].state.value
  163. };
  164.  
  165. let response = await Login(data);
  166. console.warn("status", response.status);
  167.  
  168. switch (response.status) {
  169. case 500:
  170. Alert.alert(
  171. i18n.t("errors.sorry"),
  172. i18n.t("errors.500"),
  173. [
  174. {
  175. text: "OK",
  176. onPress: () => this.state.inputs[0].state.inputRef.focus()
  177. }
  178. ],
  179. { cancelable: false }
  180. );
  181. break;
  182. case 404:
  183. console.warn("No found");
  184. break;
  185. case 401:
  186. //this.setState({formError: i18n.t("login.invalid_username_password")});
  187. Alert.alert(
  188. i18n.t("login.invalid_username_password_title"),
  189. i18n.t("login.invalid_username_password"),
  190. [
  191. {
  192. text: "OK",
  193. onPress: () => this.state.inputs[0].state.inputRef.focus()
  194. }
  195. ],
  196. { cancelable: false }
  197. );
  198. this.setState({ loading: false });
  199. break;
  200. case 200:
  201. let session = await response.json();
  202. console.warn("session", session);
  203. await AsyncStorage.setItem("userToken", JSON.stringify(session));
  204. this.setState({ loading: false });
  205. this.goToHome();
  206. break;
  207. }
  208. } catch (e) {
  209. console.warn("error fetch", e);
  210. this.setState({ loading: false });
  211. }
  212. }
  213. };
  214.  
  215. render() {
  216. return (
  217. <ImageBackground
  218. style={[GlobalStyles.containerRow, GlobalStyles.backgroundColor]}
  219. source={require("../../resources/img//background.png")}
  220. >
  221. <KeyboardAwareScrollView
  222. style={GlobalStyles.containerForm}
  223. scrollEnabled={false}
  224. >
  225. <Animatable.View
  226. animation="fadeInUp"
  227. delay={1200}
  228. duration={700}
  229. style={[styles.container]}
  230. >
  231. <View
  232. style={[GlobalStyles.containerRow, { padding: 7 * this.pxr }]}
  233. >
  234. <View
  235. style={{
  236. width: 50,
  237. height: 58,
  238. alignItems: "center",
  239. justifyContent: "center"
  240. }}
  241. >
  242. <Image
  243. style={{
  244. height: "80%",
  245. width: "80%"
  246. }}
  247. source={require("../../resources/img/logo-aff.png")}
  248. />
  249. </View>
  250. </View>
  251. <View style={[GlobalStyles.containerRow]}>
  252. <Text style={[{ color: "#fff" }, GlobalStyles.titleText_android]}>
  253. Log In
  254. </Text>
  255.  
  256. <Text
  257. style={[
  258. { color: "#fff", paddingBottom: 4 * this.pxr },
  259. GlobalStyles.mediumText
  260. ]}
  261. >
  262. Vivamus sed vestibulum veilt
  263. </Text>
  264. </View>
  265. {this.state.formError != "" ? this.errorTemplate() : null}
  266. <Form>
  267. <View style={GlobalStyles.containerRow}>
  268. <Text style={GlobalStyles.generalText}>
  269. {i18n.t("login.user-email")}
  270. </Text>
  271. <Email
  272. changeFocus={this.changeInputFocus(0)}
  273. update={this.updateCanRegisterStateEmail}
  274. special
  275. ref={ref => {
  276. this.state.inputs[0] = ref;
  277. }}
  278. placeholderTextColor="white"
  279. />
  280. </View>
  281. <View
  282. style={[
  283. GlobalStyles.containerRow,
  284. { marginTop: 3.5 * this.pxr }
  285. ]}
  286. >
  287. <Text style={GlobalStyles.generalText}>
  288. {i18n.t("login.password")}
  289. </Text>
  290. <Password
  291. changeFocus={this.changeInputFocus(1)}
  292. update={this.updateCanRegisterState}
  293. returnKeyType="go"
  294. special
  295. ref={ref => {
  296. this.state.inputs[1] = ref;
  297. }}
  298. placeholderTextColor="white"
  299. />
  300. </View>
  301. <View style={GlobalStyles.containerRow}>
  302. {this.state.loading ? (
  303. this.show_loading()
  304. ) : (
  305. <Btn
  306. onPress={this.Login}
  307. text={i18n.t("login.login-button")}
  308. customStyle={{
  309. borderRadius: 30,
  310. flexDirection: "row",
  311. width: "80%",
  312. marginTop: 10,
  313. width: "100%",
  314. color: "#828282",
  315. backgroundColor: "#fff"
  316. }}
  317. />
  318. )}
  319. </View>
  320. </Form>
  321. <View
  322. style={[
  323. GlobalStyles.containerRow,
  324. { paddingBottom: 30, marginTop: 5 }
  325. ]}
  326. >
  327. <TouchableOpacity onPress={this.goToRegister}>
  328. <Text
  329. style={[
  330. {
  331. borderRadius: 0,
  332. flexDirection: "row",
  333. justifyContent: "flex-start",
  334. alignSelf: "flex-start",
  335. alignSelf: "center",
  336. textDecorationLine: "underline",
  337. marginTop: 4
  338. },
  339. styles.textSocial
  340. ]}
  341. >
  342. {i18n.t("login.forgot-password")}
  343. </Text>
  344. </TouchableOpacity>
  345. </View>
  346. <View style={{ flex: 1, flexDirection: "row" }}>
  347. <TouchableOpacity
  348. style={[styles.socialMedia, styles.socialMediaGoogle]}
  349. >
  350. <View
  351. style={{
  352. width: "15%",
  353. paddingTop: 7,
  354. paddingLeft: 5
  355. }}
  356. >
  357. <Image
  358. style={[
  359. GlobalStyles.containerRow,
  360. GlobalStyles.backgroundColor
  361. ]}
  362. source={require("../../resources/img/google.png")}
  363. style={{ width: 12, height: 12 }}
  364. />
  365. </View>
  366. <View style={{ width: "85%" }}>
  367. <Text
  368. style={[
  369. styles.textSocial,
  370. {
  371. textAlign: "left",
  372. alignSelf: "flex-start",
  373. paddingLeft: 10,
  374. paddingTop: 1
  375. }
  376. ]}
  377. >
  378. {i18n.t("login.login_google")}
  379. </Text>
  380. </View>
  381. </TouchableOpacity>
  382.  
  383. <TouchableOpacity
  384. style={[styles.socialMedia, styles.socialMediaFacebook]}
  385. >
  386. <View
  387. style={{
  388. width: "85%"
  389. }}
  390. >
  391. <Text
  392. style={[
  393. styles.textSocial,
  394. { textAlign: "right", paddingTop: 1 }
  395. ]}
  396. >
  397. {i18n.t("login.login_facebook")}
  398. </Text>
  399. </View>
  400. <View
  401. style={{
  402. width: "15%",
  403. padding: 6
  404. }}
  405. >
  406. <Image
  407. style={[
  408. GlobalStyles.containerRow,
  409. GlobalStyles.backgroundColor,
  410. { paddingTop: 3.5 * this.pxr }
  411. ]}
  412. source={require("../../resources/img/facebook.png")}
  413. style={{ width: 5, height: 14 }}
  414. />
  415. </View>
  416. </TouchableOpacity>
  417. </View>
  418. <View
  419. style={[
  420. GlobalStyles.containerRow,
  421. { paddingTop: 40, paddingBottom: 10 }
  422. ]}
  423. >
  424. <Text style={{ color: "#fff", fontSize: 14 }}>
  425. {i18n.t("login.any-account")}
  426. </Text>
  427. </View>
  428. <View style={{ flex: 1, flexDirection: "row" }}>
  429. <View style={{ width: "8%" }} />
  430. <View
  431. style={{
  432. width: "20%",
  433. marginleft: 80,
  434. borderTop: "solid",
  435. borderColor: "#fff",
  436. borderTopWidth: 1,
  437. marginTop: 12
  438. }}
  439. />
  440. <View style={{ width: "40%" }}>
  441. <TouchableOpacity
  442. style={styles.singIn}
  443. onPress={this.goToRegister}
  444. >
  445. <Text style={styles.textSignIn}>
  446. {i18n.t("login.sign-in")}
  447. </Text>
  448. </TouchableOpacity>
  449. </View>
  450. <View
  451. style={{
  452. width: "20%",
  453. marginleft: 80,
  454. borderTop: "solid",
  455. borderColor: "#fff",
  456. borderTopWidth: 1,
  457. marginTop: 12
  458. }}
  459. />
  460. </View>
  461. <View style={{ width: "8%" }} />
  462. </Animatable.View>
  463. </KeyboardAwareScrollView>
  464. </ImageBackground>
  465. );
  466. }
  467. }
  468. const styles = StyleSheet.create({
  469. socialMedia: {
  470. width: "45%",
  471. height: 30,
  472. borderColor: "#fff",
  473. borderWidth: 1,
  474. flex: 1,
  475. flexDirection: "row"
  476. },
  477. socialMediaFacebook: {
  478. borderLeftColor: "transparent",
  479. borderLeftWidth: 0,
  480. borderTopRightRadius: 5,
  481. borderBottomRightRadius: 5
  482. },
  483. socialMediaGoogle: {
  484. borderTopLeftRadius: 5,
  485. borderBottomLeftRadius: 5
  486. },
  487. textSocial: {
  488. color: "#fff",
  489. fontSize: GlobalStyles.mediumText.fontSize,
  490. alignSelf: "center",
  491. marginTop: 4
  492. },
  493. textSignIn: {
  494. color: "#fff",
  495. alignSelf: "center",
  496. fontSize: GlobalStyles.regularText_android.fontSize
  497. },
  498. singIn: {
  499. borderRadius: 20,
  500. borderColor: "#fff",
  501. borderWidth: 1,
  502. padding: 3,
  503. width: "100%",
  504. alignItems: "center",
  505. justifyContent: "center"
  506. },
  507. imagenSocial: { width: 10, height: 10, flexDirection: "row" }
  508. });
  509.  
  510. const Activity = StyleSheet.create({
  511. container: {
  512. flex: 1,
  513. justifyContent: "center",
  514. alignItems: "center"
  515. },
  516. horizontal: {
  517. flexDirection: "row",
  518. justifyContent: "space-around",
  519. padding: 10
  520. }
  521. });
Add Comment
Please, Sign In to add comment