index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import React, { FC, ReactNode, useState } from 'react';
  2. import { ScrollView, Text, TouchableOpacity, View } from 'react-native';
  3. import { Image } from 'expo-image';
  4. import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
  5. import { CommonActions, NavigationProp, useNavigation } from '@react-navigation/native';
  6. import {
  7. type Score,
  8. type Series,
  9. type SocialData,
  10. usePostGetProfileInfoPublicQuery,
  11. usePostGetProfileInfoQuery
  12. } from '@api/user';
  13. import { BigText, Button, PageWrapper, Loading, AvatarWithInitials } from '../../../components';
  14. import { Colors } from '../../../theme';
  15. import { styles } from './styles';
  16. import { ButtonVariants } from '../../../types/components';
  17. import { API_HOST } from '../../../constants';
  18. import { NAVIGATION_PAGES } from '../../../types';
  19. import { navigationOpts } from './navigation-opts';
  20. import { storage, StoreType } from '../../../storage';
  21. import { getFontSize, getYears } from '../../../utils';
  22. import IconFacebook from '../../../../assets/icons/facebook.svg';
  23. import IconInstagram from '../../../../assets/icons/instagram.svg';
  24. import IconTwitter from '../../../../assets/icons/x(twitter).svg';
  25. import IconYouTube from '../../../../assets/icons/youtube.svg';
  26. import IconGlobe from '../../../../assets/icons/bottom-navigation/globe.svg';
  27. import IconLink from '../../../../assets/icons/link.svg';
  28. import GearIcon from '../../../../assets/icons/gear.svg';
  29. const Tab = createMaterialTopTabNavigator();
  30. type Props = {
  31. navigation: NavigationProp<any>;
  32. route: any;
  33. };
  34. const ProfileScreen: FC<Props> = ({ navigation, route }) => {
  35. const isPublicView = route.name === NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW;
  36. const token = storage.get('token', StoreType.STRING) as string;
  37. if (!token) return <UnauthenticatedProfileScreen />;
  38. const { data, isFetching } = isPublicView
  39. ? usePostGetProfileInfoPublicQuery(route.params?.userId, true)
  40. : usePostGetProfileInfoQuery(token, true);
  41. if (!data || isFetching) return <Loading />;
  42. return (
  43. <PageWrapper>
  44. <View style={styles.pageWrapper}>
  45. <View>
  46. {data.avatar ? (
  47. <Image
  48. style={{ borderRadius: 64 / 2, width: 64, height: 64 }}
  49. source={{ uri: API_HOST + data.avatar }}
  50. />
  51. ) : (
  52. <AvatarWithInitials
  53. text={`${data.first_name[0] ?? ''}${data.last_name[0] ?? ''}`}
  54. flag={API_HOST + data.homebase_flag}
  55. size={64}
  56. />
  57. )}
  58. </View>
  59. <View style={{ gap: 5, width: '75%' }}>
  60. <Text style={[styles.headerText, { fontSize: getFontSize(18), flex: 0 }]}>
  61. {data.first_name} {data.last_name}
  62. </Text>
  63. <View style={{ display: 'flex', justifyContent: 'space-between', flexDirection: 'row' }}>
  64. <View
  65. style={{
  66. display: 'flex',
  67. flexDirection: 'row',
  68. gap: 10,
  69. alignItems: 'center'
  70. }}
  71. >
  72. <Text
  73. style={{ color: Colors.DARK_BLUE, fontWeight: '600', fontSize: getFontSize(12) }}
  74. >
  75. Age: {getYears(data.date_of_birth)}
  76. </Text>
  77. <Image source={{ uri: API_HOST + data.homebase_flag }} style={styles.countryFlag} />
  78. {data.homebase2_flag && data.homebase2_flag !== data.homebase_flag ? (
  79. <Image
  80. source={{ uri: API_HOST + data.homebase2_flag }}
  81. style={[styles.countryFlag, { marginLeft: -15 }]}
  82. />
  83. ) : null}
  84. </View>
  85. {!isPublicView ? (
  86. <View>
  87. <TouchableOpacity onPress={() => navigation.navigate(NAVIGATION_PAGES.SETTINGS)}>
  88. <GearIcon fill={Colors.DARK_BLUE} />
  89. </TouchableOpacity>
  90. </View>
  91. ) : null}
  92. </View>
  93. </View>
  94. </View>
  95. <Tab.Navigator
  96. screenOptions={{
  97. ...navigationOpts,
  98. tabBarLabel: ({ children, color, focused }) => (
  99. <Text style={[styles.headerText, { color }]}>{children}</Text>
  100. )
  101. }}
  102. >
  103. <Tab.Screen name="Personal Info">
  104. {() => (
  105. <PersonalInfo
  106. data={{
  107. bio: data.bio,
  108. date_of_birth: data.date_of_birth,
  109. scores: data.scores,
  110. links: data.links,
  111. homebase: data.homebase_name,
  112. homebase2: data.homebase2_name,
  113. series: data.series
  114. }}
  115. />
  116. )}
  117. </Tab.Screen>
  118. <Tab.Screen name="Ranking">{() => <View></View>}</Tab.Screen>
  119. <Tab.Screen name="Photos">{() => <View></View>}</Tab.Screen>
  120. </Tab.Navigator>
  121. </PageWrapper>
  122. );
  123. };
  124. type PersonalInfoProps = {
  125. data: {
  126. bio: string;
  127. date_of_birth: string;
  128. scores: Score[];
  129. links: {
  130. f?: SocialData;
  131. t?: SocialData;
  132. i?: SocialData;
  133. y?: SocialData;
  134. www?: SocialData;
  135. other?: SocialData;
  136. };
  137. homebase: string;
  138. homebase2: string;
  139. series: Series[];
  140. };
  141. };
  142. const PersonalInfo: FC<PersonalInfoProps> = ({ data }) => {
  143. const [showMoreSeries, setShowMoreSeries] = useState(false);
  144. return (
  145. <ScrollView
  146. showsVerticalScrollIndicator={false}
  147. contentContainerStyle={{ gap: 20 }}
  148. style={{ marginTop: 20 }}
  149. >
  150. <InfoItem inline={true} title={'RANKING'}>
  151. {data.scores?.map((data, index) => {
  152. if (!data.score) return null;
  153. return (
  154. <View
  155. key={index}
  156. style={{ display: 'flex', flexDirection: 'column', gap: 5, alignItems: 'center' }}
  157. >
  158. <Text style={[styles.headerText, { flex: 0 }]}>{data.score}</Text>
  159. <Text style={[styles.titleText, { flex: 0 }]}>{data.name}</Text>
  160. </View>
  161. );
  162. })}
  163. </InfoItem>
  164. <InfoItem showMore={showMoreSeries} inline={true} title={'SERIES BADGES'}>
  165. {data.series?.slice(0, showMoreSeries ? data.series.length : 8).map((data, index) => (
  166. <View
  167. key={index}
  168. style={{ display: 'flex', flexDirection: 'column', gap: 5, alignItems: 'center' }}
  169. >
  170. <Image source={{ uri: API_HOST + data.icon_png }} style={{ width: 28, height: 28 }} />
  171. <Text style={[styles.headerText, { flex: 0 }]}>{data.score}</Text>
  172. </View>
  173. ))}
  174. </InfoItem>
  175. {data.series?.length > 8 ? (
  176. <Button
  177. children={showMoreSeries ? 'Hide series' : 'Show more'}
  178. variant={ButtonVariants.TEXT}
  179. onPress={() => setShowMoreSeries(!showMoreSeries)}
  180. />
  181. ) : null}
  182. <View style={{ display: 'flex', flexDirection: 'row' }}>
  183. <Text style={styles.headerText}>DATE OF BIRTH</Text>
  184. <Text style={styles.titleText}>{new Date(data.date_of_birth).toDateString()}</Text>
  185. </View>
  186. <View style={{ display: 'flex', flexDirection: 'row' }}>
  187. <Text style={styles.headerText}>REGION OF ORIGIN</Text>
  188. <Text style={styles.titleText}>{data.homebase}</Text>
  189. </View>
  190. {data.homebase2 ? (
  191. <View style={{ display: 'flex', flexDirection: 'row' }}>
  192. <Text style={styles.headerText}>SECOND REGION</Text>
  193. <Text style={styles.titleText}>{data.homebase2}</Text>
  194. </View>
  195. ) : null}
  196. <InfoItem title={'BIO'}>
  197. <Text style={[styles.titleText, { flex: 0 }]}>{data.bio}</Text>
  198. </InfoItem>
  199. <InfoItem title={'SOCIAL LINKS'}>
  200. <View style={{ display: 'flex', flexDirection: 'row', gap: 15, alignItems: 'center' }}>
  201. {data.links?.f?.link ? <IconFacebook fill={Colors.DARK_BLUE} /> : null}
  202. {data.links?.i?.link ? <IconInstagram fill={Colors.DARK_BLUE} /> : null}
  203. {data.links?.t?.link ? <IconTwitter fill={Colors.DARK_BLUE} /> : null}
  204. {data.links?.y?.link ? <IconYouTube fill={Colors.DARK_BLUE} /> : null}
  205. {data.links?.www?.link ? <IconGlobe fill={Colors.DARK_BLUE} /> : null}
  206. {data.links?.other?.link ? <IconLink fill={Colors.DARK_BLUE} /> : null}
  207. </View>
  208. </InfoItem>
  209. </ScrollView>
  210. );
  211. };
  212. const InfoItem: FC<{
  213. title: string;
  214. inline?: boolean;
  215. children: ReactNode;
  216. showMore?: boolean;
  217. }> = ({ title, inline, children, showMore }) => (
  218. <View>
  219. <Text style={[styles.headerText, { flex: 0 }]}>{title}</Text>
  220. <View
  221. style={[
  222. {
  223. display: 'flex',
  224. flexDirection: inline ? 'row' : 'column',
  225. justifyContent: 'space-evenly',
  226. marginTop: 10
  227. },
  228. showMore ? { flexWrap: 'wrap', gap: 8 } : {}
  229. ]}
  230. >
  231. {children}
  232. </View>
  233. </View>
  234. );
  235. const UnauthenticatedProfileScreen = () => {
  236. const navigation = useNavigation();
  237. return (
  238. <PageWrapper>
  239. <View style={{ marginTop: 15, display: 'flex', gap: 10 }}>
  240. <BigText children={'You are not logged in. Please login or register to access profile.'} />
  241. <Button
  242. onPress={() =>
  243. navigation.dispatch(
  244. CommonActions.reset({
  245. index: 1,
  246. routes: [{ name: NAVIGATION_PAGES.WELCOME }]
  247. })
  248. )
  249. }
  250. variant={ButtonVariants.FILL}
  251. >
  252. Go to login/register
  253. </Button>
  254. </View>
  255. </PageWrapper>
  256. );
  257. };
  258. export default ProfileScreen;