index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { useCallback, useState } from 'react';
  2. import { View, ScrollView } from 'react-native';
  3. import { Formik } from 'formik';
  4. import * as yup from 'yup';
  5. import { CommonActions, useFocusEffect, 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. const [isLoading, setIsLoading] = useState(true);
  27. useFocusEffect(
  28. useCallback(() => {
  29. const fetchData = async () => {
  30. setIsLoading(false);
  31. };
  32. fetchData();
  33. }, [])
  34. );
  35. if (isLoading) {
  36. return null;
  37. }
  38. if (data) {
  39. if (data.token) {
  40. storage.set('token', data.token);
  41. storage.set('uid', data.uid.toString());
  42. dispatch(
  43. CommonActions.reset({
  44. index: 1,
  45. routes: [{ name: NAVIGATION_PAGES.IN_APP }]
  46. })
  47. );
  48. }
  49. }
  50. return (
  51. <PageWrapper>
  52. <ScrollView showsVerticalScrollIndicator={false}>
  53. <View style={{ gap: 10 }}>
  54. <Header label={'Sign Up'} />
  55. <BigText>Edit account data</BigText>
  56. <KeyboardAwareScrollView>
  57. <Formik
  58. initialValues={{
  59. photo: {
  60. type: '',
  61. uri: '',
  62. name: ''
  63. },
  64. first_name: '',
  65. last_name: '',
  66. date_of_birth: '',
  67. homebase: 0,
  68. homebase2: undefined
  69. }}
  70. validationSchema={SignUpSchema}
  71. onSubmit={(values) => {
  72. const data = {
  73. user: {
  74. ...user,
  75. first_name: values.first_name,
  76. last_name: values.last_name,
  77. date_of_birth: values.date_of_birth,
  78. homebase: values.homebase,
  79. homebase2: values.homebase2
  80. },
  81. photo: {
  82. type: values.photo.type,
  83. uri: values.photo.uri,
  84. name: values.photo.uri.split('/').pop()!
  85. }
  86. };
  87. userRegister(data);
  88. }}
  89. >
  90. {(props) => (
  91. <View>
  92. <View style={{ display: 'flex', alignItems: 'center' }}>
  93. <AvatarPicker selectedAvatar={(asset) => props.setFieldValue('photo', asset)} />
  94. </View>
  95. <Input
  96. onChange={props.handleChange('first_name')}
  97. value={props.values.first_name}
  98. onBlur={props.handleBlur('first_name')}
  99. placeholder={'Enter your first name'}
  100. header={'First name'}
  101. formikError={props.touched.first_name && props.errors.first_name}
  102. />
  103. <Input
  104. onChange={props.handleChange('last_name')}
  105. value={props.values.last_name}
  106. onBlur={props.handleBlur('last_name')}
  107. placeholder={'Enter your last name'}
  108. header={'Last name'}
  109. formikError={props.touched.last_name && props.errors.last_name}
  110. />
  111. <InputDatePicker
  112. headerTitle={'Date of birth'}
  113. selectedDate={(date) => props.setFieldValue('date_of_birth', date)}
  114. formikError={props.touched.date_of_birth && props.errors.date_of_birth}
  115. />
  116. <ModalFlatList
  117. headerTitle={'Region of origin'}
  118. selectedObject={(data) => props.setFieldValue('homebase', data.id)}
  119. />
  120. <ModalFlatList
  121. headerTitle={'Second region'}
  122. selectedObject={(data) => props.setFieldValue('homebase2', data.id)}
  123. />
  124. <View style={{ marginTop: 10 }}>
  125. <Button onPress={props.handleSubmit}>Sign up</Button>
  126. </View>
  127. </View>
  128. )}
  129. </Formik>
  130. </KeyboardAwareScrollView>
  131. </View>
  132. </ScrollView>
  133. </PageWrapper>
  134. );
  135. };
  136. export default EditAccount;