|
| 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 | +import CustomizableCalendarDay from 'react-dates/esm/components/CustomizableCalendarDay.js' |
| 20 | +import moment, { Moment } from 'moment' |
| 21 | + |
| 22 | +import 'react-dates/initialize' |
| 23 | + |
| 24 | +import { Icon } from '../Icon' |
| 25 | +import { customDayStyles, DayPickerCalendarInfoHorizontal, DayPickerRangeControllerPresets, styles } from './constants' |
| 26 | +import { DatePickerRangeControllerProps } from './types' |
| 27 | + |
| 28 | +import 'react-dates/lib/css/_datepicker.css' |
| 29 | + |
| 30 | +export const DatePickerRangeController = ({ |
| 31 | + handlePredefinedRange, |
| 32 | + handleApply, |
| 33 | + calendar, |
| 34 | + calendarInputs, |
| 35 | + handleDateInput, |
| 36 | + handleDatesChange, |
| 37 | + calendarValue, |
| 38 | + focusedInput, |
| 39 | + handleFocusChange, |
| 40 | +}: DatePickerRangeControllerProps) => { |
| 41 | + const [showCalendar, setShowCalender] = useState(false) |
| 42 | + const onClickApplyTimeChange = () => { |
| 43 | + setShowCalender(false) |
| 44 | + handleApply() |
| 45 | + } |
| 46 | + |
| 47 | + const onClickPredefinedTimeRange = (startDate: Moment, endDate: Moment, endStr: string) => () => { |
| 48 | + handlePredefinedRange(startDate, endDate, endStr) |
| 49 | + setShowCalender(false) |
| 50 | + } |
| 51 | + |
| 52 | + const renderDatePresets = () => ( |
| 53 | + <div |
| 54 | + className="flex left top" |
| 55 | + style={{ |
| 56 | + ...styles.PresetDateRangePicker_panel, |
| 57 | + ...DayPickerCalendarInfoHorizontal, |
| 58 | + ...{ |
| 59 | + PresetDateRangePicker_panel: { |
| 60 | + padding: '0px', |
| 61 | + width: '200px', |
| 62 | + height: '100%', |
| 63 | + }, |
| 64 | + ...styles.DayPicker__horizontal, |
| 65 | + }, |
| 66 | + }} |
| 67 | + > |
| 68 | + <div style={{ width: '312px', borderLeft: 'solid 1px var(--N200)', height: '304px', padding: '16px' }}> |
| 69 | + <p className="mb-16 fw-6">Pick time range</p> |
| 70 | + <div> |
| 71 | + <div className="w-100 mb-16"> |
| 72 | + From |
| 73 | + <input |
| 74 | + type="text" |
| 75 | + className="dc__block w-100 dc__border" |
| 76 | + value={calendarInputs.startDate} |
| 77 | + onChange={(event) => { |
| 78 | + handleDateInput('startDate', event.target.value) |
| 79 | + }} |
| 80 | + /> |
| 81 | + </div> |
| 82 | + <div className="w-100 mb-16"> |
| 83 | + To |
| 84 | + <input |
| 85 | + type="text" |
| 86 | + className="dc__block w-100 dc__border" |
| 87 | + value={calendarInputs.endDate} |
| 88 | + onChange={(event) => { |
| 89 | + handleDateInput('endDate', event.target.value) |
| 90 | + }} |
| 91 | + /> |
| 92 | + </div> |
| 93 | + <button type="button" className="cta small" onClick={onClickApplyTimeChange}> |
| 94 | + Apply Time Range |
| 95 | + </button> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + <div style={{ width: '220px', padding: '16px', borderLeft: 'solid 1px var(--N200)', height: '304px' }}> |
| 99 | + {DayPickerRangeControllerPresets.map(({ text, startDate, endDate, endStr }) => { |
| 100 | + const isSelected = |
| 101 | + startDate.isSame(calendar.startDate, 'minute') && |
| 102 | + startDate.isSame(calendar.startDate, 'hour') && |
| 103 | + startDate.isSame(calendar.startDate, 'day') && |
| 104 | + endDate.isSame(calendar.endDate, 'day') |
| 105 | + let buttonStyles = { |
| 106 | + ...styles.PresetDateRangePicker_button, |
| 107 | + } |
| 108 | + if (isSelected) { |
| 109 | + buttonStyles = { |
| 110 | + ...buttonStyles, |
| 111 | + ...styles.PresetDateRangePicker_button__selected, |
| 112 | + } |
| 113 | + } |
| 114 | + return ( |
| 115 | + <button |
| 116 | + type="button" |
| 117 | + key={text} |
| 118 | + style={{ ...buttonStyles, textAlign: 'left' }} |
| 119 | + onClick={onClickPredefinedTimeRange(startDate, endDate, endStr)} |
| 120 | + > |
| 121 | + {text} |
| 122 | + </button> |
| 123 | + ) |
| 124 | + })} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ) |
| 128 | + |
| 129 | + return ( |
| 130 | + <> |
| 131 | + <div |
| 132 | + data-testid="app-metrics-range-picker-box" |
| 133 | + className="flex h-36" |
| 134 | + style={{ borderRadius: '4px', border: 'solid 1px var(--N200)' }} |
| 135 | + onClick={() => { |
| 136 | + setShowCalender(!showCalendar) |
| 137 | + }} |
| 138 | + > |
| 139 | + <p className="cursor" style={{ marginBottom: '0', height: '32px', padding: '5px' }}> |
| 140 | + {calendarValue} |
| 141 | + </p> |
| 142 | + <Icon name="ic-caret-down-small" color="N600" /> |
| 143 | + </div> |
| 144 | + {showCalendar && ( |
| 145 | + <DayPickerRangeController |
| 146 | + startDate={calendar.startDate} |
| 147 | + endDate={calendar.endDate} |
| 148 | + focusedInput={focusedInput} |
| 149 | + onDatesChange={handleDatesChange} |
| 150 | + onFocusChange={handleFocusChange} |
| 151 | + numberOfMonths={1} |
| 152 | + withPortal |
| 153 | + renderCalendarInfo={renderDatePresets} |
| 154 | + calendarInfoPosition="after" |
| 155 | + hideKeyboardShortcutsPanel |
| 156 | + isOutsideRange={(day) => !isInclusivelyBeforeDay(day, moment())} // enable past dates |
| 157 | + renderCalendarDay={(props) => <CustomizableCalendarDay {...props} {...customDayStyles} />} |
| 158 | + onOutsideClick={() => { |
| 159 | + setShowCalender(false) |
| 160 | + }} |
| 161 | + initialVisibleMonth={() => moment().subtract(2, 'd')} // |
| 162 | + /> |
| 163 | + )} |
| 164 | + </> |
| 165 | + ) |
| 166 | +} |
0 commit comments