feat: refresh interval is added (#1712)

* feat: refresh interval is added
This commit is contained in:
Palash Gupta 2022-11-14 22:32:19 +05:30 committed by GitHub
parent a50d7f227c
commit 2e124da366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 120 additions and 51 deletions

View File

@ -1,6 +1,6 @@
export const options: IOptions[] = [ export const options: IOptions[] = [
{ {
label: '', label: 'off',
key: 'off', key: 'off',
value: 0, value: 0,
}, },

View File

@ -1,9 +1,20 @@
import { Select } from 'antd'; import { CaretDownFilled } from '@ant-design/icons';
import {
Checkbox,
Divider,
Popover,
Radio,
RadioChangeEvent,
Space,
Typography,
} from 'antd';
import { CheckboxChangeEvent } from 'antd/lib/checkbox';
import get from 'api/browser/localstorage/get'; import get from 'api/browser/localstorage/get';
import set from 'api/browser/localstorage/set'; import set from 'api/browser/localstorage/set';
import { DASHBOARD_TIME_IN_DURATION } from 'constants/app'; import { DASHBOARD_TIME_IN_DURATION } from 'constants/app';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import useUrlQuery from 'hooks/useUrlQuery'; import useUrlQuery from 'hooks/useUrlQuery';
import _omit from 'lodash-es/omit';
import React, { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
@ -15,7 +26,7 @@ import { UPDATE_TIME_INTERVAL } from 'types/actions/globalTime';
import { GlobalReducer } from 'types/reducer/globalTime'; import { GlobalReducer } from 'types/reducer/globalTime';
import { options } from './config'; import { options } from './config';
import { SelectContainer } from './styles'; import { ButtonContainer, Container } from './styles';
function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element {
const { minTime: initialMinTime, selectedTime } = useSelector< const { minTime: initialMinTime, selectedTime } = useSelector<
@ -24,8 +35,6 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element {
>((state) => state.globalTime); >((state) => state.globalTime);
const { pathname } = useLocation(); const { pathname } = useLocation();
const params = useUrlQuery();
const localStorageData = JSON.parse(get(DASHBOARD_TIME_IN_DURATION) || '{}'); const localStorageData = JSON.parse(get(DASHBOARD_TIME_IN_DURATION) || '{}');
const localStorageValue = useMemo(() => localStorageData[pathname], [ const localStorageValue = useMemo(() => localStorageData[pathname], [
@ -33,6 +42,16 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element {
localStorageData, localStorageData,
]); ]);
const [isAutoRefreshEnabled, setIsAutoRefreshfreshEnabled] = useState<boolean>(
Boolean(localStorageValue),
);
useEffect(() => {
setIsAutoRefreshfreshEnabled(Boolean(localStorageValue));
}, [localStorageValue]);
const params = useUrlQuery();
const dispatch = useDispatch<Dispatch<AppActions>>(); const dispatch = useDispatch<Dispatch<AppActions>>();
const [selectedOption, setSelectedOption] = useState<string>( const [selectedOption, setSelectedOption] = useState<string>(
localStorageValue || options[0].key, localStorageValue || options[0].key,
@ -50,7 +69,7 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element {
useInterval(() => { useInterval(() => {
const selectedValue = getOption?.value; const selectedValue = getOption?.value;
if (disabled) { if (disabled || !isAutoRefreshEnabled) {
return; return;
} }
@ -69,31 +88,70 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element {
}, getOption?.value || 0); }, getOption?.value || 0);
const onChangeHandler = useCallback( const onChangeHandler = useCallback(
(value: unknown) => { (event: RadioChangeEvent) => {
if (typeof value === 'string') { const selectedValue = event.target.value;
setSelectedOption(value); setSelectedOption(selectedValue);
params.set(DASHBOARD_TIME_IN_DURATION, value); params.set(DASHBOARD_TIME_IN_DURATION, selectedValue);
set( set(
DASHBOARD_TIME_IN_DURATION, DASHBOARD_TIME_IN_DURATION,
JSON.stringify({ ...localStorageData, [pathname]: value }), JSON.stringify({ ...localStorageData, [pathname]: selectedValue }),
); );
} setIsAutoRefreshfreshEnabled(true);
}, },
[params, pathname, localStorageData], [params, pathname, localStorageData],
); );
const onChangeAutoRefreshHandler = useCallback(
(event: CheckboxChangeEvent) => {
const { checked } = event.target;
if (!checked) {
// remove the path from localstorage
set(
DASHBOARD_TIME_IN_DURATION,
JSON.stringify(_omit(localStorageData, pathname)),
);
}
setIsAutoRefreshfreshEnabled(checked);
},
[localStorageData, pathname],
);
return ( return (
<SelectContainer <Popover
disabled={disabled} placement="bottomLeft"
onChange={onChangeHandler} trigger={['click']}
value={selectedOption} content={
<Container>
<Checkbox
onChange={onChangeAutoRefreshHandler}
checked={isAutoRefreshEnabled}
disabled={disabled}
>
Auto Refresh
</Checkbox>
<Divider />
<Typography.Paragraph>Refresh Interval</Typography.Paragraph>
<Radio.Group onChange={onChangeHandler} value={selectedOption}>
<Space direction="vertical">
{options
.filter((e) => e.label !== 'off')
.map((option) => (
<Radio key={option.key} value={option.key}>
{option.label}
</Radio>
))}
</Space>
</Radio.Group>
</Container>
}
> >
{options.map((option) => ( <ButtonContainer title="Set auto refresh" type="primary">
<Select.Option key={option.key} value={option.key}> <CaretDownFilled />
{option.label} </ButtonContainer>
</Select.Option> </Popover>
))}
</SelectContainer>
); );
} }

View File

@ -1,9 +1,13 @@
import { Select } from 'antd'; import { Button } from 'antd';
import styled from 'styled-components'; import styled from 'styled-components';
export const SelectContainer = styled(Select)` export const Container = styled.div`
min-width: 8rem;
`;
export const ButtonContainer = styled(Button)`
&&& { &&& {
width: 100%; padding-left: 0.5rem;
min-width: 4rem; padding-right: 0.5rem;
} }
`; `;

View File

@ -19,7 +19,7 @@ import AutoRefresh from '../AutoRefresh';
import CustomDateTimeModal, { DateTimeRangeType } from '../CustomDateTimeModal'; import CustomDateTimeModal, { DateTimeRangeType } from '../CustomDateTimeModal';
import { getDefaultOption, getOptions, Time } from './config'; import { getDefaultOption, getOptions, Time } from './config';
import RefreshText from './Refresh'; import RefreshText from './Refresh';
import { Form, FormItem } from './styles'; import { Form, FormContainer, FormItem } from './styles';
const { Option } = DefaultSelect; const { Option } = DefaultSelect;
@ -263,29 +263,31 @@ function DateTimeSelection({
layout="inline" layout="inline"
initialValues={{ interval: selectedTime }} initialValues={{ interval: selectedTime }}
> >
<DefaultSelect <FormContainer>
onSelect={(value: unknown): void => onSelectHandler(value as Time)} <DefaultSelect
value={getInputLabel(startTime, endTime, selectedTime)} onSelect={(value: unknown): void => onSelectHandler(value as Time)}
data-testid="dropDown" value={getInputLabel(startTime, endTime, selectedTime)}
> data-testid="dropDown"
{options.map(({ value, label }) => ( >
<Option key={value + label} value={value}> {options.map(({ value, label }) => (
{label} <Option key={value + label} value={value}>
</Option> {label}
))} </Option>
</DefaultSelect> ))}
</DefaultSelect>
<FormItem hidden={refreshButtonHidden}> <FormItem hidden={refreshButtonHidden}>
<Button <Button
icon={<SyncOutlined />} icon={<SyncOutlined />}
type="primary" type="primary"
onClick={onRefreshHandler} onClick={onRefreshHandler}
/> />
</FormItem> </FormItem>
<FormItem> <FormItem>
<AutoRefresh disabled={refreshButtonHidden} /> <AutoRefresh disabled={refreshButtonHidden} />
</FormItem> </FormItem>
</FormContainer>
</Form> </Form>
<RefreshText <RefreshText

View File

@ -28,3 +28,8 @@ export const RefreshTextContainer = styled.div<Props>`
visibility: ${({ refreshButtonHidden }): string => visibility: ${({ refreshButtonHidden }): string =>
refreshButtonHidden ? 'hidden' : 'visible'}; refreshButtonHidden ? 'hidden' : 'visible'};
`; `;
export const FormContainer = styled.div`
display: flex;
gap: 0.1rem;
`;