Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginScreen.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // @flow
  2. import * as React from 'react';
  3. import {Animated, KeyboardAvoidingView, StyleSheet, View} from "react-native";
  4. import {Avatar, Button, Card, HelperText, Paragraph, TextInput, withTheme} from 'react-native-paper';
  5. import ConnectionManager from "../../managers/ConnectionManager";
  6. import i18n from 'i18n-js';
  7. import ErrorDialog from "../../components/Dialogs/ErrorDialog";
  8. import {withCollapsible} from "../../utils/withCollapsible";
  9. import {Collapsible} from "react-navigation-collapsible";
  10. import CustomTabBar from "../../components/Tabbar/CustomTabBar";
  11. import type {CustomTheme} from "../../managers/ThemeManager";
  12. import {Linking} from "expo";
  13. type Props = {
  14. navigation: Object,
  15. route: Object,
  16. collapsibleStack: Collapsible,
  17. theme: CustomTheme
  18. }
  19. type State = {
  20. email: string,
  21. password: string,
  22. isEmailValidated: boolean,
  23. isPasswordValidated: boolean,
  24. loading: boolean,
  25. dialogVisible: boolean,
  26. dialogError: number,
  27. }
  28. const ICON_AMICALE = require('../../../assets/amicale.png');
  29. const RESET_PASSWORD_PATH = "https://www.amicale-insat.fr//password/reset";
  30. const emailRegex = /^.+@.+\..+$/;
  31. class LoginScreen extends React.Component<Props, State> {
  32. state = {
  33. email: '',
  34. password: '',
  35. isEmailValidated: false,
  36. isPasswordValidated: false,
  37. loading: false,
  38. dialogVisible: false,
  39. dialogError: 0,
  40. };
  41. onEmailChange: Function;
  42. onPasswordChange: Function;
  43. passwordInputRef: Object;
  44. constructor(props) {
  45. super(props);
  46. this.onEmailChange = this.onInputChange.bind(this, true);
  47. this.onPasswordChange = this.onInputChange.bind(this, false);
  48. }
  49. showErrorDialog = (error: number) =>
  50. this.setState({
  51. dialogVisible: true,
  52. dialogError: error,
  53. });
  54. hideErrorDialog = () => this.setState({dialogVisible: false});
  55. handleSuccess = () => this.props.navigation.goBack();
  56. onResetPasswordClick = () => Linking.openURL(RESET_PASSWORD_PATH);
  57. validateEmail = () => this.setState({isEmailValidated: true});
  58. isEmailValid() {
  59. return emailRegex.test(this.state.email);
  60. }
  61. shouldShowEmailError() {
  62. return this.state.isEmailValidated && !this.isEmailValid();
  63. }
  64. validatePassword = () => this.setState({isPasswordValidated: true});
  65. isPasswordValid() {
  66. return this.state.password !== '';
  67. }
  68. shouldShowPasswordError() {
  69. return this.state.isPasswordValidated && !this.isPasswordValid();
  70. }
  71. shouldEnableLogin() {
  72. return this.isEmailValid() && this.isPasswordValid() && !this.state.loading;
  73. }
  74. onInputChange(isEmail: boolean, value: string) {
  75. if (isEmail) {
  76. this.setState({
  77. email: value,
  78. isEmailValidated: false,
  79. });
  80. } else {
  81. this.setState({
  82. password: value,
  83. isPasswordValidated: false,
  84. });
  85. }
  86. }
  87. onEmailSubmit = () => this.passwordInputRef.focus();
  88. onSubmit = () => {
  89. if (this.shouldEnableLogin()) {
  90. this.setState({loading: true});
  91. ConnectionManager.getInstance().connect(this.state.email, this.state.password)
  92. .then(this.handleSuccess)
  93. .catch(this.showErrorDialog)
  94. .finally(() => {
  95. this.setState({loading: false});
  96. });
  97. }
  98. };
  99. getFormInput() {
  100. return (
  101. <View>
  102. <TextInput
  103. label={i18n.t("loginScreen.email")}
  104. mode='outlined'
  105. value={this.state.email}
  106. onChangeText={this.onEmailChange}
  107. onBlur={this.validateEmail}
  108. onSubmitEditing={this.onEmailSubmit}
  109. error={this.shouldShowEmailError()}
  110. textContentType={'emailAddress'}
  111. autoCapitalize={'none'}
  112. autoCompleteType={'email'}
  113. autoCorrect={false}
  114. keyboardType={'email-address'}
  115. returnKeyType={'next'}
  116. secureTextEntry={false}
  117. />
  118. <HelperText
  119. type="error"
  120. visible={this.shouldShowEmailError()}
  121. >
  122. {i18n.t("loginScreen.emailError")}
  123. </HelperText>
  124. <TextInput
  125. ref={(ref) => {
  126. this.passwordInputRef = ref;
  127. }}
  128. label={i18n.t("loginScreen.password")}
  129. mode='outlined'
  130. value={this.state.password}
  131. onChangeText={this.onPasswordChange}
  132. onBlur={this.validatePassword}
  133. onSubmitEditing={this.onSubmit}
  134. error={this.shouldShowPasswordError()}
  135. textContentType={'password'}
  136. autoCapitalize={'none'}
  137. autoCompleteType={'password'}
  138. autoCorrect={false}
  139. keyboardType={'default'}
  140. returnKeyType={'done'}
  141. secureTextEntry={true}
  142. />
  143. <HelperText
  144. type="error"
  145. visible={this.shouldShowPasswordError()}
  146. >
  147. {i18n.t("loginScreen.passwordError")}
  148. </HelperText>
  149. </View>
  150. );
  151. }
  152. getMainCard() {
  153. return (
  154. <Card style={styles.card}>
  155. <Card.Title
  156. title={i18n.t("loginScreen.title")}
  157. subtitle={i18n.t("loginScreen.subtitle")}
  158. left={(props) => <Avatar.Image
  159. {...props}
  160. source={ICON_AMICALE}
  161. style={{backgroundColor: 'transparent'}}/>}
  162. />
  163. <Card.Content>
  164. {this.getFormInput()}
  165. <Card.Actions>
  166. <Button
  167. icon="send"
  168. mode="contained"
  169. disabled={!this.shouldEnableLogin()}
  170. loading={this.state.loading}
  171. onPress={this.onSubmit}
  172. style={{marginLeft: 'auto'}}>
  173. {i18n.t("loginScreen.login")}
  174. </Button>
  175. </Card.Actions>
  176. <Card.Actions>
  177. <Button
  178. icon="help-circle"
  179. mode="contained"
  180. onPress={this.onResetPasswordClick}
  181. style={{marginLeft: 'auto'}}>
  182. {i18n.t("loginScreen.resetPassword")}
  183. </Button>
  184. </Card.Actions>
  185. </Card.Content>
  186. </Card>
  187. );
  188. }
  189. getSecondaryCard() {
  190. return (
  191. <Card style={styles.card}>
  192. <Card.Title
  193. title={i18n.t("loginScreen.whyAccountTitle")}
  194. subtitle={i18n.t("loginScreen.whyAccountSub")}
  195. left={(props) => <Avatar.Icon
  196. {...props}
  197. icon={"help"}
  198. color={this.props.theme.colors.primary}
  199. style={{backgroundColor: 'transparent'}}/>}
  200. />
  201. <Card.Content>
  202. <Paragraph>{i18n.t("loginScreen.whyAccountParagraph")}</Paragraph>
  203. <Paragraph>{i18n.t("loginScreen.whyAccountParagraph2")}</Paragraph>
  204. <Paragraph>{i18n.t("loginScreen.noAccount")}</Paragraph>
  205. </Card.Content>
  206. </Card>
  207. );
  208. }
  209. render() {
  210. const {containerPaddingTop, scrollIndicatorInsetTop, onScroll} = this.props.collapsibleStack;
  211. return (
  212. <KeyboardAvoidingView
  213. behavior={"height"}
  214. contentContainerStyle={styles.container}
  215. style={styles.container}
  216. enabled
  217. keyboardVerticalOffset={100}
  218. >
  219. <Animated.ScrollView
  220. onScroll={onScroll}
  221. contentContainerStyle={{
  222. paddingTop: containerPaddingTop,
  223. paddingBottom: CustomTabBar.TAB_BAR_HEIGHT + 20
  224. }}
  225. scrollIndicatorInsets={{top: scrollIndicatorInsetTop}}
  226. >
  227. <View>
  228. {this.getMainCard()}
  229. {this.getSecondaryCard()}
  230. </View>
  231. <ErrorDialog
  232. visible={this.state.dialogVisible}
  233. onDismiss={this.hideErrorDialog}
  234. errorCode={this.state.dialogError}
  235. />
  236. </Animated.ScrollView>
  237. </KeyboardAvoidingView>
  238. );
  239. }
  240. }
  241. const styles = StyleSheet.create({
  242. container: {
  243. flex: 1,
  244. flexDirection: 'column',
  245. justifyContent: 'center',
  246. },
  247. card: {
  248. margin: 10,
  249. },
  250. header: {
  251. fontSize: 36,
  252. marginBottom: 48
  253. },
  254. textInput: {},
  255. btnContainer: {
  256. marginTop: 5,
  257. marginBottom: 10,
  258. }
  259. });
  260. export default withCollapsible(withTheme(LoginScreen));