|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. Devtron Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { useState } from 'react' |
| 18 | +import { DayPickerRangeController, isInclusivelyBeforeDay } from 'react-dates' |
| 19 | +// eslint-disable-next-line import/extensions |
| 20 | +import CustomizableCalendarDay from 'react-dates/esm/components/CustomizableCalendarDay.js' |
| 21 | +import moment, { Moment } from 'moment' |
| 22 | + |
| 23 | +import { ComponentSizeType } from '@Shared/constants' |
| 24 | + |
| 25 | +import 'react-dates/initialize' |
| 26 | + |
| 27 | +import { Button } from '../Button' |
| 28 | +import { Icon } from '../Icon' |
| 29 | +import { customDayStyles, DayPickerCalendarInfoHorizontal, DayPickerRangeControllerPresets, styles } from './constants' |
| 30 | +import { DatePickerRangeControllerProps } from './types' |
| 31 | + |
| 32 | +import 'react-dates/lib/css/_datepicker.css' |
| 33 | + |
| 34 | +export const DatePickerRangeController = ({ |
| 35 | + handlePredefinedRange, |
| 36 | + handleApply, |
| 37 | + calendar, |
| 38 | + calendarInputs, |
| 39 | + handleDateInput, |
| 40 | + handleDatesChange, |
| 41 | + calendarValue, |
| 42 | + focusedInput, |
| 43 | + handleFocusChange, |
| 44 | +}: DatePickerRangeControllerProps) => { |
| 45 | + const [showCalendar, setShowCalender] = useState(false) |
| 46 | + const onClickApplyTimeChange = () => { |
| 47 | + setShowCalender(false) |
| 48 | + handleApply() |
| 49 | + } |
| 50 | + |
| 51 | + const onClickPredefinedTimeRange = (startDate: Moment, endDate: Moment, endStr: string) => () => { |
| 52 | + handlePredefinedRange(startDate, endDate, endStr) |
| 53 | + setShowCalender(false) |
| 54 | + } |
| 55 | + |
| 56 | + const renderDatePresets = () => ( |
| 57 | + <div |
| 58 | + className="flex left top" |
| 59 | + style={{ |
| 60 | + ...styles.PresetDateRangePicker_panel, |
| 61 | + ...DayPickerCalendarInfoHorizontal, |
| 62 | + ...{ |
| 63 | + PresetDateRangePicker_panel: { |
| 64 | + padding: '0px', |
| 65 | + width: '200px', |
| 66 | + height: '100%', |
| 67 | + }, |
| 68 | + ...styles.DayPicker__horizontal, |
| 69 | + }, |
| 70 | + }} |
| 71 | + > |
| 72 | + <div className="w-300 h-300 p-16"> |
| 73 | + <p className="mb-16 fw-6">Pick time range</p> |
| 74 | + <div> |
| 75 | + <div className="w-100 mb-16"> |
| 76 | + <span>From</span> |
| 77 | + <input |
| 78 | + type="text" |
| 79 | + className="dc__block w-100 dc__border" |
| 80 | + value={calendarInputs.startDate} |
| 81 | + onChange={(event) => { |
| 82 | + handleDateInput('startDate', event.target.value) |
| 83 | + }} |
| 84 | + /> |
| 85 | + </div> |
| 86 | + <div className="w-100 mb-16"> |
| 87 | + <span>To</span> |
| 88 | + <input |
| 89 | + type="text" |
| 90 | + className="dc__block w-100 dc__border" |
| 91 | + value={calendarInputs.endDate} |
| 92 | + onChange={(event) => { |
| 93 | + handleDateInput('endDate', event.target.value) |
| 94 | + }} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + <Button |
| 98 | + text="Apply Time Range" |
| 99 | + onClick={onClickApplyTimeChange} |
| 100 | + dataTestId="apply-time-range" |
| 101 | + size={ComponentSizeType.medium} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + <div className="w-200 p-16 h-300"> |
| 106 | + {DayPickerRangeControllerPresets.map(({ text, startDate, endDate, endStr }) => { |
| 107 | + const isSelected = |
| 108 | + startDate.isSame(calendar.startDate, 'minute') && |
| 109 | + startDate.isSame(calendar.startDate, 'hour') && |
| 110 | + startDate.isSame(calendar.startDate, 'day') && |
| 111 | + endDate.isSame(calendar.endDate, 'day') |
| 112 | + let buttonStyles = { |
| 113 | + ...styles.PresetDateRangePicker_button, |
| 114 | + } |
| 115 | + if (isSelected) { |
| 116 | + buttonStyles = { |
| 117 | + ...buttonStyles, |
| 118 | + ...styles.PresetDateRangePicker_button__selected, |
| 119 | + } |
| 120 | + } |
| 121 | + return ( |
| 122 | + <button |
| 123 | + type="button" |
| 124 | + key={text} |
| 125 | + style={{ ...buttonStyles, textAlign: 'left' }} |
| 126 | + onClick={onClickPredefinedTimeRange(startDate, endDate, endStr)} |
| 127 | + > |
| 128 | + {text} |
| 129 | + </button> |
| 130 | + ) |
| 131 | + })} |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + ) |
| 135 | + |
| 136 | + const toggleCalender = () => { |
| 137 | + setShowCalender(!showCalendar) |
| 138 | + } |
| 139 | + |
| 140 | + const hideCalender = () => setShowCalender(false) |
| 141 | + |
| 142 | + return ( |
| 143 | + <> |
| 144 | + <div |
| 145 | + data-testid="app-metrics-range-picker-box" |
| 146 | + className="flex h-36" |
| 147 | + style={{ borderRadius: '4px', border: 'solid 1px var(--N200)' }} |
| 148 | + onClick={toggleCalender} |
| 149 | + > |
| 150 | + <p className="cursor mb-0 h-32 p-6">{calendarValue}</p> |
| 151 | + <Icon name="ic-caret-down-small" color="N600" /> |
| 152 | + </div> |
| 153 | + {showCalendar && ( |
| 154 | + <DayPickerRangeController |
| 155 | + startDate={calendar.startDate} |
| 156 | + endDate={calendar.endDate} |
| 157 | + focusedInput={focusedInput} |
| 158 | + onDatesChange={handleDatesChange} |
| 159 | + onFocusChange={handleFocusChange} |
| 160 | + numberOfMonths={1} |
| 161 | + withPortal |
| 162 | + renderCalendarInfo={renderDatePresets} |
| 163 | + calendarInfoPosition="after" |
| 164 | + hideKeyboardShortcutsPanel |
| 165 | + isOutsideRange={(day) => !isInclusivelyBeforeDay(day, moment())} // enable past dates |
| 166 | + renderCalendarDay={(props) => <CustomizableCalendarDay {...props} {...customDayStyles} />} |
| 167 | + onOutsideClick={hideCalender} |
| 168 | + initialVisibleMonth={() => moment().subtract(2, 'd')} // |
| 169 | + /> |
| 170 | + )} |
| 171 | + </> |
| 172 | + ) |
| 173 | +} |
0 commit comments