index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import React, { FC, useCallback, useEffect, useState } from 'react';
  2. import { View, Text, Image, TouchableOpacity, Platform } from 'react-native';
  3. import ImageView from 'better-react-native-image-viewing';
  4. import { styles } from '../RegionViewScreen/styles';
  5. import { Button, HorizontalTabView, Loading, Modal as ReactModal } from 'src/components';
  6. import { CommonActions, useFocusEffect } from '@react-navigation/native';
  7. import { Colors } from 'src/theme';
  8. import { ScrollView } from 'react-native-gesture-handler';
  9. import { styles as ButtonStyles } from 'src/components/RegionPopup/style';
  10. import { usePostSetToggleItem } from '@api/series';
  11. import { NAVIGATION_PAGES } from 'src/types';
  12. import { API_HOST } from 'src/constants';
  13. import { StoreType, storage } from 'src/storage';
  14. import { ButtonVariants } from 'src/types/components';
  15. import { useRegion } from 'src/contexts/RegionContext';
  16. import formatNumber from '../../TravelsScreen/utils/formatNumber';
  17. import { PhotosData, Props, SeriesData, SeriesGroup, SeriesItem } from '../RegionViewScreen/types';
  18. import ImageCarousel from '../RegionViewScreen/ImageCarousel';
  19. import TravelSeriesList from '../RegionViewScreen/TravelSeriesList';
  20. import { useGetCountryDataQuery } from '@api/countries';
  21. import EditModal from '../../TravelsScreen/Components/EditSlowModal';
  22. import MarkIcon from 'assets/icons/mark.svg';
  23. import ChevronLeft from 'assets/icons/chevron-left.svg';
  24. import CaseSvg from 'assets/icons/briefcase.svg';
  25. import HouseSvg from 'assets/icons/house.svg';
  26. import EditSvg from 'assets/icons/travels-screens/pen-to-square.svg';
  27. import CheckSvg from 'assets/icons/travels-screens/circle-check.svg';
  28. import CheckRegularSvg from 'assets/icons/travels-screens/circle-check-regular.svg';
  29. import MapSvg from 'assets/icons/travels-screens/map-location.svg';
  30. const CountryViewScreen: FC<Props> = ({ navigation, route }) => {
  31. const countryId = route.params?.regionId;
  32. const disabled = route.params?.disabled;
  33. const token = storage.get('token', StoreType.STRING) as string;
  34. const [isLoading, setIsLoading] = useState(true);
  35. const [isModalVisible, setModalVisible] = useState(false);
  36. const [currentImageIndex, setCurrentImageIndex] = useState(0);
  37. const [activeIndex, setActiveIndex] = useState(0);
  38. const [indexSeries, setIndexSeries] = useState(0);
  39. const [routes, setRoutes] = useState<SeriesGroup[]>([]);
  40. const [series, setSeries] = useState<SeriesData[]>([]);
  41. const [photos, setPhotos] = useState<PhotosData[]>([]);
  42. const [name, setName] = useState('');
  43. const { data } = useGetCountryDataQuery(countryId, true, token && token);
  44. const { mutate: updateSeriesItem } = usePostSetToggleItem();
  45. const [isInfoModalVisible, setIsInfoModalVisible] = useState<boolean>(false);
  46. const [infoItem, setInfoItem] = useState<SeriesItem | null>(null);
  47. const [isEditSlowModalVisible, setIsEditSlowModalVisible] = useState<boolean>(false);
  48. const {
  49. handleUpdateSlow: updateSlow,
  50. userData: regionData,
  51. setUserData: setRegionData,
  52. handleUpdateSlowList
  53. } = useRegion();
  54. useEffect(() => {
  55. navigation.getParent()?.setOptions({
  56. tabBarStyle: {
  57. display: 'flex',
  58. ...Platform.select({
  59. android: {
  60. height: 58
  61. }
  62. })
  63. }
  64. });
  65. }, [navigation]);
  66. useFocusEffect(
  67. useCallback(() => {
  68. const fetchGroups = async () => {
  69. let staticGroups = [];
  70. const routesData = data?.data?.series?.map((item) => ({
  71. key: item.series_id?.toString(),
  72. title: item.series_name,
  73. series_id: item.series_id,
  74. icon: item.icon,
  75. items: item.items
  76. }));
  77. routesData && staticGroups.push(...routesData);
  78. setPhotos(
  79. data?.data?.photos?.map((item) => ({
  80. ...item,
  81. uriSmall: `${API_HOST}/ajax/pic/${item.id}/small`,
  82. uri: `${API_HOST}/ajax/pic/${item.id}/full`
  83. })) ?? []
  84. );
  85. setName(data?.data.name ?? '');
  86. setSeries(data?.data?.series || []);
  87. setRoutes(staticGroups);
  88. if (regionData?.id !== countryId) {
  89. setRegionData(data?.data || {});
  90. }
  91. setIsLoading(false);
  92. };
  93. if (data && data.result === 'OK') {
  94. fetchGroups();
  95. }
  96. }, [data])
  97. );
  98. const handleCheckboxChange = useCallback(
  99. async (item: SeriesItem, double: boolean, seriesId: number) => {
  100. setSeries((currentData) => {
  101. const groupIndex = currentData.findIndex((group) => group?.series_id === seriesId);
  102. if (groupIndex === -1) return currentData;
  103. const newData = [...currentData];
  104. const newGroup = { ...newData[groupIndex] };
  105. newGroup.items = newGroup.items.map((subItem) =>
  106. subItem.id === item.id
  107. ? {
  108. ...subItem,
  109. ...(double
  110. ? { visited_double: subItem.visited_double === 0 ? 1 : 0 }
  111. : { visited: subItem.visited === 0 ? 1 : 0 })
  112. }
  113. : subItem
  114. );
  115. newData[groupIndex] = newGroup;
  116. return newData;
  117. });
  118. const itemData = {
  119. token: token,
  120. series_id: seriesId,
  121. item_id: item.id,
  122. checked: (item.visited === 1 ? 0 : 1) as 0 | 1,
  123. double: (double && !item.visited_double ? 1 : 0) as 0 | 1
  124. };
  125. try {
  126. updateSeriesItem(itemData);
  127. } catch (error) {
  128. console.error('Failed to update checkbox state', error);
  129. }
  130. },
  131. [token, updateSeriesItem]
  132. );
  133. const openModal = (index: number) => {
  134. setCurrentImageIndex(index);
  135. setModalVisible(true);
  136. };
  137. if (isLoading) return <Loading />;
  138. const handleUpdateSlowModal = (
  139. id: number,
  140. v: boolean,
  141. s11: boolean,
  142. s31: boolean,
  143. s101: boolean
  144. ) => {
  145. const updatedSlow = {
  146. ...regionData,
  147. visited: v,
  148. slow11: Number(s11) as 0 | 1,
  149. slow31: Number(s31) as 0 | 1,
  150. slow101: Number(s101) as 0 | 1
  151. };
  152. const updatedSlowData = {
  153. token,
  154. id,
  155. v,
  156. s11,
  157. s31,
  158. s101
  159. };
  160. route.params?.isTravelsScreen
  161. ? handleUpdateSlowList(id, v, s11, s31, s101)
  162. : updateSlow(id, v, s11, s31, s101);
  163. updatedSlow && setRegionData(updatedSlow);
  164. };
  165. const handleUpdateSlow = () => {
  166. route.params?.isTravelsScreen
  167. ? handleUpdateSlowList(
  168. countryId,
  169. !regionData.visited,
  170. Boolean(regionData.slow11),
  171. Boolean(regionData.slow31),
  172. Boolean(regionData.slow101)
  173. )
  174. : updateSlow(
  175. countryId,
  176. !regionData.visited,
  177. Boolean(regionData.slow11),
  178. Boolean(regionData.slow31),
  179. Boolean(regionData.slow101)
  180. );
  181. setRegionData({
  182. ...regionData,
  183. visited: !regionData.visited
  184. });
  185. };
  186. const renderDurationIcon = (condition: 0 | 1) =>
  187. condition ? <CheckSvg fill={Colors.DARK_BLUE} /> : <CheckRegularSvg />;
  188. return (
  189. <View style={styles.container}>
  190. <TouchableOpacity
  191. onPress={() => {
  192. navigation.goBack();
  193. }}
  194. style={styles.backButton}
  195. >
  196. <View style={styles.chevronWrapper}>
  197. <ChevronLeft fill={Colors.WHITE} />
  198. </View>
  199. </TouchableOpacity>
  200. <ScrollView
  201. contentContainerStyle={{ flexGrow: 1 }}
  202. nestedScrollEnabled={true}
  203. showsVerticalScrollIndicator={false}
  204. >
  205. {photos.length > 0 ? (
  206. <ImageCarousel
  207. photos={photos}
  208. activeIndex={activeIndex}
  209. setActiveIndex={setActiveIndex}
  210. openModal={openModal}
  211. />
  212. ) : (
  213. <View style={styles.emptyImage}>
  214. <Image
  215. source={require('../../../../../assets/images/logo-opacity.png')}
  216. style={{ width: 100, height: 100 }}
  217. />
  218. <Text style={styles.emptyImageText}>No image available at this location</Text>
  219. </View>
  220. )}
  221. <TouchableOpacity
  222. onPress={() => {
  223. route.params?.isTravelsScreen || route.params?.isProfileScreen
  224. ? navigation.dispatch(
  225. CommonActions.reset({
  226. index: 1,
  227. routes: [
  228. {
  229. name: NAVIGATION_PAGES.IN_APP_MAP_TAB,
  230. state: {
  231. routes: [
  232. {
  233. name: NAVIGATION_PAGES.MAP_TAB,
  234. params: { id: countryId, type: 'countries' }
  235. }
  236. ]
  237. }
  238. }
  239. ]
  240. })
  241. )
  242. : navigation.goBack();
  243. }}
  244. style={styles.goToMapBtn}
  245. >
  246. <View style={styles.chevronWrapper}>
  247. <MapSvg fill={Colors.WHITE} />
  248. </View>
  249. </TouchableOpacity>
  250. <View style={styles.wrapper}>
  251. {regionData?.visited && !disabled && (
  252. <View style={styles.durationContainer}>
  253. <View style={styles.durationItem}>
  254. {renderDurationIcon(regionData.slow11)}
  255. <Text style={styles.visitDuration}>11+ days</Text>
  256. </View>
  257. <View style={styles.durationItem}>
  258. {renderDurationIcon(regionData.slow31)}
  259. <Text style={styles.visitDuration}>31+ days</Text>
  260. </View>
  261. <View style={styles.durationItem}>
  262. {renderDurationIcon(regionData.slow101)}
  263. <Text style={styles.visitDuration}>101+ days</Text>
  264. </View>
  265. </View>
  266. )}
  267. <View style={styles.nameContainer}>
  268. <Text style={styles.title}>{name}</Text>
  269. <View style={ButtonStyles.btnContainer}>
  270. {regionData?.visited && !disabled ? (
  271. <TouchableOpacity
  272. onPress={() => setIsEditSlowModalVisible(true)}
  273. style={ButtonStyles.editBtn}
  274. >
  275. <EditSvg width={14} height={14} />
  276. </TouchableOpacity>
  277. ) : null}
  278. {!disabled ? (
  279. <TouchableOpacity
  280. style={[
  281. ButtonStyles.btn,
  282. regionData?.visited && !disabled
  283. ? ButtonStyles.visitedButton
  284. : ButtonStyles.markVisitedButton
  285. ]}
  286. onPress={handleUpdateSlow}
  287. >
  288. {regionData?.visited ? (
  289. <View style={ButtonStyles.visitedContainer}>
  290. <MarkIcon width={16} height={16} />
  291. <Text style={ButtonStyles.visitedButtonText}>Visited</Text>
  292. </View>
  293. ) : (
  294. <Text style={[ButtonStyles.markVisitedButtonText]}>Mark Visited</Text>
  295. )}
  296. </TouchableOpacity>
  297. ) : null}
  298. </View>
  299. </View>
  300. <View style={styles.divider} />
  301. <View style={styles.stats}>
  302. {data?.data.users_from_country_count ?? 0 > 0 ? (
  303. <TouchableOpacity
  304. style={[styles.statItem, { justifyContent: 'flex-start' }]}
  305. onPress={() =>
  306. navigation.navigate(
  307. ...([
  308. NAVIGATION_PAGES.USERS_LIST,
  309. {
  310. id: countryId,
  311. isFromHere: true,
  312. type: 'country'
  313. }
  314. ] as never)
  315. )
  316. }
  317. >
  318. <View style={styles.icon}>
  319. <HouseSvg />
  320. </View>
  321. <View
  322. style={{
  323. height: 12,
  324. width: 1,
  325. backgroundColor: Colors.DARK_BLUE,
  326. marginRight: 6
  327. }}
  328. />
  329. <View style={styles.userImageContainer}>
  330. {data?.data.users_from_country &&
  331. data?.data.users_from_country.length > 0 &&
  332. data?.data.users_from_country?.map((user, index: number) => (
  333. <Image
  334. key={index}
  335. source={{ uri: API_HOST + user }}
  336. style={styles.userImage}
  337. />
  338. ))}
  339. <View style={styles.userCountContainer}>
  340. <Text style={styles.userCount}>
  341. {formatNumber(data?.data?.users_from_country_count ?? 0)}
  342. </Text>
  343. </View>
  344. </View>
  345. </TouchableOpacity>
  346. ) : (
  347. <View style={[styles.statItem, { justifyContent: 'flex-start' }]} />
  348. )}
  349. {data?.data.users_who_visited_country_count ?? 0 > 0 ? (
  350. <TouchableOpacity
  351. style={[styles.statItem, { justifyContent: 'flex-end' }]}
  352. onPress={() =>
  353. navigation.navigate(
  354. ...([
  355. NAVIGATION_PAGES.USERS_LIST,
  356. {
  357. id: countryId,
  358. isFromHere: false,
  359. type: 'country'
  360. }
  361. ] as never)
  362. )
  363. }
  364. >
  365. <View style={styles.icon}>
  366. <CaseSvg />
  367. </View>
  368. <View
  369. style={{
  370. height: 12,
  371. width: 1,
  372. backgroundColor: Colors.DARK_BLUE,
  373. marginRight: 6
  374. }}
  375. />
  376. <View style={styles.userImageContainer}>
  377. {data?.data.users_who_visited_country &&
  378. data?.data.users_who_visited_country.length > 0 &&
  379. data?.data.users_who_visited_country?.map((user, index) => (
  380. <Image
  381. key={index}
  382. source={{ uri: API_HOST + user }}
  383. style={[styles.userImage]}
  384. />
  385. ))}
  386. <View style={styles.userCountContainer}>
  387. <Text style={styles.userCount}>
  388. {formatNumber(data?.data.users_who_visited_country_count ?? 0)}
  389. </Text>
  390. </View>
  391. </View>
  392. </TouchableOpacity>
  393. ) : (
  394. <View style={[styles.statItem, { justifyContent: 'flex-end' }]} />
  395. )}
  396. </View>
  397. <View style={[styles.divider, { marginBottom: 8 }]} />
  398. {series.length > 0 ? (
  399. <>
  400. <Text style={styles.travelSeriesTitle}>TRAVEL SERIES</Text>
  401. <HorizontalTabView
  402. index={indexSeries}
  403. setIndex={setIndexSeries}
  404. routes={routes}
  405. renderScene={({ route }: { route: SeriesGroup }) => <View style={{ height: 0 }} />}
  406. />
  407. <TravelSeriesList
  408. series={series}
  409. indexSeries={indexSeries}
  410. routes={routes}
  411. handleCheckboxChange={handleCheckboxChange}
  412. setIsInfoModalVisible={setIsInfoModalVisible}
  413. setInfoItem={setInfoItem}
  414. disabled={disabled}
  415. includeAll={false}
  416. />
  417. </>
  418. ) : null}
  419. </View>
  420. <ImageView
  421. images={photos}
  422. imageIndex={currentImageIndex}
  423. visible={isModalVisible}
  424. onRequestClose={() => setModalVisible(false)}
  425. backgroundColor={Colors.DARK_BLUE}
  426. onImageIndexChange={setActiveIndex}
  427. FooterComponent={({ imageIndex }) => (
  428. <View style={styles.imageFooter}>
  429. <Text style={styles.imageDescription}>{photos[imageIndex].title}</Text>
  430. <TouchableOpacity
  431. onPress={() => {
  432. setModalVisible(false);
  433. navigation.navigate(
  434. ...([
  435. NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW,
  436. { userId: photos[imageIndex].user_id, hideTabBar: true }
  437. ] as never)
  438. );
  439. }}
  440. disabled={disabled}
  441. style={styles.imageOwner}
  442. >
  443. <Text style={styles.imageOwnerText}>{photos[imageIndex].first_name}</Text>
  444. <Text style={styles.imageOwnerText}>{photos[imageIndex].last_name}</Text>
  445. </TouchableOpacity>
  446. </View>
  447. )}
  448. />
  449. <ReactModal
  450. visible={isInfoModalVisible}
  451. children={
  452. <View style={styles.modalView}>
  453. <Text style={styles.infoTitle}>{infoItem?.name}</Text>
  454. <Text style={styles.infoText}>{infoItem?.description}</Text>
  455. <Button
  456. variant={ButtonVariants.OPACITY}
  457. containerStyles={styles.btnContainer}
  458. textStyles={{
  459. color: Colors.DARK_BLUE
  460. }}
  461. onPress={() => setIsInfoModalVisible(false)}
  462. children={'Got it'}
  463. />
  464. </View>
  465. }
  466. onRequestClose={() => setIsInfoModalVisible(false)}
  467. headerTitle={'Info'}
  468. visibleInPercent={'auto'}
  469. />
  470. </ScrollView>
  471. <EditModal
  472. isVisible={isEditSlowModalVisible}
  473. onClose={() => setIsEditSlowModalVisible(false)}
  474. item={{ ...regionData, country_id: countryId }}
  475. updateSlow={(id, v, s11, s31, s101) => {
  476. handleUpdateSlowModal(id, v, s11, s31, s101);
  477. }}
  478. />
  479. </View>
  480. );
  481. };
  482. export default CountryViewScreen;