index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import React, { useCallback, useEffect, useState } from 'react';
  2. import { View, ScrollView, Text } 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. import { fetchAndSaveStatistics } from 'src/database/statisticsService';
  15. import { usePostGetProfileInfoDataQuery } from '@api/user';
  16. import { Colors } from 'src/theme';
  17. import { getFontSize } from 'src/utils';
  18. const SignUpSchema = yup.object({
  19. first_name: yup.string().required(),
  20. last_name: yup.string().required(),
  21. date_of_birth: yup.string().required(),
  22. homebase: yup.number().required().min(1, 'Region of origin is required'),
  23. homebase2: yup.number().optional()
  24. });
  25. //TODO: formik avatar | date and flatlist error shown
  26. const EditAccount = () => {
  27. const { dispatch } = useNavigation();
  28. const [user] = store((state) => [state.registration.user]);
  29. const { data, error, mutate: userRegister } = useRegisterMutation();
  30. const { data: profileData } = usePostGetProfileInfoDataQuery(
  31. data?.token || '',
  32. data?.uid ? +data.uid : 0,
  33. data?.token ? true : false
  34. );
  35. const [isLoading, setIsLoading] = useState(true);
  36. const [isSubmitting, setIsSubmitting] = useState(false);
  37. useFocusEffect(
  38. useCallback(() => {
  39. const fetchData = async () => {
  40. setIsLoading(false);
  41. };
  42. fetchData();
  43. }, [])
  44. );
  45. useEffect(() => {
  46. if (profileData) {
  47. const userInfo = {
  48. avatar: profileData?.data?.user_data.avatar ?? '',
  49. first_name: profileData?.data?.user_data.first_name,
  50. last_name: profileData?.data?.user_data.last_name,
  51. homebase_flag: profileData?.data?.user_data.flag1
  52. };
  53. storage.set('currentUserData', JSON.stringify(userInfo));
  54. setIsSubmitting(false);
  55. dispatch(
  56. CommonActions.reset({
  57. index: 1,
  58. routes: [{ name: NAVIGATION_PAGES.INFO }]
  59. })
  60. );
  61. }
  62. }, [profileData]);
  63. const updateLocalData = async (token: string) => {
  64. await fetchAndSaveStatistics(token);
  65. };
  66. if (isLoading) {
  67. return null;
  68. }
  69. return (
  70. <PageWrapper>
  71. <ScrollView showsVerticalScrollIndicator={false}>
  72. <View style={{ gap: 10 }}>
  73. <Header label={'Sign Up'} />
  74. <BigText>Edit account data</BigText>
  75. <KeyboardAwareScrollView>
  76. <Formik
  77. initialValues={{
  78. photo: {
  79. type: '',
  80. uri: '',
  81. name: ''
  82. },
  83. first_name: '',
  84. last_name: '',
  85. date_of_birth: '',
  86. homebase: 0,
  87. homebase2: undefined
  88. }}
  89. validationSchema={SignUpSchema}
  90. onSubmit={async (values) => {
  91. setIsSubmitting(true);
  92. const data = {
  93. user: {
  94. ...user,
  95. first_name: values.first_name,
  96. last_name: values.last_name,
  97. date_of_birth: values.date_of_birth,
  98. homebase: values.homebase,
  99. homebase2: values.homebase2
  100. },
  101. photo: values.photo.uri
  102. ? {
  103. type: values.photo.type,
  104. uri: values.photo.uri,
  105. name: values.photo.uri.split('/').pop()!
  106. }
  107. : {
  108. type: undefined,
  109. uri: undefined,
  110. name: undefined
  111. }
  112. };
  113. await userRegister(data, {
  114. onSuccess: (data) => {
  115. if (data && data.token) {
  116. storage.set('token', data.token);
  117. storage.set('uid', data.uid.toString());
  118. updateLocalData(data.token);
  119. } else {
  120. setIsSubmitting(false);
  121. }
  122. },
  123. onError: () => {
  124. setIsSubmitting(false);
  125. }
  126. });
  127. }}
  128. >
  129. {(props) => (
  130. <View>
  131. <View style={{ display: 'flex', alignItems: 'center' }}>
  132. <AvatarPicker selectedAvatar={(asset) => props.setFieldValue('photo', asset)} />
  133. </View>
  134. <Input
  135. onChange={props.handleChange('first_name')}
  136. value={props.values.first_name}
  137. onBlur={props.handleBlur('first_name')}
  138. placeholder={'Enter your first name'}
  139. header={'First name'}
  140. formikError={props.touched.first_name && props.errors.first_name}
  141. />
  142. <Input
  143. onChange={props.handleChange('last_name')}
  144. value={props.values.last_name}
  145. onBlur={props.handleBlur('last_name')}
  146. placeholder={'Enter your last name'}
  147. header={'Last name'}
  148. formikError={props.touched.last_name && props.errors.last_name}
  149. />
  150. <InputDatePicker
  151. headerTitle={'Date of birth'}
  152. selectedDate={(date) => props.setFieldValue('date_of_birth', date)}
  153. formikError={props.touched.date_of_birth && props.errors.date_of_birth}
  154. />
  155. <ModalFlatList
  156. headerTitle={'Region of origin'}
  157. selectedObject={(data) => props.setFieldValue('homebase', data.id)}
  158. />
  159. {props.touched.homebase && props.errors.homebase ? (
  160. <Text
  161. style={{
  162. color: Colors.RED,
  163. fontSize: getFontSize(12),
  164. fontFamily: 'redhat-600',
  165. marginTop: 5
  166. }}
  167. >
  168. {props.errors.homebase}
  169. </Text>
  170. ) : null}
  171. <ModalFlatList
  172. headerTitle={'Second region'}
  173. selectedObject={(data) => props.setFieldValue('homebase2', data.id)}
  174. />
  175. <View style={{ marginTop: 10 }}>
  176. <Button onPress={props.handleSubmit} disabled={isSubmitting}>
  177. Sign up
  178. </Button>
  179. </View>
  180. </View>
  181. )}
  182. </Formik>
  183. </KeyboardAwareScrollView>
  184. </View>
  185. </ScrollView>
  186. </PageWrapper>
  187. );
  188. };
  189. export default EditAccount;