index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React from 'react';
  2. import { View, ScrollView } from 'react-native';
  3. import { Formik } from 'formik';
  4. import * as yup from 'yup';
  5. import { CommonActions, useNavigation } from '@react-navigation/native';
  6. import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
  7. import { AvatarPicker, BigText, Button, Header, Input, PageWrapper } from '../../../components';
  8. import { InputDatePicker } from '../../../components/Calendar/InputDatePicker';
  9. import { ModalFlatList } from '../../../components/FlatList/modal-flatlist';
  10. import { useRegisterMutation } from '@api/auth';
  11. import { storage } from '../../../storage';
  12. import store from '../../../storage/zustand';
  13. import { NAVIGATION_PAGES } from '../../../types';
  14. const SignUpSchema = yup.object({
  15. first_name: yup.string().required(),
  16. last_name: yup.string().required(),
  17. date_of_birth: yup.string().required(),
  18. homebase: yup.number().required(),
  19. homebase2: yup.number().optional()
  20. });
  21. //TODO: formik avatar | date and flatlist error shown
  22. const EditAccount = () => {
  23. const { dispatch } = useNavigation();
  24. const [user] = store((state) => [state.registration.user]);
  25. const { data, error, mutate: userRegister } = useRegisterMutation();
  26. if (data) {
  27. if (data.token) {
  28. storage.set('token', data.token);
  29. storage.set('uid', data.uid.toString());
  30. dispatch(
  31. CommonActions.reset({
  32. index: 1,
  33. routes: [{ name: NAVIGATION_PAGES.IN_APP }]
  34. })
  35. );
  36. }
  37. }
  38. return (
  39. <PageWrapper>
  40. <ScrollView showsVerticalScrollIndicator={false}>
  41. <View style={{ gap: 10 }}>
  42. <Header label={'Sign Up'} />
  43. <BigText>Edit account data</BigText>
  44. <KeyboardAwareScrollView>
  45. <Formik
  46. initialValues={{
  47. photo: {
  48. type: '',
  49. uri: '',
  50. name: ''
  51. },
  52. first_name: '',
  53. last_name: '',
  54. date_of_birth: '',
  55. homebase: 0,
  56. homebase2: undefined
  57. }}
  58. validationSchema={SignUpSchema}
  59. onSubmit={(values) => {
  60. const data = {
  61. user: {
  62. ...user,
  63. first_name: values.first_name,
  64. last_name: values.last_name,
  65. date_of_birth: values.date_of_birth,
  66. homebase: values.homebase,
  67. homebase2: values.homebase2
  68. },
  69. photo: {
  70. type: values.photo.type,
  71. uri: values.photo.uri,
  72. name: values.photo.uri.split('/').pop()!
  73. }
  74. };
  75. userRegister(data);
  76. }}
  77. >
  78. {(props) => (
  79. <View>
  80. <View style={{ display: 'flex', alignItems: 'center' }}>
  81. <AvatarPicker selectedAvatar={(asset) => props.setFieldValue('photo', asset)} />
  82. </View>
  83. <Input
  84. onChange={props.handleChange('first_name')}
  85. value={props.values.first_name}
  86. onBlur={props.handleBlur('first_name')}
  87. placeholder={'Enter your first name'}
  88. header={'First name'}
  89. formikError={props.touched.first_name && props.errors.first_name}
  90. />
  91. <Input
  92. onChange={props.handleChange('last_name')}
  93. value={props.values.last_name}
  94. onBlur={props.handleBlur('last_name')}
  95. placeholder={'Enter your last name'}
  96. header={'Last name'}
  97. formikError={props.touched.last_name && props.errors.last_name}
  98. />
  99. <InputDatePicker
  100. headerTitle={'Date of birth'}
  101. selectedDate={(date) => props.setFieldValue('date_of_birth', date)}
  102. formikError={props.touched.date_of_birth && props.errors.date_of_birth}
  103. />
  104. <ModalFlatList
  105. headerTitle={'Region of origin'}
  106. selectedObject={(data) => props.setFieldValue('homebase', data.id)}
  107. />
  108. <ModalFlatList
  109. headerTitle={'Second region'}
  110. selectedObject={(data) => props.setFieldValue('homebase2', data.id)}
  111. />
  112. <View style={{ marginTop: 10 }}>
  113. <Button onPress={props.handleSubmit}>Sign up</Button>
  114. </View>
  115. </View>
  116. )}
  117. </Formik>
  118. </KeyboardAwareScrollView>
  119. </View>
  120. </ScrollView>
  121. </PageWrapper>
  122. );
  123. };
  124. export default EditAccount;