index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. setIsLoading(false);
  89. };
  90. if (data && data.result === 'OK') {
  91. fetchGroups();
  92. }
  93. }, [data])
  94. );
  95. const handleCheckboxChange = useCallback(
  96. async (item: SeriesItem, double: boolean, seriesId: number) => {
  97. setSeries((currentData) => {
  98. const groupIndex = currentData.findIndex((group) => group?.series_id === seriesId);
  99. if (groupIndex === -1) return currentData;
  100. const newData = [...currentData];
  101. const newGroup = { ...newData[groupIndex] };
  102. newGroup.items = newGroup.items.map((subItem) =>
  103. subItem.id === item.id
  104. ? {
  105. ...subItem,
  106. ...(double
  107. ? { visited_double: subItem.visited_double === 0 ? 1 : 0 }
  108. : { visited: subItem.visited === 0 ? 1 : 0 })
  109. }
  110. : subItem
  111. );
  112. newData[groupIndex] = newGroup;
  113. return newData;
  114. });
  115. const itemData = {
  116. token: token,
  117. series_id: seriesId,
  118. item_id: item.id,
  119. checked: (item.visited === 1 ? 0 : 1) as 0 | 1,
  120. double: (double && !item.visited_double ? 1 : 0) as 0 | 1
  121. };
  122. try {
  123. updateSeriesItem(itemData);
  124. } catch (error) {
  125. console.error('Failed to update checkbox state', error);
  126. }
  127. },
  128. [token, updateSeriesItem]
  129. );
  130. const openModal = (index: number) => {
  131. setCurrentImageIndex(index);
  132. setModalVisible(true);
  133. };
  134. if (isLoading) return <Loading />;
  135. const handleUpdateSlowModal = (
  136. id: number,
  137. v: boolean,
  138. s11: boolean,
  139. s31: boolean,
  140. s101: boolean
  141. ) => {
  142. const updatedSlow = {
  143. ...regionData,
  144. visited: v,
  145. slow11: Number(s11) as 0 | 1,
  146. slow31: Number(s31) as 0 | 1,
  147. slow101: Number(s101) as 0 | 1
  148. };
  149. const updatedSlowData = {
  150. token,
  151. id,
  152. v,
  153. s11,
  154. s31,
  155. s101
  156. };
  157. route.params?.isTravelsScreen
  158. ? handleUpdateSlowList(id, v, s11, s31, s101)
  159. : updateSlow(id, v, s11, s31, s101);
  160. updatedSlow && setRegionData(updatedSlow);
  161. };
  162. const handleUpdateSlow = () => {
  163. route.params?.isTravelsScreen
  164. ? handleUpdateSlowList(
  165. countryId,
  166. !regionData.visited,
  167. Boolean(regionData.slow11),
  168. Boolean(regionData.slow31),
  169. Boolean(regionData.slow101)
  170. )
  171. : updateSlow(
  172. countryId,
  173. !regionData.visited,
  174. Boolean(regionData.slow11),
  175. Boolean(regionData.slow31),
  176. Boolean(regionData.slow101)
  177. );
  178. setRegionData({
  179. ...regionData,
  180. visited: !regionData.visited
  181. });
  182. };
  183. const renderDurationIcon = (condition: 0 | 1) =>
  184. condition ? <CheckSvg fill={Colors.DARK_BLUE} /> : <CheckRegularSvg />;
  185. return (
  186. <View style={styles.container}>
  187. <TouchableOpacity
  188. onPress={() => {
  189. navigation.goBack();
  190. }}
  191. style={styles.backButton}
  192. >
  193. <View style={styles.chevronWrapper}>
  194. <ChevronLeft fill={Colors.WHITE} />
  195. </View>
  196. </TouchableOpacity>
  197. <ScrollView
  198. contentContainerStyle={{ flexGrow: 1 }}
  199. nestedScrollEnabled={true}
  200. showsVerticalScrollIndicator={false}
  201. >
  202. {photos.length > 0 ? (
  203. <ImageCarousel
  204. photos={photos}
  205. activeIndex={activeIndex}
  206. setActiveIndex={setActiveIndex}
  207. openModal={openModal}
  208. />
  209. ) : (
  210. <View style={styles.emptyImage}>
  211. <Image
  212. source={require('../../../../../assets/images/logo-opacity.png')}
  213. style={{ width: 100, height: 100 }}
  214. />
  215. <Text style={styles.emptyImageText}>No image available at this location</Text>
  216. </View>
  217. )}
  218. <TouchableOpacity
  219. onPress={() => {
  220. route.params?.isTravelsScreen
  221. ? navigation.dispatch(
  222. CommonActions.reset({
  223. index: 1,
  224. routes: [
  225. {
  226. name: NAVIGATION_PAGES.IN_APP_MAP_TAB,
  227. state: {
  228. routes: [
  229. {
  230. name: NAVIGATION_PAGES.MAP_TAB,
  231. params: { id: countryId, type: 'countries' }
  232. }
  233. ]
  234. }
  235. }
  236. ]
  237. })
  238. )
  239. : navigation.goBack();
  240. }}
  241. style={styles.goToMapBtn}
  242. >
  243. <View style={styles.chevronWrapper}>
  244. <MapSvg fill={Colors.WHITE} />
  245. </View>
  246. </TouchableOpacity>
  247. <View style={styles.wrapper}>
  248. {regionData?.visited && !disabled && (
  249. <View style={styles.durationContainer}>
  250. <View style={styles.durationItem}>
  251. {renderDurationIcon(regionData.slow11)}
  252. <Text style={styles.visitDuration}>11+ days</Text>
  253. </View>
  254. <View style={styles.durationItem}>
  255. {renderDurationIcon(regionData.slow31)}
  256. <Text style={styles.visitDuration}>31+ days</Text>
  257. </View>
  258. <View style={styles.durationItem}>
  259. {renderDurationIcon(regionData.slow101)}
  260. <Text style={styles.visitDuration}>101+ days</Text>
  261. </View>
  262. </View>
  263. )}
  264. <View style={styles.nameContainer}>
  265. <Text style={styles.title}>{name}</Text>
  266. <View style={ButtonStyles.btnContainer}>
  267. {regionData?.visited && !disabled ? (
  268. <TouchableOpacity
  269. onPress={() => setIsEditSlowModalVisible(true)}
  270. style={ButtonStyles.editBtn}
  271. >
  272. <EditSvg width={14} height={14} />
  273. </TouchableOpacity>
  274. ) : null}
  275. {!disabled ? (
  276. <TouchableOpacity
  277. style={[
  278. ButtonStyles.btn,
  279. regionData?.visited && !disabled
  280. ? ButtonStyles.visitedButton
  281. : ButtonStyles.markVisitedButton
  282. ]}
  283. onPress={handleUpdateSlow}
  284. >
  285. {regionData?.visited ? (
  286. <View style={ButtonStyles.visitedContainer}>
  287. <MarkIcon width={16} height={16} />
  288. <Text style={ButtonStyles.visitedButtonText}>Visited</Text>
  289. </View>
  290. ) : (
  291. <Text style={[ButtonStyles.markVisitedButtonText]}>Mark Visited</Text>
  292. )}
  293. </TouchableOpacity>
  294. ) : null}
  295. </View>
  296. </View>
  297. <View style={styles.divider} />
  298. <View style={styles.stats}>
  299. {data?.data.users_from_country_count ?? 0 > 0 ? (
  300. <TouchableOpacity
  301. style={[styles.statItem, { justifyContent: 'flex-start' }]}
  302. onPress={() =>
  303. navigation.navigate(
  304. ...([
  305. NAVIGATION_PAGES.USERS_LIST,
  306. {
  307. id: countryId,
  308. isFromHere: true,
  309. type: 'country'
  310. }
  311. ] as never)
  312. )
  313. }
  314. >
  315. <View style={styles.icon}>
  316. <HouseSvg />
  317. </View>
  318. <View
  319. style={{
  320. height: 12,
  321. width: 1,
  322. backgroundColor: Colors.DARK_BLUE,
  323. marginRight: 6
  324. }}
  325. />
  326. <View style={styles.userImageContainer}>
  327. {data?.data.users_from_country &&
  328. data?.data.users_from_country.length > 0 &&
  329. data?.data.users_from_country?.map((user, index: number) => (
  330. <Image
  331. key={index}
  332. source={{ uri: API_HOST + user }}
  333. style={styles.userImage}
  334. />
  335. ))}
  336. <View style={styles.userCountContainer}>
  337. <Text style={styles.userCount}>
  338. {formatNumber(data?.data?.users_from_country_count ?? 0)}
  339. </Text>
  340. </View>
  341. </View>
  342. </TouchableOpacity>
  343. ) : (
  344. <View style={[styles.statItem, { justifyContent: 'flex-start' }]} />
  345. )}
  346. {data?.data.users_who_visited_country_count ?? 0 > 0 ? (
  347. <TouchableOpacity
  348. style={[styles.statItem, { justifyContent: 'flex-end' }]}
  349. onPress={() =>
  350. navigation.navigate(
  351. ...([
  352. NAVIGATION_PAGES.USERS_LIST,
  353. {
  354. id: countryId,
  355. isFromHere: false,
  356. type: 'country'
  357. }
  358. ] as never)
  359. )
  360. }
  361. >
  362. <View style={styles.icon}>
  363. <CaseSvg />
  364. </View>
  365. <View
  366. style={{
  367. height: 12,
  368. width: 1,
  369. backgroundColor: Colors.DARK_BLUE,
  370. marginRight: 6
  371. }}
  372. />
  373. <View style={styles.userImageContainer}>
  374. {data?.data.users_who_visited_country &&
  375. data?.data.users_who_visited_country.length > 0 &&
  376. data?.data.users_who_visited_country?.map((user, index) => (
  377. <Image
  378. key={index}
  379. source={{ uri: API_HOST + user }}
  380. style={[styles.userImage]}
  381. />
  382. ))}
  383. <View style={styles.userCountContainer}>
  384. <Text style={styles.userCount}>
  385. {formatNumber(data?.data.users_who_visited_country_count ?? 0)}
  386. </Text>
  387. </View>
  388. </View>
  389. </TouchableOpacity>
  390. ) : (
  391. <View style={[styles.statItem, { justifyContent: 'flex-end' }]} />
  392. )}
  393. </View>
  394. <View style={[styles.divider, { marginBottom: 8 }]} />
  395. {series.length > 0 ? (
  396. <>
  397. <Text style={styles.travelSeriesTitle}>TRAVEL SERIES</Text>
  398. <HorizontalTabView
  399. index={indexSeries}
  400. setIndex={setIndexSeries}
  401. routes={routes}
  402. renderScene={({ route }: { route: SeriesGroup }) => <View style={{ height: 0 }} />}
  403. />
  404. <TravelSeriesList
  405. series={series}
  406. indexSeries={indexSeries}
  407. routes={routes}
  408. handleCheckboxChange={handleCheckboxChange}
  409. setIsInfoModalVisible={setIsInfoModalVisible}
  410. setInfoItem={setInfoItem}
  411. disabled={disabled}
  412. includeAll={false}
  413. />
  414. </>
  415. ) : null}
  416. </View>
  417. <ImageView
  418. images={photos}
  419. imageIndex={currentImageIndex}
  420. visible={isModalVisible}
  421. onRequestClose={() => setModalVisible(false)}
  422. backgroundColor={Colors.DARK_BLUE}
  423. onImageIndexChange={setActiveIndex}
  424. FooterComponent={({ imageIndex }) => (
  425. <View style={styles.imageFooter}>
  426. <Text style={styles.imageDescription}>{photos[imageIndex].title}</Text>
  427. <TouchableOpacity
  428. onPress={() => {
  429. setModalVisible(false);
  430. navigation.navigate(
  431. ...([
  432. NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW,
  433. { userId: photos[imageIndex].user_id, hideTabBar: true }
  434. ] as never)
  435. );
  436. }}
  437. disabled={disabled}
  438. style={styles.imageOwner}
  439. >
  440. <Text style={styles.imageOwnerText}>{photos[imageIndex].first_name}</Text>
  441. <Text style={styles.imageOwnerText}>{photos[imageIndex].last_name}</Text>
  442. </TouchableOpacity>
  443. </View>
  444. )}
  445. />
  446. <ReactModal
  447. visible={isInfoModalVisible}
  448. children={
  449. <View style={styles.modalView}>
  450. <Text style={styles.infoTitle}>{infoItem?.name}</Text>
  451. <Text style={styles.infoText}>{infoItem?.description}</Text>
  452. <Button
  453. variant={ButtonVariants.OPACITY}
  454. containerStyles={styles.btnContainer}
  455. textStyles={{
  456. color: Colors.DARK_BLUE
  457. }}
  458. onPress={() => setIsInfoModalVisible(false)}
  459. children={'Got it'}
  460. />
  461. </View>
  462. }
  463. onRequestClose={() => setIsInfoModalVisible(false)}
  464. headerTitle={'Info'}
  465. visibleInPercent={'auto'}
  466. />
  467. </ScrollView>
  468. <EditModal
  469. isVisible={isEditSlowModalVisible}
  470. onClose={() => setIsEditSlowModalVisible(false)}
  471. item={{ ...regionData, country_id: countryId }}
  472. updateSlow={(id, v, s11, s31, s101) => {
  473. handleUpdateSlowModal(id, v, s11, s31, s101);
  474. }}
  475. />
  476. </View>
  477. );
  478. };
  479. export default CountryViewScreen;