import React, { useCallback, useEffect, useRef, useState } from 'react'; import { View, Text, TouchableOpacity, FlatList } from 'react-native'; import { useFocusEffect, useNavigation } from '@react-navigation/native'; import { PageWrapper, Header, Modal, FlatList as List, WarningModal } from 'src/components'; import TripItem from '../Components/TripItem'; import { useGetTripsYearsQuery, useGetTripsForYearQuery } from '@api/trips'; import { StoreType, storage } from 'src/storage'; import { TripsData } from '../utils/types'; import { NAVIGATION_PAGES } from 'src/types'; import { styles } from './styles'; import ChevronIcon from '../../../../../assets/icons/travels-screens/chevron-bottom.svg'; import AddIcon from '../../../../../assets/icons/travels-screens/circle-plus.svg'; import TripSvg from '../../../../../assets/icons/travels-screens/trip.svg'; import InfoIcon from 'assets/icons/info-solid.svg'; import { Colors } from 'src/theme'; import ChevronLeftIcon from 'assets/icons/chevron-left.svg'; import { getFontSize } from 'src/utils'; import { FlashList, FlashListRef } from '@shopify/flash-list'; import WarningIcon from 'assets/icons/warning.svg'; import moment from 'moment'; const TripsScreen = ({ route }: { route: any }) => { const token = storage.get('token', StoreType.STRING) as string; const navigation = useNavigation(); const [isDatePickerVisible, setDatePickerVisible] = useState(false); const { data: years, refetch } = useGetTripsYearsQuery(token, true); const [selectedYear, setSelectedYear] = useState(moment().year().toString()); const { data: tripsData, refetch: refetchTrips } = useGetTripsForYearQuery( token, selectedYear as string, selectedYear ? true : false ); const [trips, setTrips] = useState([]); const [statistics, setStatistics] = useState<{ countries: { description: string; list: { id: number; country: string; days_spent: number; flag: string | null; regions: { id: number; region: string; days_spent: number; }[]; }[]; }; general: [string]; regions: { description: string; list: { id: number; days_spent: number; flag: string | null; flag1: string | null; flag2: string | null; region: string; }[]; }; dates_missing: 0 | 1; } | null>(null); const [isWarningModalVisible, setIsWarningModalVisible] = useState(false); const flashListRef = useRef | null>(null); useFocusEffect( useCallback(() => { const fetchData = async () => { try { await refetch(); await refetchTrips(); } catch (error) { console.error(error); } }; if (route.params?.saved || route.params?.updated || route.params?.deleted) { fetchData(); setIsWarningModalVisible(true); } }, [route.params]) ); useEffect(() => { if (years && years.data && !selectedYear) { setSelectedYear(years.data[0]); } }, [years]); useEffect(() => { if (!isWarningModalVisible) { navigation.setParams({ saved: false, updated: false, deleted: false } as never); } }, [isWarningModalVisible]); useEffect(() => { if (tripsData && tripsData.trips) { setStatistics(tripsData.statistics); setTrips(tripsData.trips); } }, [tripsData]); const renderItem = useCallback( ({ item }: { item: TripsData }) => ( = 2025} /> ), [selectedYear] ); const onAddNewTripPress = useCallback(() => { if (Number(selectedYear) >= 2025) { navigation.navigate(NAVIGATION_PAGES.ADD_TRIP_2025 as never); } else { navigation.navigate(NAVIGATION_PAGES.ADD_TRIP as never); } }, [navigation, selectedYear]); const handleScrollToTrip = () => { if (!flashListRef?.current) return; const index = trips.findIndex((trip) => trip.dates_missing === 1); if (index === -1) return; flashListRef.current.scrollToIndex({ index, animated: true, viewPosition: 0 }); }; return ( Trips setDatePickerVisible(true)}> {selectedYear} Add New Trip {trips.length === 0 ? ( No trips at the moment ) : ( `${item.id}-${index}`} style={styles.tripsList} contentContainerStyle={styles.tripsListContentContainer} showsVerticalScrollIndicator={false} ListHeaderComponent={ Number(selectedYear) >= 2025 ? ( {statistics?.general ? ( {statistics.general} ) : null} navigation.navigate( ...([ NAVIGATION_PAGES.REGIONS_VISITED, { title: statistics?.countries?.description, isRegion: false, data: statistics?.countries?.list } ] as never) ) } hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} > {statistics?.countries?.description} navigation.navigate( ...([ NAVIGATION_PAGES.REGIONS_VISITED, { title: statistics?.regions?.description, isRegion: true, data: statistics?.regions?.list } ] as never) ) } hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} > {statistics?.regions?.description} {statistics?.dates_missing ? ( For accurate annual statistics, please enter exact dates for all trips. ) : null} ) : null } /> )} setDatePickerVisible(false)} headerTitle={'Select Year'} visible={isDatePickerVisible} > { setSelectedYear(object); setDatePickerVisible(false); }} initialData={years?.data} date={true} /> { setIsWarningModalVisible(false); }} title={ route.params?.saved ? 'Trip added' : route.params?.deleted ? 'Trip deleted' : 'Trip updated' } message={ route.params?.saved ? 'Trip was successfully added to your list of trips' : route.params?.deleted ? 'This trip was successfully deleted.' : 'Trip was successfully updated' } /> ); }; export default TripsScreen;