import React, { useState } from 'react';
import { View, Text, TouchableOpacity, Image } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import moment from 'moment';
import { TripsData } from '../../utils/types';
import { API_HOST } from 'src/constants';
import { Colors } from 'src/theme';
import { NAVIGATION_PAGES } from 'src/types';
import { styles } from './styles';
import CalendarIcon from 'assets/icons/travels-screens/calendar.svg';
import EditIcon from 'assets/icons/travels-screens/pen-to-square.svg';
import ArrowIcon from 'assets/icons/chevron-left.svg';
import WarningIcon from 'assets/icons/warning.svg';
const formatDate = (dateString: string | null) => {
if (!dateString) return;
const date = moment(dateString);
const formattedDate = date.format('MMM DD');
const year = date.format('YYYY');
return (
{formattedDate}
{'\n'}
{year}
);
};
const getValidDate = (value?: string | null) => {
if (!value) return null;
const date = moment(value);
return date.isValid() ? date : null;
};
const formatSingleLineDate = (dateString: string | null) => {
const date = getValidDate(dateString);
if (!date) return null;
return `${date.format('MMM DD')} ${date.format('YYYY')}`;
};
const renderMissingDatesWarning = (withMarginBottom = true) => (
Fill in exact dates to get accurate statistics.
);
const getTripDateLabel = (item: TripsData) => {
const validFrom = getValidDate(item.date_from);
const validTo = getValidDate(item.date_to);
if (!validFrom && !validTo) {
if (item.year) {
return {item.year};
}
return null;
}
const fromYear = validFrom ? validFrom.format('YYYY') : null;
const toYear = validTo ? validTo.format('YYYY') : null;
if (item.dates_missing === 1) {
if (!fromYear && !toYear) return null;
if (fromYear && toYear && fromYear === toYear) {
return {toYear};
}
return (
<>
{fromYear ? {fromYear} : null}
{fromYear && toYear ? - : null}
{toYear ? {toYear} : null}
>
);
}
const hasValidFrom = !!validFrom;
const hasValidTo = !!validTo;
if (!hasValidFrom && !hasValidTo) return null;
if (hasValidFrom && !hasValidTo) {
return (
{formatSingleLineDate(item.date_from)}
);
}
if (!hasValidFrom && hasValidTo) {
return (
{formatSingleLineDate(item.date_to)}
);
}
if (validFrom && validTo && item.date_from === item.date_to) {
return (
{formatSingleLineDate(item.date_from)}
);
}
return (
<>
{formatDate(item.date_from)}
-
{formatDate(item.date_to)}
>
);
};
const TripItem = ({ item }: { item: TripsData }) => {
const navigation = useNavigation();
const [showAllRegions, setShowAllRegions] = useState(false);
const MAX_VISIBLE_REGIONS = 3;
const totalRegions = item.regions?.length || 0;
const visibleRegions = showAllRegions
? item.regions
: item.regions?.slice(0, MAX_VISIBLE_REGIONS);
const validFrom = getValidDate(item.date_from);
const validTo = getValidDate(item.date_to);
const hasAnyValidDate = !!validFrom || !!validTo || !!item.year;;
const shouldRenderWarningAbove = item.dates_missing === 1 && hasAnyValidDate;
const shouldRenderWarningInsteadOfDate = item.dates_missing === 1 && !hasAnyValidDate;
const tripDateLabel = getTripDateLabel(item);
return (
{shouldRenderWarningAbove ? renderMissingDatesWarning(true) : null}
{shouldRenderWarningInsteadOfDate ? (
renderMissingDatesWarning(false)
) : tripDateLabel ? (
{tripDateLabel}
) : null}
navigation.navigate(
...([NAVIGATION_PAGES.ADD_TRIP_2025, { editTripId: item.id }] as never)
)
}
>
Edit
{item.description && (
<>
Description
{item.description}
>
)}
Visited regions
{visibleRegions?.map((region, index) => {
if (!region.id || !region.region_name) return null;
const [name, ...rest] = region.region_name?.split(/ – | - /);
const subname = rest?.join(' - ');
return (
{region.flag2 && (
)}
{name}
{subname}
);
})}
{totalRegions > MAX_VISIBLE_REGIONS && (
setShowAllRegions(!showAllRegions)}
>
{showAllRegions ? 'Show less' : `Show more (${totalRegions - MAX_VISIBLE_REGIONS})`}
)}
);
};
export default TripItem;