PersonalInfo.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import { FC, useCallback, useEffect, useState } from 'react';
  2. import { TouchableOpacity, View, Text, Image, Dimensions } from 'react-native';
  3. import { Series, usePostGetProfileRegions } from '@api/user';
  4. import { NavigationProp } from '@react-navigation/native';
  5. import Modal from 'react-native-modal';
  6. import Tooltip from 'react-native-walkthrough-tooltip';
  7. import RegionsRenderer from '../RegionsRenderer';
  8. import CompassIcon from 'assets/icons/travels-section/compass.svg';
  9. import FriendsIcon from 'assets/icons/user-group.svg';
  10. import FlagsIcon from 'assets/icons/travels-section/flags.svg';
  11. import PhotosIcon from 'assets/icons/travels-section/images.svg';
  12. import RegionsIcon from 'assets/icons/travels-section/regions.svg';
  13. import SeriesIcon from 'assets/icons/travels-section/series.svg';
  14. import WHSIcon from 'assets/icons/travels-section/whs.svg';
  15. import ArrowIcon from 'assets/icons/next.svg';
  16. import { styles } from './styles';
  17. import { InfoItem } from './InfoItem';
  18. import { Colors } from 'src/theme';
  19. import { API_HOST } from 'src/constants';
  20. import { NAVIGATION_PAGES } from 'src/types';
  21. import { AvatarWithInitials, WarningModal } from 'src/components';
  22. import { usePostFriendRequestMutation, usePostUpdateFriendStatusMutation } from '@api/friends';
  23. import FriendStatus from './FriendStatus';
  24. type PersonalInfoProps = {
  25. data: {
  26. bio: string;
  27. scores: { [key: string]: number | string };
  28. homebase: string;
  29. series: Series[];
  30. friends: {
  31. avatar: string | null;
  32. user_id: number;
  33. first_name: string;
  34. last_name: string;
  35. flag: string;
  36. }[];
  37. firstName: string;
  38. lastName: string;
  39. friendRequestSent: 0 | 1;
  40. friendRequestReceived: 0 | 1;
  41. isFriend: 0 | 1;
  42. friendDbId: number;
  43. ownProfile: 0 | 1;
  44. };
  45. updates: {
  46. countries: number;
  47. dare: number;
  48. friends: number;
  49. new_nm: number;
  50. photos: number;
  51. series: number;
  52. visited_regions: number;
  53. whs: number;
  54. };
  55. userId: number;
  56. navigation: NavigationProp<any>;
  57. isPublicView: boolean;
  58. token: string | null;
  59. };
  60. const AVATAR_SIZE = 28;
  61. const AVATAR_MARGIN = 8;
  62. const SCREEN_PADDING_PERCENT = 0.05;
  63. export const PersonalInfo: FC<PersonalInfoProps> = ({
  64. data,
  65. userId,
  66. updates,
  67. navigation,
  68. isPublicView,
  69. token
  70. }) => {
  71. const [showMoreSeries, setShowMoreSeries] = useState(false);
  72. const [type, setType] = useState<string>('nm');
  73. const [isModalVisible, setIsModalVisible] = useState(false);
  74. const [toolTipVisible, setToolTipVisible] = useState<number | null>(null);
  75. const [tooltipUser, setTooltipUser] = useState<number | null>(null);
  76. const { mutateAsync: sendFriendRequest } = usePostFriendRequestMutation();
  77. const { mutateAsync: updateFriendStatus } = usePostUpdateFriendStatusMutation();
  78. const [friendStatus, setFriendStatus] = useState<number | null>(null);
  79. const [modalInfo, setModalInfo] = useState({
  80. type: 'friend',
  81. message: '',
  82. isVisible: false,
  83. action: () => {},
  84. title: ''
  85. });
  86. const { data: regions } = usePostGetProfileRegions(token as string, userId, type);
  87. useEffect(() => {
  88. if (data.isFriend === 1) {
  89. setFriendStatus(1);
  90. } else if (data.friendRequestReceived === 1) {
  91. setFriendStatus(2);
  92. } else if (data.friendRequestSent === 1) {
  93. setFriendStatus(3);
  94. } else {
  95. setFriendStatus(4);
  96. }
  97. }, [data]);
  98. const scores = [
  99. { name: 'score_nm', score: 'NM' },
  100. { name: 'score_un', score: 'UN' },
  101. { name: 'score_unp', score: 'UN+' },
  102. { name: 'score_mqp', score: 'DARE' },
  103. { name: 'score_tcc', score: 'TCC' },
  104. { name: 'score_deep', score: 'DEEP' },
  105. { name: 'score_slow', score: 'SLOW' },
  106. { name: 'score_yes', score: 'YES' },
  107. { name: 'score_kye', score: 'KYE' },
  108. { name: 'score_whs', score: 'WHS' }
  109. ];
  110. const handleOpenModal = (type: string) => {
  111. switch (type) {
  112. case 'NM':
  113. setType('nm');
  114. break;
  115. case 'DARE':
  116. setType('mqp');
  117. break;
  118. case 'UN':
  119. setType('un');
  120. break;
  121. case 'UN+':
  122. setType('unp');
  123. break;
  124. case 'TCC':
  125. setType('tcc');
  126. break;
  127. case 'SLOW':
  128. setType('slow');
  129. break;
  130. case 'YES':
  131. setType('yes');
  132. break;
  133. case 'WHS':
  134. setType('whs');
  135. break;
  136. case 'KYE':
  137. setType('kye');
  138. case 'DEEP':
  139. setType('deep');
  140. }
  141. setIsModalVisible(true);
  142. };
  143. const hasUpdates = () => {
  144. return (
  145. (updates.countries && updates.countries > 0) ||
  146. (updates.visited_regions && updates.visited_regions > 0) ||
  147. (updates.dare && updates.dare > 0) ||
  148. (updates.series && updates.series > 0) ||
  149. (updates.whs && updates.whs > 0) ||
  150. (updates.new_nm && updates.new_nm > 0) ||
  151. (updates.photos && updates.photos > 0) ||
  152. (updates.friends && updates.friends > 0)
  153. );
  154. };
  155. const handleSendFriendRequest = useCallback(async () => {
  156. await sendFriendRequest(
  157. { token: token as string, uid: userId },
  158. {
  159. onSuccess: () => {
  160. setFriendStatus(3);
  161. }
  162. }
  163. );
  164. }, [sendFriendRequest, token, userId]);
  165. const handleUpdateFriendStatus = useCallback(
  166. async (status: number) => {
  167. await updateFriendStatus(
  168. { token: token as string, id: data.friendDbId, status },
  169. {
  170. onSuccess: () => {
  171. status === -1 || status === 2 ? setFriendStatus(4) : setFriendStatus(status);
  172. }
  173. }
  174. );
  175. },
  176. [updateFriendStatus, token, data.friendDbId]
  177. );
  178. const screenWidth = Dimensions.get('window').width;
  179. const availableWidth = screenWidth * (1 - 2 * SCREEN_PADDING_PERCENT);
  180. const maxAvatars = Math.floor(availableWidth / (AVATAR_SIZE - AVATAR_MARGIN)) - 2;
  181. return (
  182. <>
  183. <View style={styles.wrapper}>
  184. <View style={styles.scoreContainer}>
  185. {scores.map((score, index) => {
  186. let scoreRank = +data.scores[score.name] > 0 ? data.scores[score.name] : '-';
  187. if (score.score === 'YES' && +scoreRank >= 4500) {
  188. scoreRank = '-';
  189. }
  190. return (
  191. <TouchableOpacity
  192. key={index}
  193. style={styles.rankingItem}
  194. disabled={score.score === 'DEEP' || score.score === 'KYE'}
  195. onPress={() => handleOpenModal(score.score)}
  196. >
  197. <Text style={styles.rankingScore}>{scoreRank}</Text>
  198. <Text style={[styles.titleText, { flex: 0 }]}>{score.score}</Text>
  199. </TouchableOpacity>
  200. );
  201. })}
  202. </View>
  203. {isPublicView && token ? (
  204. <FriendStatus
  205. status={friendStatus}
  206. data={data}
  207. setModalInfo={setModalInfo}
  208. handleSendFriendRequest={handleSendFriendRequest}
  209. handleUpdateFriendStatus={handleUpdateFriendStatus}
  210. />
  211. ) : null}
  212. {data.friends.length > 0 ? (
  213. <InfoItem inline={true} title={'FRIENDS'}>
  214. <View style={{ flexDirection: 'row', flex: 1 }}>
  215. {data.friends.slice(0, maxAvatars).map((friend, index) => (
  216. <Tooltip
  217. isVisible={tooltipUser === index}
  218. content={
  219. <TouchableOpacity
  220. onPress={() => {
  221. setTooltipUser(null);
  222. navigation.navigate(
  223. ...([
  224. NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW,
  225. { userId: friend.user_id }
  226. ] as never)
  227. );
  228. }}
  229. >
  230. <Text style={{}}>
  231. {friend.first_name} {friend.last_name}
  232. </Text>
  233. </TouchableOpacity>
  234. }
  235. contentStyle={{ backgroundColor: Colors.FILL_LIGHT }}
  236. placement="top"
  237. onClose={() => setTooltipUser(null)}
  238. key={index}
  239. backgroundColor="transparent"
  240. >
  241. <TouchableOpacity
  242. onPress={() => setTooltipUser(index)}
  243. style={{ marginLeft: index === 0 ? 0 : -AVATAR_MARGIN }}
  244. >
  245. {friend.avatar ? (
  246. <Image
  247. style={styles.avatar}
  248. source={{ uri: API_HOST + '/img/avatars/' + friend.avatar }}
  249. />
  250. ) : (
  251. <AvatarWithInitials
  252. text={`${friend.first_name[0] ?? ''}${friend.last_name[0] ?? ''}`}
  253. flag={API_HOST + '/img/flags_new/' + friend.flag}
  254. size={28}
  255. borderColor={Colors.DARK_LIGHT}
  256. fontSize={11}
  257. borderWidth={1}
  258. />
  259. )}
  260. </TouchableOpacity>
  261. </Tooltip>
  262. ))}
  263. </View>
  264. <Tooltip
  265. isVisible={tooltipUser === -1}
  266. content={<Text style={{}}>Only friends can see details.</Text>}
  267. contentStyle={{ backgroundColor: Colors.FILL_LIGHT, justifyContent: 'flex-end' }}
  268. placement="top"
  269. onClose={() => setTooltipUser(null)}
  270. backgroundColor="transparent"
  271. allowChildInteraction={false}
  272. >
  273. <TouchableOpacity
  274. style={[styles.avatar, styles.userShowMore]}
  275. onPress={() => {
  276. if (friendStatus !== 1 && isPublicView) {
  277. setTooltipUser(-1);
  278. } else {
  279. data.ownProfile === 0
  280. ? navigation.navigate(
  281. ...([
  282. NAVIGATION_PAGES.FRIENDS_LIST,
  283. {
  284. id: userId,
  285. type: 'friends'
  286. }
  287. ] as never)
  288. )
  289. : navigation.navigate(NAVIGATION_PAGES.MY_FRIENDS);
  290. }
  291. }}
  292. >
  293. <View style={styles.dots}></View>
  294. <View style={styles.dots}></View>
  295. <View style={styles.dots}></View>
  296. </TouchableOpacity>
  297. </Tooltip>
  298. </InfoItem>
  299. ) : null}
  300. {hasUpdates() ? (
  301. <InfoItem title={'UPDATES (last 90 days)'}>
  302. <View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
  303. {updates.countries && updates.countries > 0 ? (
  304. <View style={styles.updates}>
  305. <FlagsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  306. <View>
  307. <Text style={styles.updatesTextCount}>+{updates.countries}</Text>
  308. <Text style={styles.updatesText}>visited countries</Text>
  309. </View>
  310. </View>
  311. ) : null}
  312. {updates.visited_regions && updates.visited_regions > 0 ? (
  313. <View style={styles.updates}>
  314. <RegionsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  315. <View>
  316. <Text style={styles.updatesTextCount}>+{updates.visited_regions}</Text>
  317. <Text style={styles.updatesText}>visited regions</Text>
  318. </View>
  319. </View>
  320. ) : null}
  321. {updates.dare && updates.dare > 0 ? (
  322. <View style={styles.updates}>
  323. <CompassIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  324. <View>
  325. <Text style={styles.updatesTextCount}>+{updates.dare}</Text>
  326. <Text style={styles.updatesText}>new DARE places</Text>
  327. </View>
  328. </View>
  329. ) : null}
  330. {updates.series && updates.series > 0 ? (
  331. <View style={styles.updates}>
  332. <SeriesIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  333. <View>
  334. <Text style={styles.updatesTextCount}>+{updates.series}</Text>
  335. <Text style={styles.updatesText}>new series</Text>
  336. </View>
  337. </View>
  338. ) : null}
  339. {updates.whs && updates.whs > 0 ? (
  340. <View style={styles.updates}>
  341. <WHSIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  342. <View>
  343. <Text style={styles.updatesTextCount}>+{updates.whs}</Text>
  344. <Text style={styles.updatesText}>new WHS sites</Text>
  345. </View>
  346. </View>
  347. ) : null}
  348. {updates.new_nm && updates.new_nm > 0 ? (
  349. <View style={styles.updates}>
  350. <RegionsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  351. <View>
  352. <Text style={styles.updatesTextCount}>+{updates.new_nm}</Text>
  353. <Text style={styles.updatesText}>new NM regions</Text>
  354. </View>
  355. </View>
  356. ) : null}
  357. {updates.photos && updates.photos > 0 ? (
  358. <View style={styles.updates}>
  359. <PhotosIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  360. <View>
  361. <Text style={styles.updatesTextCount}>+{updates.photos}</Text>
  362. <Text style={styles.updatesText}>new photos</Text>
  363. </View>
  364. </View>
  365. ) : null}
  366. {updates.friends && updates.friends > 0 ? (
  367. <View style={styles.updates}>
  368. <FriendsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  369. <View>
  370. <Text style={styles.updatesTextCount}>+{updates.friends}</Text>
  371. <Text style={styles.updatesText}>new friends</Text>
  372. </View>
  373. </View>
  374. ) : null}
  375. </View>
  376. </InfoItem>
  377. ) : null}
  378. {data.series?.length > 0 && (
  379. <InfoItem showMore={showMoreSeries} inline={true} title={'SERIES'}>
  380. {data.series?.slice(0, showMoreSeries ? data.series.length : 8).map((data, index) => (
  381. <Tooltip
  382. isVisible={toolTipVisible === index}
  383. content={<Text style={{}}>{data.name}</Text>}
  384. contentStyle={{ backgroundColor: Colors.FILL_LIGHT }}
  385. placement="top"
  386. onClose={() => setToolTipVisible(null)}
  387. key={index}
  388. backgroundColor="transparent"
  389. allowChildInteraction={false}
  390. >
  391. <TouchableOpacity style={styles.series} onPress={() => setToolTipVisible(index)}>
  392. <Image
  393. source={{ uri: API_HOST + data.app_icon }}
  394. style={{ width: 28, height: 28 }}
  395. />
  396. <Text style={[styles.headerText, { flex: 0 }]}>{data.score}</Text>
  397. </TouchableOpacity>
  398. </Tooltip>
  399. ))}
  400. </InfoItem>
  401. )}
  402. {data.series?.length > 8 ? (
  403. <TouchableOpacity onPress={() => setShowMoreSeries(!showMoreSeries)}>
  404. <View
  405. style={[
  406. { alignItems: 'center' },
  407. showMoreSeries
  408. ? { transform: 'rotate(180deg)', paddingTop: 8 }
  409. : { paddingBottom: 8 }
  410. ]}
  411. >
  412. <ArrowIcon stroke={'#B7C6CB'} />
  413. </View>
  414. </TouchableOpacity>
  415. ) : null}
  416. <View style={{ display: 'flex', flexDirection: 'row' }}>
  417. <Text style={styles.headerText}>REGION OF ORIGIN</Text>
  418. <Text style={styles.titleText}>{data.homebase}</Text>
  419. </View>
  420. {data.bio && data.bio.length > 0 && (
  421. <InfoItem title={'BIO'}>
  422. <Text style={[styles.titleText, { flex: 0 }]}>{data.bio}</Text>
  423. </InfoItem>
  424. )}
  425. </View>
  426. <Modal
  427. isVisible={isModalVisible}
  428. onBackdropPress={() => setIsModalVisible(false)}
  429. onBackButtonPress={() => setIsModalVisible(false)}
  430. style={styles.modal}
  431. statusBarTranslucent={true}
  432. presentationStyle="overFullScreen"
  433. >
  434. <RegionsRenderer type={type} regions={regions} setIsModalVisible={setIsModalVisible} />
  435. </Modal>
  436. <WarningModal
  437. type={modalInfo.type}
  438. isVisible={modalInfo.isVisible}
  439. message={modalInfo.message}
  440. action={modalInfo.action}
  441. onClose={() => setModalInfo({ ...modalInfo, isVisible: false })}
  442. title=""
  443. />
  444. </>
  445. );
  446. };