index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { useCallback, useEffect, useRef } from 'react';
  2. import { Text, TouchableOpacity, View, Image, Animated, Dimensions } from 'react-native';
  3. import MarkIcon from 'assets/icons/mark.svg';
  4. import EditSvg from 'assets/icons/travels-screens/pen-to-square.svg';
  5. import CalendarSvg from 'assets/icons/travels-screens/calendar.svg';
  6. import RotateSvg from 'assets/icons/travels-screens/rotate.svg';
  7. import CheckSvg from 'assets/icons/travels-screens/circle-check.svg';
  8. import CheckRegularSvg from 'assets/icons/travels-screens/circle-check-regular.svg';
  9. import { styles } from './style';
  10. import React from 'react';
  11. import { Colors } from 'src/theme';
  12. import { useFocusEffect, useNavigation } from '@react-navigation/native';
  13. import { NAVIGATION_PAGES } from 'src/types';
  14. interface Region {
  15. id: number;
  16. name: string;
  17. region_photos: string;
  18. visitors_count: number;
  19. }
  20. interface RegionPopupProps {
  21. region: Region;
  22. userAvatars: string[];
  23. userData: any;
  24. openEditModal: () => void;
  25. updateNM: (region: number, first: number, last: number, visits: number, quality: number) => void;
  26. updateDare: (region: number, visits: 0 | 1) => void;
  27. disabled?: boolean;
  28. updateSlow: (id: number, v: boolean, s11: boolean, s31: boolean, s101: boolean) => void;
  29. openEditSlowModal: () => void;
  30. }
  31. const RegionPopup: React.FC<RegionPopupProps> = ({
  32. region,
  33. userAvatars,
  34. userData,
  35. openEditModal,
  36. updateNM,
  37. updateDare,
  38. disabled,
  39. updateSlow,
  40. openEditSlowModal
  41. }) => {
  42. const fadeAnim = useRef(new Animated.Value(0)).current;
  43. const navigation = useNavigation();
  44. useEffect(() => {
  45. Animated.timing(fadeAnim, {
  46. toValue: 1,
  47. duration: 300,
  48. useNativeDriver: true
  49. }).start();
  50. }, [fadeAnim]);
  51. const splitRegionName = (fullName: string) => {
  52. const [name, ...rest] = fullName?.split(/ – | - /);
  53. const subname = rest?.join(' - ');
  54. return {
  55. regionTitle: name,
  56. regionSubtitle: subname ? subname : ''
  57. };
  58. };
  59. const { regionTitle, regionSubtitle } = splitRegionName(region.name);
  60. const regionImg = JSON.parse(region.region_photos)[0];
  61. function formatNumber(number: number) {
  62. if (number >= 1000 && number < 10000) {
  63. return (number / 1000).toFixed(1) + 'k';
  64. } else if (number >= 10000) {
  65. return (number / 1000).toFixed(0) + 'k';
  66. }
  67. return number.toString();
  68. }
  69. const formattedCount = formatNumber(region.visitors_count);
  70. const renderDurationIcon = (condition: 0 | 1) =>
  71. condition ? <CheckSvg fill={Colors.DARK_BLUE} /> : <CheckRegularSvg />;
  72. const isSmallScreen = Dimensions.get('window').width < 383;
  73. return (
  74. <Animated.View style={[styles.popupContainer, { opacity: fadeAnim }]}>
  75. <TouchableOpacity
  76. onPress={() => {
  77. if (userData?.type === 'countries') {
  78. navigation.navigate(
  79. ...([
  80. NAVIGATION_PAGES.COUNTRY_PREVIEW,
  81. {
  82. regionId: region.id,
  83. type: 'country',
  84. disabled
  85. }
  86. ] as never)
  87. );
  88. } else {
  89. navigation.navigate(
  90. ...([
  91. NAVIGATION_PAGES.REGION_PREVIEW,
  92. {
  93. regionId: region.id,
  94. type: userData?.type,
  95. disabled
  96. }
  97. ] as never)
  98. );
  99. }
  100. }}
  101. style={{ flex: 1 }}
  102. >
  103. <View style={styles.regionInfoContainer}>
  104. {regionImg && <Image source={{ uri: regionImg }} style={styles.regionImage} />}
  105. <View style={styles.regionTextContainer}>
  106. <Text style={styles.regionTitle}>{regionTitle}</Text>
  107. {regionSubtitle && <Text style={styles.regionSubtitle}>{regionSubtitle}</Text>}
  108. {userData?.type === 'countries' && (
  109. <View style={styles.infoContainer}>
  110. <Text style={styles.infoText} numberOfLines={2}>
  111. {userData?.info_text}
  112. </Text>
  113. {!userData?.visited && (
  114. <TouchableOpacity
  115. onPress={() =>
  116. navigation.navigate(
  117. ...([
  118. NAVIGATION_PAGES.EDIT_COUNTRY_DATA,
  119. {
  120. countryId: region.id,
  121. countryName: regionTitle,
  122. countryFlag: userData?.flag,
  123. slow11: userData?.slow11,
  124. slow31: userData?.slow31,
  125. slow101: userData?.slow101,
  126. isSlowList: false
  127. }
  128. ] as never)
  129. )
  130. }
  131. style={styles.editRegionBtn}
  132. hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
  133. >
  134. <EditSvg width={14} height={14} />
  135. </TouchableOpacity>
  136. )}
  137. </View>
  138. )}
  139. </View>
  140. </View>
  141. <View>
  142. <View style={styles.separator} />
  143. </View>
  144. <View style={styles.bottomContainer}>
  145. <View style={styles.userContainer}>
  146. {userData?.visited && userData?.first_visit_year > 1 && !disabled && (
  147. <View style={styles.infoContent}>
  148. <CalendarSvg height={18} width={18} fill={Colors.DARK_BLUE} />
  149. <Text style={styles.visitedButtonText}>{userData?.first_visit_year}</Text>
  150. </View>
  151. )}
  152. {userData?.visited && userData?.type === 'nm' && !disabled && (
  153. <View style={styles.infoContent}>
  154. <RotateSvg fill={Colors.DARK_BLUE} />
  155. <Text style={styles.visitedButtonText}>
  156. {userData.no_of_visits >= 10 ? '10+' : userData.no_of_visits}
  157. </Text>
  158. </View>
  159. )}
  160. {userData?.visited && userData?.type === 'countries' && !disabled && (
  161. <View style={styles.durationContainer}>
  162. <View style={styles.durationItem}>
  163. {renderDurationIcon(userData.slow11)}
  164. <View style={[styles.slowText, isSmallScreen ? { flexDirection: 'column' } : {}]}>
  165. <Text style={styles.visitDuration}>11+ </Text>
  166. <Text style={styles.visitDuration}>days</Text>
  167. </View>
  168. </View>
  169. <View style={styles.durationItem}>
  170. {renderDurationIcon(userData.slow31)}
  171. <View style={[styles.slowText, isSmallScreen ? { flexDirection: 'column' } : {}]}>
  172. <Text style={styles.visitDuration}>31+ </Text>
  173. <Text style={styles.visitDuration}>days</Text>
  174. </View>
  175. </View>
  176. <View style={styles.durationItem}>
  177. {renderDurationIcon(userData.slow101)}
  178. <View style={[styles.slowText, isSmallScreen ? { flexDirection: 'column' } : {}]}>
  179. <Text style={styles.visitDuration}>101+ </Text>
  180. <Text style={styles.visitDuration}>days</Text>
  181. </View>
  182. </View>
  183. </View>
  184. )}
  185. {(!userData?.visited || userData?.type === 'dare' || disabled) && (
  186. <View style={styles.userImageContainer}>
  187. {userAvatars?.map((avatar, index) => (
  188. <Image key={index} source={{ uri: avatar }} style={styles.userImage} />
  189. ))}
  190. <View style={styles.userCountContainer}>
  191. <Text style={styles.userCount}>{formattedCount}</Text>
  192. </View>
  193. </View>
  194. )}
  195. </View>
  196. <View style={styles.btnContainer}>
  197. {userData?.visited &&
  198. userData?.type !== 'dare' &&
  199. !disabled &&
  200. (userData?.visited_regions <= 1 || userData?.type === 'nm') ? (
  201. <TouchableOpacity
  202. onPress={
  203. userData?.type === 'countries'
  204. ? () =>
  205. navigation.navigate(
  206. ...([
  207. NAVIGATION_PAGES.EDIT_COUNTRY_DATA,
  208. {
  209. countryId: region.id,
  210. countryName: regionTitle,
  211. countryFlag: userData?.flag,
  212. slow11: userData?.slow11,
  213. slow31: userData?.slow31,
  214. slow101: userData?.slow101,
  215. isSlowList: false
  216. }
  217. ] as never)
  218. )
  219. : openEditModal
  220. }
  221. style={styles.editBtn}
  222. >
  223. <EditSvg width={14} height={14} />
  224. </TouchableOpacity>
  225. ) : null}
  226. <TouchableOpacity
  227. style={[
  228. styles.btn,
  229. userData?.visited && !disabled ? styles.visitedButton : styles.markVisitedButton
  230. ]}
  231. onPress={() => {
  232. if (
  233. userData?.visited &&
  234. !disabled &&
  235. userData?.type === 'countries' &&
  236. userData?.visited_regions > 1
  237. ) {
  238. navigation.navigate(
  239. ...([
  240. NAVIGATION_PAGES.EDIT_COUNTRY_DATA,
  241. {
  242. countryId: region.id,
  243. countryName: regionTitle,
  244. countryFlag: userData?.flag,
  245. slow11: userData?.slow11,
  246. slow31: userData?.slow31,
  247. slow101: userData?.slow101,
  248. isSlowList: false
  249. }
  250. ] as never)
  251. );
  252. return;
  253. }
  254. userData?.type === 'nm'
  255. ? updateNM(
  256. region.id,
  257. userData.visited ? 0 : 1,
  258. userData.visited ? 0 : 1,
  259. userData.visited ? 0 : 1,
  260. 3
  261. )
  262. : userData?.type === 'countries'
  263. ? updateSlow(
  264. region.id,
  265. !userData.visited,
  266. Boolean(userData.slow11),
  267. Boolean(userData.slow31),
  268. Boolean(userData.slow101)
  269. )
  270. : updateDare(region.id, userData.visited ? 0 : 1);
  271. }}
  272. >
  273. {userData?.visited &&
  274. !disabled &&
  275. userData?.type === 'countries' &&
  276. userData?.visited_regions > 1 ? (
  277. <View style={styles.visitedContainer}>
  278. <EditSvg width={14} height={14} />
  279. <Text style={styles.visitedButtonText}>Edit</Text>
  280. </View>
  281. ) : userData?.visited && !disabled ? (
  282. <View style={styles.visitedContainer}>
  283. <MarkIcon width={16} height={16} />
  284. <Text style={styles.visitedButtonText}>Visited</Text>
  285. </View>
  286. ) : (
  287. <Text style={[styles.markVisitedButtonText]}>Mark Visited</Text>
  288. )}
  289. </TouchableOpacity>
  290. </View>
  291. </View>
  292. </TouchableOpacity>
  293. </Animated.View>
  294. );
  295. };
  296. export default React.memo(RegionPopup);