index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { FC } 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 { storageSet } from '../../storage';
  9. import { NAVIGATION_PAGES } from '../../types';
  10. import { useLoginMutation } from '../../modules/auth/api/queries/use-post-login';
  11. type Props = {
  12. navigation: NavigationProp<any>;
  13. };
  14. const LoginSchema = yup.object({
  15. login: yup.string().required(),
  16. pass: yup.string().required()
  17. });
  18. const LoginScreen: FC<Props> = ({ navigation }) => {
  19. const { dispatch, navigate } = useNavigation();
  20. const { data, mutate: userLogin } = useLoginMutation();
  21. if (data) {
  22. if (data.token) {
  23. storageSet('token', data.token);
  24. storageSet('uid', data.uid.toString());
  25. dispatch(
  26. CommonActions.reset({
  27. index: 1,
  28. routes: [{ name: NAVIGATION_PAGES.IN_APP }]
  29. })
  30. );
  31. }
  32. }
  33. return (
  34. <PageWrapper>
  35. <Header label={'Login'} />
  36. <Formik
  37. initialValues={{
  38. login: '',
  39. pass: ''
  40. }}
  41. onSubmit={({ login, pass }) => {
  42. userLogin({
  43. login,
  44. pass
  45. });
  46. }}
  47. validationSchema={LoginSchema}
  48. >
  49. {(props) => (
  50. <>
  51. <View style={{ gap: 15 }}>
  52. <BigText>Welcome back</BigText>
  53. <Input
  54. header={'Email address'}
  55. placeholder={'Email or login'}
  56. inputMode={'email'}
  57. onChange={props.handleChange('login')}
  58. value={props.values.login}
  59. onBlur={props.handleBlur('login')}
  60. formikError={
  61. data?.result_description || (props.touched.login && props.errors.login)
  62. }
  63. />
  64. <Input
  65. header={'Password'}
  66. isPrivate={true}
  67. placeholder={'Login'}
  68. onChange={props.handleChange('pass')}
  69. value={props.values.pass}
  70. onBlur={props.handleBlur('pass')}
  71. formikError={data?.result_description || (props.touched.pass && props.errors.pass)}
  72. />
  73. </View>
  74. <View style={{ gap: 30, marginTop: '5%' }}>
  75. <Button
  76. variant={ButtonVariants.TEXT}
  77. onPress={() => navigation.navigate(NAVIGATION_PAGES.RESET_PASSWORD)}
  78. >
  79. Forgot password
  80. </Button>
  81. <Button onPress={props.handleSubmit}>Login</Button>
  82. </View>
  83. </>
  84. )}
  85. </Formik>
  86. </PageWrapper>
  87. );
  88. };
  89. export default LoginScreen;