Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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

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