index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import React, { FC, ReactNode, useState } from 'react';
  2. import { Linking, ScrollView, Text, TouchableOpacity, View, Image } from 'react-native';
  3. import { CommonActions, NavigationProp, useNavigation } from '@react-navigation/native';
  4. import Modal from 'react-native-modal';
  5. import Tooltip from 'react-native-walkthrough-tooltip';
  6. import {
  7. type Score,
  8. type Series,
  9. type SocialData,
  10. usePostGetProfileInfoPublicQuery,
  11. usePostGetProfileInfoQuery,
  12. usePostGetProfileRegions
  13. } from '@api/user';
  14. import {
  15. BigText,
  16. Button,
  17. PageWrapper,
  18. Loading,
  19. AvatarWithInitials,
  20. Header
  21. } from '../../../components';
  22. import { Colors } from '../../../theme';
  23. import { styles } from './styles';
  24. import { ButtonVariants } from '../../../types/components';
  25. import { API_HOST } from '../../../constants';
  26. import { NAVIGATION_PAGES } from '../../../types';
  27. import { storage, StoreType } from '../../../storage';
  28. import { getFontSize, getYears } from '../../../utils';
  29. import IconFacebook from '../../../../assets/icons/facebook.svg';
  30. import IconInstagram from '../../../../assets/icons/instagram.svg';
  31. import IconTwitter from '../../../../assets/icons/x(twitter).svg';
  32. import IconYouTube from '../../../../assets/icons/youtube.svg';
  33. import IconGlobe from '../../../../assets/icons/bottom-navigation/globe.svg';
  34. import IconLink from '../../../../assets/icons/link.svg';
  35. import GearIcon from '../../../../assets/icons/gear.svg';
  36. import ArrowIcon from '../../../../assets/icons/next.svg';
  37. import RegionsRenderer from './RegionsRenderer';
  38. type Props = {
  39. navigation: NavigationProp<any>;
  40. route: any;
  41. };
  42. const ProfileScreen: FC<Props> = ({ navigation, route }) => {
  43. const isPublicView = route.name === NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW;
  44. const token = storage.get('token', StoreType.STRING) as string;
  45. const currentUserId = storage.get('uid', StoreType.STRING) as string;
  46. if (!token) return <UnauthenticatedProfileScreen />;
  47. const { data, isFetching } = isPublicView
  48. ? usePostGetProfileInfoPublicQuery(route.params?.userId, true)
  49. : usePostGetProfileInfoQuery(token, true);
  50. if (!data || isFetching) return <Loading />;
  51. const handleGoToMap = () => {
  52. isPublicView
  53. ? navigation.navigate(NAVIGATION_PAGES.USERS_MAP, { userId: route.params?.userId, data })
  54. : navigation.dispatch(
  55. CommonActions.reset({
  56. index: 1,
  57. routes: [{ name: NAVIGATION_PAGES.IN_APP_MAP_TAB }]
  58. })
  59. );
  60. };
  61. return (
  62. <PageWrapper>
  63. {isPublicView && <Header label="Profile" />}
  64. <TouchableOpacity
  65. style={[styles.usersMap, { backgroundColor: '#EBF2F5' }]}
  66. onPress={handleGoToMap}
  67. >
  68. <Image
  69. source={{
  70. uri: `${API_HOST}/img/single_maps/${isPublicView ? route.params?.userId : currentUserId}.png`
  71. }}
  72. style={styles.usersMap}
  73. />
  74. </TouchableOpacity>
  75. <View style={styles.pageWrapper}>
  76. <View style={{ top: -34 }}>
  77. {data.avatar ? (
  78. <Image style={styles.avatar} source={{ uri: API_HOST + data.avatar }} />
  79. ) : (
  80. <AvatarWithInitials
  81. text={`${data.first_name[0] ?? ''}${data.last_name[0] ?? ''}`}
  82. flag={API_HOST + data.homebase_flag}
  83. size={64}
  84. borderColor={Colors.WHITE}
  85. />
  86. )}
  87. </View>
  88. <View style={{ gap: 5, flex: 1 }}>
  89. <Text style={[styles.headerText, { fontSize: getFontSize(18), flex: 0 }]}>
  90. {data.first_name} {data.last_name}
  91. </Text>
  92. <View style={styles.userInfoContainer}>
  93. <View style={styles.userInfo}>
  94. <Text
  95. style={{ color: Colors.DARK_BLUE, fontWeight: '600', fontSize: getFontSize(12) }}
  96. >
  97. Age: {getYears(data.date_of_birth)}
  98. </Text>
  99. <Image source={{ uri: API_HOST + data.homebase_flag }} style={styles.countryFlag} />
  100. {data.homebase2_flag && data.homebase2_flag !== data.homebase_flag ? (
  101. <Image
  102. source={{ uri: API_HOST + data.homebase2_flag }}
  103. style={[styles.countryFlag, { marginLeft: -15 }]}
  104. />
  105. ) : null}
  106. </View>
  107. {!isPublicView ? (
  108. <TouchableOpacity
  109. style={styles.settings}
  110. onPress={() => navigation.navigate(NAVIGATION_PAGES.EDIT_PERSONAL_INFO)}
  111. >
  112. <GearIcon
  113. width={20}
  114. height={20}
  115. fill={Colors.DARK_BLUE}
  116. style={{ alignSelf: 'center' }}
  117. />
  118. </TouchableOpacity>
  119. ) : null}
  120. </View>
  121. </View>
  122. </View>
  123. <PersonalInfo
  124. data={{
  125. bio: data.bio,
  126. date_of_birth: data.date_of_birth,
  127. scores: data.scores,
  128. links: data.links,
  129. homebase: data.homebase_name,
  130. homebase2: data.homebase2_name,
  131. series: data.series
  132. }}
  133. userId={isPublicView ? route.params?.userId : +currentUserId}
  134. />
  135. </PageWrapper>
  136. );
  137. };
  138. type PersonalInfoProps = {
  139. data: {
  140. bio: string;
  141. date_of_birth: string;
  142. scores: Score[];
  143. links: {
  144. f?: SocialData;
  145. t?: SocialData;
  146. i?: SocialData;
  147. y?: SocialData;
  148. www?: SocialData;
  149. other?: SocialData;
  150. };
  151. homebase: string;
  152. homebase2: string;
  153. series: Series[];
  154. };
  155. userId: number;
  156. };
  157. const PersonalInfo: FC<PersonalInfoProps> = ({ data, userId }) => {
  158. const [showMoreSeries, setShowMoreSeries] = useState(false);
  159. const [type, setType] = useState<string>('nm');
  160. const [isModalVisible, setIsModalVisible] = useState(false);
  161. const [toolTipVisible, setToolTipVisible] = useState<number | null>(null);
  162. const { data: regions } = usePostGetProfileRegions(userId, type);
  163. const scores = ['NM1301', 'DARE', 'UN', 'UN+', 'TCC', 'SLOW', 'YES', 'WHS'];
  164. const handleOpenUrl = (url: string | undefined) => {
  165. url && Linking.openURL(url);
  166. };
  167. const hasActiveLinks = () => {
  168. return (
  169. (data.links?.f?.link && data.links?.f?.active !== 0) ||
  170. (data.links?.i?.link && data.links?.i?.active !== 0) ||
  171. (data.links?.t?.link && data.links?.t?.active !== 0) ||
  172. (data.links?.y?.link && data.links?.y?.active !== 0) ||
  173. (data.links?.www?.link && data.links?.www?.active !== 0) ||
  174. (data.links?.other?.link && data.links?.other?.active !== 0)
  175. );
  176. };
  177. const handleOpenModal = (type: string) => {
  178. switch (type) {
  179. case 'NM1301':
  180. setType('nm');
  181. break;
  182. case 'DARE':
  183. setType('mqp');
  184. break;
  185. case 'UN':
  186. setType('un');
  187. break;
  188. case 'UN+':
  189. setType('unp');
  190. break;
  191. case 'TCC':
  192. setType('tcc');
  193. break;
  194. case 'SLOW':
  195. setType('slow');
  196. break;
  197. case 'YES':
  198. setType('yes');
  199. break;
  200. case 'WHS':
  201. setType('whs');
  202. break;
  203. }
  204. setIsModalVisible(true);
  205. };
  206. return (
  207. <>
  208. <ScrollView
  209. showsVerticalScrollIndicator={false}
  210. contentContainerStyle={{ gap: 20, paddingBottom: 32 }}
  211. style={{ paddingTop: 20 }}
  212. >
  213. <InfoItem inline={true} title={'RANKING'}>
  214. <View style={{ flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between' }}>
  215. {scores.map((score, index) => {
  216. let scoreRank =
  217. data.scores?.find((s) => s.name === score && s.score > 0)?.score ?? '-';
  218. if (score === 'YES' && +scoreRank >= 4500) {
  219. scoreRank = '-';
  220. }
  221. return (
  222. <TouchableOpacity
  223. key={index}
  224. style={styles.rankingItem}
  225. onPress={() => handleOpenModal(score)}
  226. >
  227. <Text style={styles.rankingScore}>{scoreRank}</Text>
  228. <Text style={[styles.titleText, { flex: 0 }]}>
  229. {score === 'NM1301' ? 'NM' : score}
  230. </Text>
  231. </TouchableOpacity>
  232. );
  233. })}
  234. </View>
  235. </InfoItem>
  236. {data.series?.length > 0 && (
  237. <InfoItem showMore={showMoreSeries} inline={true} title={'SERIES'}>
  238. {data.series?.slice(0, showMoreSeries ? data.series.length : 8).map((data, index) => (
  239. <Tooltip
  240. isVisible={toolTipVisible === index}
  241. content={<Text style={{}}>{data.name}</Text>}
  242. contentStyle={{ backgroundColor: Colors.FILL_LIGHT }}
  243. placement="top"
  244. onClose={() => setToolTipVisible(null)}
  245. key={index}
  246. backgroundColor="transparent"
  247. allowChildInteraction={false}
  248. >
  249. <TouchableOpacity
  250. style={{ display: 'flex', flexDirection: 'column', gap: 5, alignItems: 'center' }}
  251. onPress={() => setToolTipVisible(index)}
  252. >
  253. <Image
  254. source={{ uri: API_HOST + data.icon_png }}
  255. style={{ width: 28, height: 28 }}
  256. />
  257. <Text style={[styles.headerText, { flex: 0 }]}>{data.score}</Text>
  258. </TouchableOpacity>
  259. </Tooltip>
  260. ))}
  261. </InfoItem>
  262. )}
  263. {data.series?.length > 8 ? (
  264. <TouchableOpacity onPress={() => setShowMoreSeries(!showMoreSeries)}>
  265. <View
  266. style={[
  267. { alignItems: 'center' },
  268. showMoreSeries
  269. ? { transform: 'rotate(180deg)', paddingTop: 8 }
  270. : { paddingBottom: 8 }
  271. ]}
  272. >
  273. <ArrowIcon stroke={'#B7C6CB'} />
  274. </View>
  275. </TouchableOpacity>
  276. ) : null}
  277. {/* <View style={{ display: 'flex', flexDirection: 'row' }}>
  278. <Text style={styles.headerText}>DATE OF BIRTH</Text>
  279. <Text style={styles.titleText}>{new Date(data.date_of_birth).toDateString()}</Text>
  280. </View>
  281. <View style={{ display: 'flex', flexDirection: 'row' }}>
  282. <Text style={styles.headerText}>REGION OF ORIGIN</Text>
  283. <Text style={styles.titleText}>{data.homebase}</Text>
  284. </View>
  285. {data.homebase2 ? (
  286. <View style={{ display: 'flex', flexDirection: 'row' }}>
  287. <Text style={styles.headerText}>SECOND REGION</Text>
  288. <Text style={styles.titleText}>{data.homebase2}</Text>
  289. </View>
  290. ) : null} */}
  291. {data.bio && data.bio.length > 0 && (
  292. <InfoItem title={'BIO'}>
  293. <Text style={[styles.titleText, { flex: 0 }]}>{data.bio}</Text>
  294. </InfoItem>
  295. )}
  296. {hasActiveLinks() && (
  297. <InfoItem title={'SOCIAL LINKS'}>
  298. <View style={styles.linksBox}>
  299. {data.links?.f?.link && data.links?.f?.active !== 0 ? (
  300. <TouchableOpacity onPress={() => handleOpenUrl(data.links?.f?.link)}>
  301. <IconFacebook fill={Colors.DARK_BLUE} />
  302. </TouchableOpacity>
  303. ) : null}
  304. {data.links?.i?.link && data.links?.i?.active !== 0 ? (
  305. <TouchableOpacity onPress={() => handleOpenUrl(data.links?.i?.link)}>
  306. <IconInstagram fill={Colors.DARK_BLUE} />
  307. </TouchableOpacity>
  308. ) : null}
  309. {data.links?.t?.link && data.links?.t?.active !== 0 ? (
  310. <TouchableOpacity onPress={() => handleOpenUrl(data.links?.t?.link)}>
  311. <IconTwitter fill={Colors.DARK_BLUE} />
  312. </TouchableOpacity>
  313. ) : null}
  314. {data.links?.y?.link && data.links?.y?.active !== 0 ? (
  315. <TouchableOpacity onPress={() => handleOpenUrl(data.links?.y?.link)}>
  316. <IconYouTube fill={Colors.DARK_BLUE} />
  317. </TouchableOpacity>
  318. ) : null}
  319. {data.links?.www?.link && data.links?.www?.active !== 0 ? (
  320. <TouchableOpacity onPress={() => handleOpenUrl(data.links?.www?.link)}>
  321. <IconGlobe fill={Colors.DARK_BLUE} />
  322. </TouchableOpacity>
  323. ) : null}
  324. {data.links?.other?.link && data.links?.other?.active !== 0 ? (
  325. <TouchableOpacity onPress={() => handleOpenUrl(data.links?.other?.link)}>
  326. <IconLink fill={Colors.DARK_BLUE} />
  327. </TouchableOpacity>
  328. ) : null}
  329. </View>
  330. </InfoItem>
  331. )}
  332. </ScrollView>
  333. <Modal
  334. isVisible={isModalVisible}
  335. onBackdropPress={() => setIsModalVisible(false)}
  336. onBackButtonPress={() => setIsModalVisible(false)}
  337. style={styles.modal}
  338. statusBarTranslucent={true}
  339. presentationStyle="overFullScreen"
  340. >
  341. <RegionsRenderer type={type} regions={regions} setIsModalVisible={setIsModalVisible} />
  342. </Modal>
  343. </>
  344. );
  345. };
  346. const InfoItem: FC<{
  347. title: string;
  348. inline?: boolean;
  349. children: ReactNode;
  350. showMore?: boolean;
  351. }> = ({ title, inline, children, showMore }) => (
  352. <View>
  353. <Text style={[styles.headerText, { flex: 0 }]}>{title}</Text>
  354. <View
  355. style={[
  356. {
  357. display: 'flex',
  358. flexDirection: inline ? 'row' : 'column',
  359. justifyContent: 'space-evenly',
  360. marginTop: 10
  361. },
  362. showMore ? { flexWrap: 'wrap', gap: 8 } : {}
  363. ]}
  364. >
  365. {children}
  366. </View>
  367. </View>
  368. );
  369. const UnauthenticatedProfileScreen = () => {
  370. const navigation = useNavigation();
  371. return (
  372. <PageWrapper>
  373. <View style={{ marginTop: 15, display: 'flex', gap: 10 }}>
  374. <BigText children={'You are not logged in. Please login or register to access profile.'} />
  375. <Button
  376. onPress={() =>
  377. navigation.dispatch(
  378. CommonActions.reset({
  379. index: 1,
  380. routes: [{ name: NAVIGATION_PAGES.WELCOME }]
  381. })
  382. )
  383. }
  384. variant={ButtonVariants.FILL}
  385. >
  386. Go to login/register
  387. </Button>
  388. </View>
  389. </PageWrapper>
  390. );
  391. };
  392. export default ProfileScreen;