import React, { useEffect, useState } from 'react'; import { View } from 'react-native'; import { Modal } from '../../Modal'; import DateTimePicker, { DateType } from 'react-native-ui-datepicker'; import dayjs from 'dayjs'; import 'dayjs/locale/en'; import calendar from 'dayjs/plugin/calendar'; import updateLocale from 'dayjs/plugin/updateLocale'; import localeData from 'dayjs/plugin/localeData'; import customParseFormat from 'dayjs/plugin/customParseFormat'; dayjs.locale('en'); dayjs.extend(calendar); dayjs.extend(updateLocale); dayjs.extend(localeData); dayjs.extend(customParseFormat); import { styles } from './style'; import { Colors } from '../../../theme'; import { Button } from 'src/components/Button'; import { ButtonVariants } from 'src/types/components'; export default function RangeCalendar({ isModalVisible, closeModal, allowRangeSelection = true, disableFutureDates = false, disablePastDates = false, highlightedDates, selectedDate }: { isModalVisible: boolean; closeModal: (startDate?: string | null, endDate?: string | null) => void; allowRangeSelection?: boolean; disableFutureDates?: boolean; disablePastDates?: boolean; highlightedDates?: string[]; selectedDate?: string; }) { const [selectedStartDate, setSelectedStartDate] = useState(null); const [selectedEndDate, setSelectedEndDate] = useState(null); const [singleDate, setSingleDate] = useState(undefined); const [startDate, setStartDate] = useState(undefined); const [endDate, setEndDate] = useState(undefined); useEffect(() => { if (selectedDate) { setSelectedStartDate(selectedDate); setSingleDate(dayjs(selectedDate)); } }, [selectedDate]); const getDisabledDates = (date: DateType) => { const dateString = dayjs(date).format('YYYY-MM-DD'); if (disableFutureDates) { if (dayjs(date).isAfter(dayjs(), 'day')) { return true; } } if (disablePastDates) { if (dayjs(date).isBefore(dayjs(), 'day')) { return true; } } if (highlightedDates && highlightedDates.length > 0) { return !highlightedDates.includes(dateString); } return false; }; const handleDateChange = (params: any) => { if (allowRangeSelection) { setStartDate(params.startDate); setEndDate(params.endDate); if (params.startDate) { setSelectedStartDate(dayjs(params.startDate).format('YYYY-MM-DD')); } if (params.endDate) { setSelectedEndDate(dayjs(params.endDate).format('YYYY-MM-DD')); } else { setSelectedEndDate(null); } } else { setSingleDate(params.date); setSelectedStartDate(dayjs(params.date).format('YYYY-MM-DD')); setSelectedEndDate(null); } }; const resetSelections = () => { closeModal(); setSelectedStartDate(null); setSelectedEndDate(null); setStartDate(undefined); setEndDate(undefined); setSingleDate(undefined); }; const handleClose = () => { closeModal(selectedStartDate, selectedEndDate); setSelectedStartDate(null); setSelectedEndDate(null); setStartDate(undefined); setEndDate(undefined); setSingleDate(undefined); }; const minDate = highlightedDates && highlightedDates.length > 0 ? dayjs(highlightedDates[0]) : undefined; const maxDate = highlightedDates && highlightedDates.length > 0 ? dayjs(highlightedDates[highlightedDates.length - 1]) : undefined; return (