forked from thesam73/wordle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatePickerModal.tsx
146 lines (137 loc) · 5.66 KB
/
DatePickerModal.tsx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/outline'
import { addDays, format, startOfDay } from 'date-fns'
import { useState } from 'react'
import DatePicker, { registerLocale } from 'react-datepicker'
import { DATE_LOCALE } from '../../constants/settings'
import {
DATEPICKER_CHOOSE_TEXT,
DATEPICKER_TITLE,
DATEPICKER_TODAY_TEXT,
} from '../../constants/strings'
import { getToday, getYesterday } from '../../lib/dateutils'
import {
firstGameDate,
getLastGameDate,
isValidGameDate,
periodInDays,
} from '../../lib/words'
import { BaseModal } from './BaseModal'
type Props = {
isOpen: boolean
initialDate?: Date
handleSelectDate: (date: Date) => void
handleClose: () => void
}
export const DatePickerModal = ({
isOpen,
initialDate,
handleSelectDate,
handleClose,
}: Props) => {
const lastGameDate = getLastGameDate(getYesterday())
const [selectedDate, setSelectedDate] = useState(() => {
if (initialDate == null || initialDate > lastGameDate) {
return lastGameDate
}
return initialDate
})
const headingDateFormat = 'MMMM yyyy'
const buttonDateFormat = 'd MMM yyyy'
const formatOptions = { locale: DATE_LOCALE }
registerLocale('locale', DATE_LOCALE)
const excludedDates: Date[] = []
if (periodInDays > 1) {
let date = firstGameDate
for (date = firstGameDate; date < getToday(); date = addDays(date, 1)) {
if (!isValidGameDate(date)) {
excludedDates.push(date)
}
}
}
return (
<BaseModal
title={DATEPICKER_TITLE}
isOpen={isOpen}
handleClose={handleClose}
>
<div className="mx-auto flex max-w-2xl items-center justify-center space-x-4 py-5 text-left sm:w-48">
<DatePicker
locale="locale"
minDate={firstGameDate}
maxDate={getYesterday()}
selected={selectedDate}
excludeDates={excludedDates}
onChange={(date: Date) => setSelectedDate(startOfDay(date))}
inline
popperClassName="react-datepicker-left"
renderCustomHeader={({
date,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled,
}) => (
<div className="flex items-center justify-between px-2 py-2">
<span className="text-lg text-gray-700 dark:text-gray-100">
{format(date, headingDateFormat, formatOptions)}
</span>
<div className="space-x-2">
<button
onClick={decreaseMonth}
disabled={prevMonthButtonDisabled}
type="button"
className={`
${
prevMonthButtonDisabled &&
'cursor-not-allowed opacity-50'
}
inline-flex rounded border border-gray-300 bg-white p-1 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-0
dark:border-gray-600 dark:bg-slate-700 dark:text-gray-200 dark:focus:ring-blue-600
`}
>
<ChevronLeftIcon className="h-5 w-5 text-gray-600 dark:text-gray-300" />
</button>
<button
onClick={increaseMonth}
disabled={nextMonthButtonDisabled}
type="button"
className={`
${
nextMonthButtonDisabled &&
'cursor-not-allowed opacity-50'
}
inline-flex rounded border border-gray-300 bg-white p-1 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-0
dark:border-gray-600 dark:bg-slate-700 dark:text-gray-200 dark:focus:ring-blue-600
`}
>
<ChevronRightIcon className="h-5 w-5 text-gray-600 dark:text-gray-300" />
</button>
</div>
</div>
)}
/>
</div>
<div className="mt-5 flex columns-2 items-center items-stretch justify-center gap-2 text-center dark:text-white sm:mt-6">
<button
type="button"
disabled={!isValidGameDate(getToday())}
className="mt-2 inline-flex w-full items-center justify-center rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-center text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:border-gray-200 disabled:bg-gray-500 disabled:bg-white disabled:text-gray-900
disabled:focus:outline-none disabled:dark:border-gray-600 disabled:dark:bg-gray-800 disabled:dark:text-gray-400 sm:text-base sm:text-base"
onClick={() => handleSelectDate(getToday())}
>
{DATEPICKER_CHOOSE_TEXT} {DATEPICKER_TODAY_TEXT}
</button>
<button
type="button"
className="mt-2 inline-flex w-full items-center justify-center rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-center text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 "
disabled={selectedDate >= getToday()}
onClick={() => handleSelectDate(selectedDate)}
>
{DATEPICKER_CHOOSE_TEXT}
<br />
{format(selectedDate, buttonDateFormat, formatOptions)}
</button>
</div>
</BaseModal>
)
}