PersonalInfo.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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(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_mqp', score: 'DARE' },
  101. { name: 'score_un', score: 'UN' },
  102. { name: 'score_unp', score: 'UN+' },
  103. { name: 'score_tcc', score: 'TCC' },
  104. { name: 'score_deep', score: 'DEEP' },
  105. { name: 'score_yes', score: 'YES' },
  106. { name: 'score_slow', score: 'SLOW' },
  107. { name: 'score_whs', score: 'WHS' },
  108. { name: 'score_kye', score: 'KYE' }
  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. navigation.navigate(
  222. ...([
  223. NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW,
  224. { userId: friend.user_id }
  225. ] as never)
  226. )
  227. }
  228. >
  229. <Text style={{}}>
  230. {friend.first_name} {friend.last_name}
  231. </Text>
  232. </TouchableOpacity>
  233. }
  234. contentStyle={{ backgroundColor: Colors.FILL_LIGHT }}
  235. placement="top"
  236. onClose={() => setTooltipUser(null)}
  237. key={index}
  238. backgroundColor="transparent"
  239. >
  240. <TouchableOpacity
  241. onPress={() => setTooltipUser(index)}
  242. style={{ marginLeft: index === 0 ? 0 : -AVATAR_MARGIN }}
  243. >
  244. {friend.avatar ? (
  245. <Image
  246. style={styles.avatar}
  247. source={{ uri: API_HOST + '/img/avatars/' + friend.avatar }}
  248. />
  249. ) : (
  250. <AvatarWithInitials
  251. text={`${friend.first_name[0] ?? ''}${friend.last_name[0] ?? ''}`}
  252. flag={API_HOST + '/img/flags_new/' + friend.flag}
  253. size={28}
  254. borderColor={Colors.DARK_LIGHT}
  255. fontSize={11}
  256. borderWidth={1}
  257. />
  258. )}
  259. </TouchableOpacity>
  260. </Tooltip>
  261. ))}
  262. </View>
  263. <Tooltip
  264. isVisible={tooltipUser === -1}
  265. content={<Text style={{}}>Only friends can see details.</Text>}
  266. contentStyle={{ backgroundColor: Colors.FILL_LIGHT, justifyContent: 'flex-end' }}
  267. placement="top"
  268. onClose={() => setTooltipUser(null)}
  269. backgroundColor="transparent"
  270. allowChildInteraction={false}
  271. >
  272. <TouchableOpacity
  273. style={[styles.avatar, styles.userShowMore]}
  274. onPress={() => {
  275. if (friendStatus !== 1 && isPublicView) {
  276. setTooltipUser(-1);
  277. } else {
  278. data.ownProfile === 0
  279. ? navigation.navigate(
  280. ...([
  281. NAVIGATION_PAGES.FRIENDS_LIST,
  282. {
  283. id: userId,
  284. type: 'friends'
  285. }
  286. ] as never)
  287. )
  288. : navigation.navigate(NAVIGATION_PAGES.MY_FRIENDS);
  289. }
  290. }}
  291. >
  292. <View style={styles.dots}></View>
  293. <View style={styles.dots}></View>
  294. <View style={styles.dots}></View>
  295. </TouchableOpacity>
  296. </Tooltip>
  297. </InfoItem>
  298. ) : null}
  299. {hasUpdates() ? (
  300. <InfoItem title={'UPDATES (last 90 days)'}>
  301. <View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
  302. {updates.countries && updates.countries > 0 ? (
  303. <View style={styles.updates}>
  304. <FlagsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  305. <View>
  306. <Text style={styles.updatesTextCount}>+{updates.countries}</Text>
  307. <Text style={styles.updatesText}>visited countries</Text>
  308. </View>
  309. </View>
  310. ) : null}
  311. {updates.visited_regions && updates.visited_regions > 0 ? (
  312. <View style={styles.updates}>
  313. <RegionsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  314. <View>
  315. <Text style={styles.updatesTextCount}>+{updates.visited_regions}</Text>
  316. <Text style={styles.updatesText}>visited regions</Text>
  317. </View>
  318. </View>
  319. ) : null}
  320. {updates.dare && updates.dare > 0 ? (
  321. <View style={styles.updates}>
  322. <CompassIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  323. <View>
  324. <Text style={styles.updatesTextCount}>+{updates.dare}</Text>
  325. <Text style={styles.updatesText}>new DARE places</Text>
  326. </View>
  327. </View>
  328. ) : null}
  329. {updates.series && updates.series > 0 ? (
  330. <View style={styles.updates}>
  331. <SeriesIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  332. <View>
  333. <Text style={styles.updatesTextCount}>+{updates.series}</Text>
  334. <Text style={styles.updatesText}>new series</Text>
  335. </View>
  336. </View>
  337. ) : null}
  338. {updates.whs && updates.whs > 0 ? (
  339. <View style={styles.updates}>
  340. <WHSIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  341. <View>
  342. <Text style={styles.updatesTextCount}>+{updates.whs}</Text>
  343. <Text style={styles.updatesText}>new WHS sites</Text>
  344. </View>
  345. </View>
  346. ) : null}
  347. {updates.new_nm && updates.new_nm > 0 ? (
  348. <View style={styles.updates}>
  349. <RegionsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  350. <View>
  351. <Text style={styles.updatesTextCount}>+{updates.new_nm}</Text>
  352. <Text style={styles.updatesText}>new NM regions</Text>
  353. </View>
  354. </View>
  355. ) : null}
  356. {updates.photos && updates.photos > 0 ? (
  357. <View style={styles.updates}>
  358. <PhotosIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  359. <View>
  360. <Text style={styles.updatesTextCount}>+{updates.photos}</Text>
  361. <Text style={styles.updatesText}>new photos</Text>
  362. </View>
  363. </View>
  364. ) : null}
  365. {updates.friends && updates.friends > 0 ? (
  366. <View style={styles.updates}>
  367. <FriendsIcon fill={Colors.DARK_BLUE} height={20} width={20} />
  368. <View>
  369. <Text style={styles.updatesTextCount}>+{updates.friends}</Text>
  370. <Text style={styles.updatesText}>new friends</Text>
  371. </View>
  372. </View>
  373. ) : null}
  374. </View>
  375. </InfoItem>
  376. ) : null}
  377. {data.series?.length > 0 && (
  378. <InfoItem showMore={showMoreSeries} inline={true} title={'SERIES'}>
  379. {data.series?.slice(0, showMoreSeries ? data.series.length : 8).map((data, index) => (
  380. <Tooltip
  381. isVisible={toolTipVisible === index}
  382. content={<Text style={{}}>{data.name}</Text>}
  383. contentStyle={{ backgroundColor: Colors.FILL_LIGHT }}
  384. placement="top"
  385. onClose={() => setToolTipVisible(null)}
  386. key={index}
  387. backgroundColor="transparent"
  388. allowChildInteraction={false}
  389. >
  390. <TouchableOpacity style={styles.series} onPress={() => setToolTipVisible(index)}>
  391. <Image
  392. source={{ uri: API_HOST + data.app_icon }}
  393. style={{ width: 28, height: 28 }}
  394. />
  395. <Text style={[styles.headerText, { flex: 0 }]}>{data.score}</Text>
  396. </TouchableOpacity>
  397. </Tooltip>
  398. ))}
  399. </InfoItem>
  400. )}
  401. {data.series?.length > 8 ? (
  402. <TouchableOpacity onPress={() => setShowMoreSeries(!showMoreSeries)}>
  403. <View
  404. style={[
  405. { alignItems: 'center' },
  406. showMoreSeries
  407. ? { transform: 'rotate(180deg)', paddingTop: 8 }
  408. : { paddingBottom: 8 }
  409. ]}
  410. >
  411. <ArrowIcon stroke={'#B7C6CB'} />
  412. </View>
  413. </TouchableOpacity>
  414. ) : null}
  415. <View style={{ display: 'flex', flexDirection: 'row' }}>
  416. <Text style={styles.headerText}>REGION OF ORIGIN</Text>
  417. <Text style={styles.titleText}>{data.homebase}</Text>
  418. </View>
  419. {data.bio && data.bio.length > 0 && (
  420. <InfoItem title={'BIO'}>
  421. <Text style={[styles.titleText, { flex: 0 }]}>{data.bio}</Text>
  422. </InfoItem>
  423. )}
  424. </View>
  425. <Modal
  426. isVisible={isModalVisible}
  427. onBackdropPress={() => setIsModalVisible(false)}
  428. onBackButtonPress={() => setIsModalVisible(false)}
  429. style={styles.modal}
  430. statusBarTranslucent={true}
  431. presentationStyle="overFullScreen"
  432. >
  433. <RegionsRenderer type={type} regions={regions} setIsModalVisible={setIsModalVisible} />
  434. </Modal>
  435. <WarningModal
  436. type={modalInfo.type}
  437. isVisible={modalInfo.isVisible}
  438. message={modalInfo.message}
  439. action={modalInfo.action}
  440. onClose={() => setModalInfo({ ...modalInfo, isVisible: false })}
  441. title=""
  442. />
  443. </>
  444. );
  445. };