|
| 1 | +import React, { useEffect, useRef, useState } from "react" |
| 2 | +import dayjs from 'dayjs' |
| 3 | +import weekday from 'dayjs/plugin/weekday' |
| 4 | +import weekOfYear from 'dayjs/plugin/weekOfYear' |
| 5 | + |
| 6 | +import { createActiveMonthDays, createPrevMonthDays, createNextMonthDays } from 'components/date-picker/utils' |
| 7 | +import "components/date-picker/date-picker.scss" |
| 8 | + |
| 9 | +const DatePicker = ({monthsInAdvance = 2, currDate}) => { |
| 10 | + dayjs.extend(weekday) |
| 11 | + dayjs.extend(weekOfYear) |
| 12 | + |
| 13 | + // set date to 3 months from now |
| 14 | + let date = new Date() |
| 15 | + date.setMonth(date.getMonth() + monthsInAdvance) |
| 16 | + |
| 17 | + // keep the active, visible date in State |
| 18 | + let [activeDate, setActiveDate] = useState(dayjs(date)) |
| 19 | + const startYear = dayjs(activeDate).format("YYYY") |
| 20 | + const startMonth = dayjs(activeDate).format("M") |
| 21 | + |
| 22 | + // this collection of dates would come from a database, etc. |
| 23 | + let initUnavailableDates = ['2022-04-10', '2022-04-11', '2022-04-12', '2022-04-14', '2022-04-15', '2022-04-17', '2022-04-18', '2022-04-19', '2022-04-24', '2022-04-25', '2022-04-27'] |
| 24 | + let activeMonthDays = createActiveMonthDays(startYear, startMonth, initUnavailableDates) |
| 25 | + let prevMonthDays = createPrevMonthDays(startYear, startMonth, activeMonthDays, initUnavailableDates) |
| 26 | + let nextMonthDays = createNextMonthDays(startYear, startMonth, activeMonthDays, initUnavailableDates) |
| 27 | + |
| 28 | + let days = [...prevMonthDays, ...activeMonthDays, ...nextMonthDays] |
| 29 | + let [unavailableDates, setUnavailableDates] = useState(initUnavailableDates) |
| 30 | + let [selectedDates, setSelectedDates] = useState([]) |
| 31 | + |
| 32 | + const setPrevMonth = () => { |
| 33 | + // only go backward as far as current month |
| 34 | + if (isPrevMonthAvailable()) { |
| 35 | + setActiveDate(dayjs(activeDate).subtract(1, "month")) |
| 36 | + } |
| 37 | + } |
| 38 | + const setNextMonth = () => { |
| 39 | + setActiveDate(dayjs(activeDate).add(1, "month")) |
| 40 | + } |
| 41 | + const isPrevMonthAvailable = () => { |
| 42 | + return dayjs(activeDate).subtract(1, 'month').get('month') >= dayjs().get('month') |
| 43 | + } |
| 44 | + const isDayUnavailable = (day) => { |
| 45 | + return unavailableDates.includes(day.date) |
| 46 | + } |
| 47 | + const bookDay = (day) => { |
| 48 | + // this function would run on "Reserve" |
| 49 | + setUnavailableDates( |
| 50 | + unavailableDates => [day.date, ...unavailableDates, `${unavailableDates.length}`] |
| 51 | + ) |
| 52 | + } |
| 53 | + const isDaySelected = (day) => { |
| 54 | + return selectedDates.includes(day.date) |
| 55 | + } |
| 56 | + const selectDay = (day) => { |
| 57 | + // to-do: consider perf of this for large quanitites of dates |
| 58 | + if (!isDayUnavailable(day)) { |
| 59 | + // add to selected Dates if not already selected |
| 60 | + if (!isDaySelected(day)) { |
| 61 | + setSelectedDates( |
| 62 | + selectedDates => [day.date, ...selectedDates] |
| 63 | + ) |
| 64 | + } else { |
| 65 | + setSelectedDates( |
| 66 | + selectedDates.filter(date => date !== day.date) |
| 67 | + ) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return ( |
| 72 | + <div className="date-picker"> |
| 73 | + <header> |
| 74 | + <button |
| 75 | + className="btn-month btn-prev" |
| 76 | + disabled={isPrevMonthAvailable() ? '' : 'disabled'} |
| 77 | + onClick={setPrevMonth} |
| 78 | + > |
| 79 | + <span></span> |
| 80 | + { dayjs(activeDate).subtract(1, "month").format("MMM") } |
| 81 | + </button> |
| 82 | + <h4>{ dayjs(activeDate).format("MMMM YYYY") }</h4> |
| 83 | + <button |
| 84 | + className="btn-month btn-next" |
| 85 | + onClick={setNextMonth} |
| 86 | + > |
| 87 | + { dayjs(activeDate).add(1, 'month').format("MMM") } |
| 88 | + <span></span> |
| 89 | + </button> |
| 90 | + </header> |
| 91 | + <div className="days-of-week"> |
| 92 | + <span title="Sunday">S</span> |
| 93 | + <span title="Monday">M</span> |
| 94 | + <span title="Tuesday">T</span> |
| 95 | + <span title="Wednesday">W</span> |
| 96 | + <span title="Thursday">T</span> |
| 97 | + <span title="Friday">F</span> |
| 98 | + <span title="Saturday">S</span> |
| 99 | + </div> |
| 100 | + <div className="date-grid"> |
| 101 | + {days.map((day, index) => { |
| 102 | + return <button |
| 103 | + aria-label={ |
| 104 | + `${dayjs(day.date).format('MMMM D')}${day.isBooked ? ' already booked' : '' }${isDaySelected(day) ? ' selected' : ''}` |
| 105 | + } |
| 106 | + aria-disabled={day.isBooked ? 'true' : 'false'} |
| 107 | + aria-selected={ |
| 108 | + isDaySelected(day) ? 'true' : 'false' |
| 109 | + } |
| 110 | + className={[ |
| 111 | + 'grid-btn', |
| 112 | + day.isBooked ? 'booked' : '', |
| 113 | + day.isCurrentMonth ? 'currentMonth' : '', |
| 114 | + isDaySelected(day) ? 'selected' : '' |
| 115 | + ].join(' ').trim()} |
| 116 | + key={index} |
| 117 | + onClick={() => selectDay(day)} |
| 118 | + > |
| 119 | + <time date-time={day.date}>{day.dayOfMonth}</time> |
| 120 | + <span className="icon" aria-hidden="true"></span> |
| 121 | + </button> |
| 122 | + })} |
| 123 | + </div> |
| 124 | + <ul className="date-key" role="list"> |
| 125 | + <li className="date-key-item-wrap"> |
| 126 | + <span className="date-key-item booked"> |
| 127 | + <span className="icon" aria-hidden="true"></span> |
| 128 | + </span> |
| 129 | + <span className="date-key-text">Booked</span> |
| 130 | + </li> |
| 131 | + <li className="date-key-item-wrap"> |
| 132 | + <span className="date-key-item available"> |
| 133 | + <span className="icon" aria-hidden="true"></span> |
| 134 | + </span> |
| 135 | + <span className="date-key-text">Available</span> |
| 136 | + </li> |
| 137 | + <li className="date-key-item-wrap"> |
| 138 | + <span className="date-key-item selected"> |
| 139 | + <span className="icon" aria-hidden="true"></span> |
| 140 | + </span> |
| 141 | + <span className="date-key-text">Selected</span> |
| 142 | + </li> |
| 143 | + </ul> |
| 144 | + <button className="reserve-btn">Reserve</button> |
| 145 | + </div> |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +export default DatePicker |
0 commit comments