edit-personal-info.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import React from 'react';
  2. import { ScrollView, View, Text } from 'react-native';
  3. import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
  4. import { Formik } from 'formik';
  5. import * as yup from 'yup';
  6. import { useNavigation } from '@react-navigation/native';
  7. import { useQueryClient } from '@tanstack/react-query';
  8. import { Image } from 'expo-image';
  9. import { API_HOST } from '../../../../constants';
  10. import { AvatarPicker, BigText, Header, Input, PageWrapper, Button } from '../../../../components';
  11. import { InputDatePicker } from '../../../../components/Calendar/InputDatePicker';
  12. import { ModalFlatList } from '../../../../components/FlatList/modal-flatlist';
  13. import { usePostGetProfileQuery } from '../../../../modules/auth/user/queries/use-post-get-profile';
  14. import { useGetRegionsWithFlagQuery } from '../../../../modules/auth/regions/queries/use-post-get-regions';
  15. import { usePostSetProfileMutation } from '../../../../modules/auth/user/queries/use-post-set-profile';
  16. import { userQueryKeys } from '../../../../modules/auth/user/user-query-keys';
  17. import type { PostSetProfileData } from '../../../../modules/auth/user/user-api';
  18. import { storage, StoreType } from '../../../../storage';
  19. import { Colors } from '../../../../theme';
  20. import FacebookIcon from '../../../../../assets/icons/facebook.svg';
  21. import InstagramIcon from '../../../../../assets/icons/instagram.svg';
  22. import XIcon from '../../../../../assets/icons/x(twitter).svg';
  23. import YoutubeIcon from '../../../../../assets/icons/youtube.svg';
  24. import GlobeIcon from '../../../../../assets/icons/bottom-navigation/globe.svg';
  25. import LinkIcon from '../../../../../assets/icons/link.svg';
  26. const ProfileSchema = yup.object({
  27. username: yup.string().optional(),
  28. email: yup.string().email().optional(),
  29. first_name: yup.string().optional(),
  30. last_name: yup.string().optional(),
  31. date_of_birth: yup.string().optional(),
  32. homebase: yup.number().optional(),
  33. homebase2: yup.number().nullable().optional(),
  34. bio: yup.string().optional(),
  35. f: yup.string().optional(),
  36. t: yup.string().optional(),
  37. i: yup.string().optional(),
  38. y: yup.string().optional(),
  39. www: yup.string().optional(),
  40. other: yup.string().optional()
  41. });
  42. export const EditPersonalInfo = () => {
  43. const token = storage.get('token', StoreType.STRING);
  44. const { mutate: updateProfile, data: updateResponse, reset } = usePostSetProfileMutation();
  45. const navigation = useNavigation();
  46. const queryClient = useQueryClient();
  47. const { data, error } = usePostGetProfileQuery(String(token), true);
  48. const regions = useGetRegionsWithFlagQuery(true);
  49. if (!data) return <Text>Loading</Text>;
  50. const originRegion = regions.data?.data.find((region) => region.id === data.homebase);
  51. const secondOrigin = regions.data?.data.find((region) => region.id === data.homebase2);
  52. return (
  53. <PageWrapper>
  54. <ScrollView showsVerticalScrollIndicator={false}>
  55. <Header label={'Edit Personal Info'} />
  56. <KeyboardAwareScrollView>
  57. <Formik
  58. validationSchema={ProfileSchema}
  59. initialValues={{
  60. username: data.username,
  61. email: data.email,
  62. first_name: data.first_name,
  63. last_name: data.last_name,
  64. date_of_birth: data.date_of_birth,
  65. homebase: data.homebase,
  66. homebase2: data.homebase2,
  67. bio: data.bio.toString(),
  68. f: data.links.f!.link,
  69. i: data.links.i!.link,
  70. t: data.links.t!.link,
  71. y: data.links.y!.link,
  72. www: data.links.www!.link,
  73. other: data.links.other!.link,
  74. photo: {
  75. type: '',
  76. uri: '',
  77. name: ''
  78. }
  79. }}
  80. onSubmit={async (values) => {
  81. const profileData: PostSetProfileData = {
  82. token: String(token),
  83. user: {
  84. username: values.username,
  85. email: values.email,
  86. first_name: values.first_name,
  87. last_name: values.last_name,
  88. date_of_birth: values.date_of_birth,
  89. homebase: values.homebase,
  90. bio: values.bio,
  91. f: values.f,
  92. i: values.i,
  93. t: values.t,
  94. y: values.y,
  95. www: values.www,
  96. other: values.other
  97. }
  98. };
  99. if (values.homebase2) {
  100. profileData.user!.homebase2 = values.homebase2;
  101. }
  102. if (values.photo.uri) {
  103. profileData.photo = {
  104. type: values.photo.type,
  105. uri: values.photo.uri,
  106. name: values.photo.uri.split('/').pop()!
  107. };
  108. }
  109. updateProfile(profileData, {
  110. onSuccess: () => {
  111. queryClient.invalidateQueries({
  112. queryKey: userQueryKeys.getProfileData(),
  113. refetchType: 'all'
  114. });
  115. Image.clearDiskCache();
  116. Image.clearMemoryCache();
  117. navigation.goBack();
  118. }
  119. });
  120. }}
  121. >
  122. {(props) => (
  123. <View style={{ gap: 10 }}>
  124. <View style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
  125. <AvatarPicker
  126. defaultAvatar={API_HOST + '/img/avatars/' + data.avatar}
  127. selectedAvatar={(asset) => props.setFieldValue('photo', asset)}
  128. />
  129. </View>
  130. <BigText>Account</BigText>
  131. <Input
  132. editable={false}
  133. header={"Username (can't edit)"}
  134. placeholder={'Text'}
  135. inputMode={'text'}
  136. onChange={props.handleChange('username')}
  137. value={props.values.username}
  138. onBlur={props.handleBlur('username')}
  139. formikError={props.touched.username && props.errors.username}
  140. />
  141. <Input
  142. editable={false}
  143. header={"Email address (can't edit)"}
  144. placeholder={'Email'}
  145. inputMode={'email'}
  146. onChange={props.handleChange('email')}
  147. value={props.values.email}
  148. onBlur={props.handleBlur('email')}
  149. formikError={props.touched.email && props.errors.email}
  150. />
  151. <BigText>General Info</BigText>
  152. <Input
  153. header={'First name'}
  154. placeholder={'Text'}
  155. inputMode={'text'}
  156. onChange={props.handleChange('first_name')}
  157. value={props.values.first_name}
  158. onBlur={props.handleBlur('first_name')}
  159. formikError={props.touched.first_name && props.errors.first_name}
  160. />
  161. <Input
  162. header={'Last name'}
  163. placeholder={'Text'}
  164. inputMode={'text'}
  165. onChange={props.handleChange('last_name')}
  166. value={props.values.last_name}
  167. onBlur={props.handleBlur('last_name')}
  168. formikError={props.touched.last_name && props.errors.last_name}
  169. />
  170. <InputDatePicker
  171. headerTitle={'Date of birth'}
  172. defaultDate={new Date(data.date_of_birth)}
  173. selectedDate={(date) => props.setFieldValue('date_of_birth', date)}
  174. formikError={props.touched.date_of_birth && props.errors.date_of_birth}
  175. />
  176. <ModalFlatList
  177. headerTitle={'Region of origin'}
  178. defaultObject={{ name: originRegion?.name }}
  179. selectedObject={(data) => props.setFieldValue('homebase', data.id)}
  180. />
  181. <ModalFlatList
  182. headerTitle={'Second region'}
  183. defaultObject={{ name: secondOrigin?.name }}
  184. selectedObject={(data) => props.setFieldValue('homebase2', data.id)}
  185. />
  186. <Input
  187. multiline={true}
  188. header={'Bio'}
  189. placeholder={'Text'}
  190. inputMode={'text'}
  191. onChange={props.handleChange('bio')}
  192. value={props.values.bio}
  193. onBlur={props.handleBlur('bio')}
  194. formikError={props.touched.bio && props.errors.bio}
  195. />
  196. <BigText>Links</BigText>
  197. <Input
  198. icon={<FacebookIcon />}
  199. placeholder={'https://www.facebook.com'}
  200. inputMode={'text'}
  201. onChange={props.handleChange('f')}
  202. value={props.values.f as unknown as string}
  203. onBlur={props.handleBlur('f')}
  204. formikError={props.touched.f && props.errors.f}
  205. />
  206. <Input
  207. icon={<InstagramIcon />}
  208. placeholder={'https://www.instagram.com'}
  209. inputMode={'text'}
  210. onChange={props.handleChange('i')}
  211. value={props.values.i as unknown as string}
  212. onBlur={props.handleBlur('i')}
  213. formikError={props.touched.i && props.errors.i}
  214. />
  215. <Input
  216. icon={<XIcon />}
  217. placeholder={'https://www.twitter.com'}
  218. inputMode={'text'}
  219. onChange={props.handleChange('t')}
  220. value={props.values.t as unknown as string}
  221. onBlur={props.handleBlur('t')}
  222. formikError={props.touched.t && props.errors.t}
  223. />
  224. <Input
  225. icon={<YoutubeIcon />}
  226. placeholder={'https://www.youtube.com'}
  227. inputMode={'text'}
  228. onChange={props.handleChange('y')}
  229. value={props.values.y as unknown as string}
  230. onBlur={props.handleBlur('y')}
  231. formikError={props.touched.y && props.errors.y}
  232. />
  233. <Input
  234. icon={<GlobeIcon fill={Colors.LIGHT_GRAY} />}
  235. placeholder={'My Website'}
  236. inputMode={'text'}
  237. onChange={props.handleChange('www')}
  238. value={props.values.www as unknown as string}
  239. onBlur={props.handleBlur('www')}
  240. formikError={props.touched.www && props.errors.www}
  241. />
  242. <Input
  243. icon={<LinkIcon />}
  244. placeholder={'Other link'}
  245. inputMode={'text'}
  246. onChange={props.handleChange('other')}
  247. value={props.values.other as unknown as string}
  248. onBlur={props.handleBlur('other')}
  249. formikError={props.touched.other && props.errors.other}
  250. />
  251. <View style={{ marginTop: 15, marginBottom: 15 }}>
  252. <Button onPress={props.handleSubmit}>Save</Button>
  253. </View>
  254. </View>
  255. )}
  256. </Formik>
  257. </KeyboardAwareScrollView>
  258. </ScrollView>
  259. </PageWrapper>
  260. );
  261. };