Skip to content

Commit

Permalink
feat: [SIG-543]: add time selection in the custom date selection (Sig…
Browse files Browse the repository at this point in the history
…Noz#4658)

* feat: [SIG-543]: inital commit

* feat: [SIG-543]: refactor date time modal to separate component

* feat: [SIG-543]: refactor date time modal to separate component

* feat: add back the time support according to the older designs in the date time picker

* fix: custom time picker minor UI fixes
  • Loading branch information
vikrantgupta25 committed Mar 11, 2024
1 parent 9ace374 commit 49aba4f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 53 deletions.
3 changes: 3 additions & 0 deletions frontend/src/components/CustomTimePicker/CustomTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ function CustomTimePicker({

const handleOpenChange = (newOpen: boolean): void => {
setOpen(newOpen);
if (!newOpen) {
setCustomDTPickerVisible?.(false);
}
};

const debouncedHandleInputChange = debounce((inputValue): void => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './CustomTimePicker.styles.scss';

import { Button, DatePicker } from 'antd';
import { Button } from 'antd';
import cx from 'classnames';
import ROUTES from 'constants/routes';
import { DateTimeRangeType } from 'container/TopNav/CustomDateTimeModal';
Expand All @@ -9,12 +9,10 @@ import {
Option,
RelativeDurationSuggestionOptions,
} from 'container/TopNav/DateTimeSelectionV2/config';
import dayjs, { Dayjs } from 'dayjs';
import { Dispatch, SetStateAction, useMemo } from 'react';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { AppState } from 'store/reducers';
import { GlobalReducer } from 'types/reducer/globalTime';

import RangePickerModal from './RangePickerModal';

interface CustomTimePickerPopoverContentProps {
options: any[];
Expand All @@ -40,35 +38,12 @@ function CustomTimePickerPopoverContent({
handleGoLive,
selectedTime,
}: CustomTimePickerPopoverContentProps): JSX.Element {
const { RangePicker } = DatePicker;
const { pathname } = useLocation();

const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(
(state) => state.globalTime,
);

const isLogsExplorerPage = useMemo(() => pathname === ROUTES.LOGS_EXPLORER, [
pathname,
]);

const disabledDate = (current: Dayjs): boolean => {
const currentDay = dayjs(current);
return currentDay.isAfter(dayjs());
};

const onPopoverClose = (visible: boolean): void => {
if (!visible) {
setCustomDTPickerVisible(false);
}
setIsOpen(visible);
};

const onModalOkHandler = (date_time: any): void => {
if (date_time?.[1]) {
onPopoverClose(false);
}
onCustomDateHandler(date_time, LexicalContext.CUSTOM_DATE_PICKER);
};
function getTimeChips(options: Option[]): JSX.Element {
return (
<div className="relative-date-time-section">
Expand Down Expand Up @@ -105,7 +80,9 @@ function CustomTimePickerPopoverContent({
}}
className={cx(
'date-time-options-btn',
selectedTime === option.value && 'active',
customDateTimeVisible
? option.value === 'custom' && 'active'
: selectedTime === option.value && 'active',
)}
>
{option.label}
Expand All @@ -121,14 +98,11 @@ function CustomTimePickerPopoverContent({
)}
>
{selectedTime === 'custom' || customDateTimeVisible ? (
<RangePicker
disabledDate={disabledDate}
allowClear
onCalendarChange={onModalOkHandler}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(selectedTime === 'custom' && {
defaultValue: [dayjs(minTime / 1000000), dayjs(maxTime / 1000000)],
})}
<RangePickerModal
setCustomDTPickerVisible={setCustomDTPickerVisible}
setIsOpen={setIsOpen}
onCustomDateHandler={onCustomDateHandler}
selectedTime={selectedTime}
/>
) : (
<div className="relative-times-container">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.custom-date-picker {
display: flex;
flex-direction: column;
}
68 changes: 68 additions & 0 deletions frontend/src/components/CustomTimePicker/RangePickerModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import './RangePickerModal.styles.scss';

import { DatePicker } from 'antd';
import { DateTimeRangeType } from 'container/TopNav/CustomDateTimeModal';
import { LexicalContext } from 'container/TopNav/DateTimeSelectionV2/config';
import dayjs, { Dayjs } from 'dayjs';
import { Dispatch, SetStateAction } from 'react';
import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers';
import { GlobalReducer } from 'types/reducer/globalTime';

interface RangePickerModalProps {
setCustomDTPickerVisible: Dispatch<SetStateAction<boolean>>;
setIsOpen: Dispatch<SetStateAction<boolean>>;
onCustomDateHandler: (
dateTimeRange: DateTimeRangeType,
lexicalContext?: LexicalContext | undefined,
) => void;
selectedTime: string;
}

function RangePickerModal(props: RangePickerModalProps): JSX.Element {
const {
setCustomDTPickerVisible,
setIsOpen,
onCustomDateHandler,
selectedTime,
} = props;
const { RangePicker } = DatePicker;
const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(
(state) => state.globalTime,
);

const disabledDate = (current: Dayjs): boolean => {
const currentDay = dayjs(current);
return currentDay.isAfter(dayjs());
};

const onPopoverClose = (visible: boolean): void => {
if (!visible) {
setCustomDTPickerVisible(false);
}
setIsOpen(visible);
};

const onModalOkHandler = (date_time: any): void => {
if (date_time?.[1]) {
onPopoverClose(false);
}
onCustomDateHandler(date_time, LexicalContext.CUSTOM_DATE_PICKER);
};
return (
<div className="custom-date-picker">
<RangePicker
disabledDate={disabledDate}
allowClear
showTime
onOk={onModalOkHandler}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(selectedTime === 'custom' && {
defaultValue: [dayjs(minTime / 1000000), dayjs(maxTime / 1000000)],
})}
/>
</div>
);
}

export default RangePickerModal;
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@
padding: 13px 14px;

&.date-picker {
width: 340px;
height: 380px;
width: 480px;
height: 430px;
}

&.relative-times {
Expand Down
17 changes: 3 additions & 14 deletions frontend/src/container/TopNav/DateTimeSelectionV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import { DateTimeRangeType } from '../CustomDateTimeModal';
import {
getDefaultOption,
getOptions,
LexicalContext,
LocalStorageTimeRange,
Time,
TimeRange,
Expand Down Expand Up @@ -319,22 +318,12 @@ function DateTimeSelection({
onLastRefreshHandler();
};

const onCustomDateHandler = (
dateTimeRange: DateTimeRangeType,
lexicalContext?: LexicalContext,
): void => {
const onCustomDateHandler = (dateTimeRange: DateTimeRangeType): void => {
if (dateTimeRange !== null) {
const [startTimeMoment, endTimeMoment] = dateTimeRange;
if (startTimeMoment && endTimeMoment) {
let startTime = startTimeMoment;
let endTime = endTimeMoment;
if (
lexicalContext &&
lexicalContext === LexicalContext.CUSTOM_DATE_PICKER
) {
startTime = startTime.startOf('day');
endTime = endTime.endOf('day');
}
const startTime = startTimeMoment;
const endTime = endTimeMoment;
setCustomDTPickerVisible(false);
updateTimeInterval('custom', [
startTime.toDate().getTime(),
Expand Down

0 comments on commit 49aba4f

Please sign in to comment.