Skip to main content

Trip planner

Several stays on one calendar: tap a start and an end for each trip, tap a trip again to remove it, and see the nights add up. The cities to visit come from a searchable list.

Loading…
import { useMemo, useState } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { addDays, DatePicker, getCountryFlag, Heading, Select, Text, useTheme } from 'patine-ui';
import type { DateRange } from 'patine-ui';

const CITIES = [
  { city: 'Lisbon', country: 'PT' },
  { city: 'Porto', country: 'PT' },
  { city: 'Madrid', country: 'ES' },
  { city: 'Seville', country: 'ES' },
  { city: 'Paris', country: 'FR' },
  { city: 'Lyon', country: 'FR' },
  { city: 'Rome', country: 'IT' },
  { city: 'Tel Aviv', country: 'IL' },
  { city: 'Kyoto', country: 'JP' },
] as const;
const OPTIONS = CITIES.map(({ city, country }) => ({ label: `${getCountryFlag(country)}  ${city}`, value: city, keywords: [country] }));
const nights = (range: DateRange) => (range.start && range.end ? Math.round((+range.end - +range.start) / 864e5) : 0);

export default function TripPlanner() {
  const { colors } = useTheme();
  const today = useMemo(() => {
    const day = new Date();
    day.setHours(0, 0, 0, 0);
    return day;
  }, []);
  const [trips, setTrips] = useState<DateRange[]>(() => [
    { start: addDays(today, 3), end: addDays(today, 6) },
    { start: addDays(today, 12), end: addDays(today, 16) },
  ]);
  const [cities, setCities] = useState(['Lisbon', 'Paris']);
  const total = trips.reduce((sum, trip) => sum + nights(trip), 0);

  return (
    <ScrollView style={{ backgroundColor: colors.background }} contentContainerStyle={styles.content}>
      <View style={styles.titles}>
        <Heading level={2}>Trips</Heading>
        <Text color="textSecondary">
          {trips.length} {trips.length === 1 ? 'trip' : 'trips'} · {total} nights away
        </Text>
      </View>
      <DatePicker mode="ranges" value={trips} onValueChange={setTrips} minDate={today} dayHeight={40} />
      <Select label="Cities" options={OPTIONS} multiple chips value={cities} onValueChange={setCities} searchable />
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  content: { gap: 16, padding: 20 },
  titles: { gap: 4 },
});

Components used​

How it works​

  • Ranges, plural. mode="ranges" keeps an array of { start, end }: a tap starts a stay, the next ends it, and tapping inside a stay removes it.
  • Nights are differences. Each stay's nights are its end minus its start, in days; store the ends with toDateKey when you save them.