File size: 3,687 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Gridicon, Button } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { Moment } from 'moment';
import React, { useState, useMemo } from 'react';
import DatePicker from 'calypso/components/date-picker';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import useDateWithOffset from 'calypso/lib/jetpack/hooks/use-date-with-offset';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions/record';

const DATE_PICKER_OPEN = recordTracksEvent( 'calypso_jetpack_backup_date_picker_open' );

const DateButton: React.FC< Props > = ( {
	selectedDate,
	onDateSelected,
	firstBackupDate,
	disabledDates,
	disabled = false,
} ) => {
	const dispatch = useDispatch();

	const [ pickerVisible, setPickerVisible ] = useState( false );
	const moment = useLocalizedMoment();
	const today = useDateWithOffset( moment() ) as Moment;
	const translate = useTranslate();

	/**
	 * A date has been picked from the calendar.
	 * @param date { Moment } - a moment date object that has been selected on the calendar
	 */
	const handleDatePicked = ( date: Moment ) => {
		onDateSelected( date );
		setPickerVisible( false );
	};

	// Map date strings that should be disabled to Date objects that can be used by the calendar
	const disabledDatesObjects = useMemo( () => {
		return disabledDates
			? disabledDates.map( ( date ) => {
					const momentDate = moment( date );
					return new Date( momentDate.year(), momentDate.month(), momentDate.date() );
			  } )
			: [];
	}, [ disabledDates, moment ] );

	const renderPicker = () => {
		if ( pickerVisible ) {
			return (
				<div className="backup-date-picker__date-button-picker">
					<div className="backup-date-picker__picker-background-screen"> </div>
					<DatePicker
						calendarViewDate={ new Date( selectedDate.year(), selectedDate.month() ) } // sets the month when the calendar opens
						moment={ moment }
						timeReference={ selectedDate.clone() } // Use the current localized time of the selectedDate to adjust against when selecting a date
						onSelectDay={ handleDatePicked }
						selectedDay={
							new Date( selectedDate.year(), selectedDate.month(), selectedDate.date() )
						}
						disabledDays={ [
							{
								before: firstBackupDate
									? new Date(
											firstBackupDate.year(),
											firstBackupDate.month(),
											firstBackupDate.date()
									  )
									: null, // The first known backup date - should be nothing before this.
								after: today // If the offset value of today that factors in the blog's GMT offset is available, use that.
									? new Date( today.year(), today.month(), today.date() )
									: moment().toDate(), // There are no backups of the future.
							},
							// Dates that do not have a backup
							...disabledDatesObjects,
						] }
					/>
				</div>
			);
		}
	};

	/**
	 * Toggle the picker open or closed.
	 * Will fire an event when the picker is opened.
	 */
	const handlePickerToggle = () => {
		if ( ! pickerVisible ) {
			dispatch( DATE_PICKER_OPEN );
		}

		setPickerVisible( ! pickerVisible );
	};

	return (
		<div className="backup-date-picker__date-button-container">
			<Button
				className="backup-date-picker__date-button-button"
				onClick={ handlePickerToggle }
				disabled={ disabled }
			>
				<Gridicon icon="calendar" />
				{ translate( 'Select Date' ) }
			</Button>
			{ renderPicker() }
		</div>
	);
};

type Props = {
	selectedDate: Moment;
	firstBackupDate: Moment | undefined;
	onDateSelected: ( m: Moment ) => void;
	disabledDates: Array< string >;
	disabled?: boolean;
};

export default DateButton;