index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { FC, useEffect } from 'react';
  2. import { View } from 'react-native';
  3. import { NavigationProp, useNavigation, CommonActions } from '@react-navigation/native';
  4. import { Formik } from 'formik';
  5. import * as yup from 'yup';
  6. import { Header, Input, Button, BigText, PageWrapper } from '../../components';
  7. import { ButtonVariants } from '../../types/components';
  8. import { StoreType, storage } from '../../storage';
  9. import { NAVIGATION_PAGES } from '../../types';
  10. import { useLoginMutation } from '@api/auth';
  11. import { fetchAndSaveStatistics } from 'src/database/statisticsService';
  12. type Props = {
  13. navigation: NavigationProp<any>;
  14. };
  15. const LoginSchema = yup.object({
  16. login: yup.string().required(),
  17. pass: yup.string().required()
  18. });
  19. const LoginScreen: FC<Props> = ({ navigation }) => {
  20. const { dispatch } = useNavigation();
  21. const isFirstLaunch = storage.get('isFirstLaunch', StoreType.BOOLEAN) ?? true;
  22. const { data, mutate: userLogin } = useLoginMutation();
  23. const updateLocalData = async (token: string) => {
  24. await fetchAndSaveStatistics(token);
  25. };
  26. useEffect(() => {
  27. if (data && data.token) {
  28. storage.set('token', data.token);
  29. storage.set('uid', data.uid.toString());
  30. storage.set('isFirstLaunch', false);
  31. updateLocalData(data.token);
  32. isFirstLaunch
  33. ? dispatch(
  34. CommonActions.reset({
  35. index: 1,
  36. routes: [{ name: NAVIGATION_PAGES.INFO }]
  37. })
  38. )
  39. : dispatch(
  40. CommonActions.reset({
  41. index: 1,
  42. routes: [{ name: NAVIGATION_PAGES.IN_APP }]
  43. })
  44. );
  45. }
  46. }, [data]);
  47. return (
  48. <PageWrapper>
  49. <Header label={'Login'} />
  50. <Formik
  51. initialValues={{
  52. login: '',
  53. pass: ''
  54. }}
  55. onSubmit={({ login, pass }) => {
  56. userLogin({
  57. login,
  58. pass
  59. });
  60. }}
  61. validationSchema={LoginSchema}
  62. >
  63. {(props) => (
  64. <>
  65. <View style={{ gap: 15 }}>
  66. <BigText>Welcome back</BigText>
  67. <Input
  68. header={'Email address'}
  69. placeholder={'Email or login'}
  70. inputMode={'email'}
  71. onChange={props.handleChange('login')}
  72. value={props.values.login}
  73. onBlur={props.handleBlur('login')}
  74. formikError={
  75. data?.result_description || (props.touched.login && props.errors.login)
  76. }
  77. />
  78. <Input
  79. header={'Password'}
  80. isPrivate={true}
  81. placeholder={'Login'}
  82. onChange={props.handleChange('pass')}
  83. value={props.values.pass}
  84. onBlur={props.handleBlur('pass')}
  85. formikError={data?.result_description || (props.touched.pass && props.errors.pass)}
  86. />
  87. </View>
  88. <View style={{ gap: 30, marginTop: '5%' }}>
  89. <Button
  90. variant={ButtonVariants.TEXT}
  91. onPress={() => navigation.navigate(NAVIGATION_PAGES.RESET_PASSWORD)}
  92. >
  93. Forgot password
  94. </Button>
  95. <Button onPress={props.handleSubmit}>Login</Button>
  96. </View>
  97. </>
  98. )}
  99. </Formik>
  100. </PageWrapper>
  101. );
  102. };
  103. export default LoginScreen;