feat: loading is added in the button (#1927)

* feat: loading is added in the button

* chore: disable condition is updated

Co-authored-by: Pranay Prateek <pranay@signoz.io>
This commit is contained in:
Palash Gupta 2023-01-02 12:08:35 +05:30 committed by GitHub
parent 31a3bc09c8
commit 126c9238ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 14 deletions

View File

@ -0,0 +1 @@
export const ITEMS_PER_PAGE_OPTIONS = [25, 50, 100, 200];

View File

@ -4,7 +4,7 @@ import {
RightOutlined, RightOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { Button, Divider, Select } from 'antd'; import { Button, Divider, Select } from 'antd';
import React, { memo } from 'react'; import React, { memo, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { AppState } from 'store/reducers'; import { AppState } from 'store/reducers';
import { import {
@ -15,16 +15,19 @@ import {
} from 'types/actions/logs'; } from 'types/actions/logs';
import { ILogsReducer } from 'types/reducer/logs'; import { ILogsReducer } from 'types/reducer/logs';
import { ITEMS_PER_PAGE_OPTIONS } from './config';
import { Container } from './styles'; import { Container } from './styles';
const { Option } = Select; const { Option } = Select;
const ITEMS_PER_PAGE_OPTIONS = [25, 50, 100, 200];
function LogControls(): JSX.Element | null { function LogControls(): JSX.Element | null {
const { logLinesPerPage, liveTail } = useSelector<AppState, ILogsReducer>( const {
(state) => state.logs, logLinesPerPage,
); liveTail,
isLoading: isLogsLoading,
isLoadingAggregate,
logs,
} = useSelector<AppState, ILogsReducer>((state) => state.logs);
const dispatch = useDispatch(); const dispatch = useDispatch();
const handleLogLinesPerPageChange = (e: number): void => { const handleLogLinesPerPageChange = (e: number): void => {
@ -51,29 +54,58 @@ function LogControls(): JSX.Element | null {
}); });
}; };
const isLoading = isLogsLoading || isLoadingAggregate;
const isNextAndPreviousDisabled = useMemo(
() =>
isLoading ||
logLinesPerPage === 0 ||
logs.length === 0 ||
logs.length < logLinesPerPage,
[isLoading, logLinesPerPage, logs.length],
);
if (liveTail !== 'STOPPED') { if (liveTail !== 'STOPPED') {
return null; return null;
} }
return ( return (
<Container> <Container>
<Button size="small" type="link" onClick={handleGoToLatest}> <Button
loading={isLoading}
size="small"
type="link"
onClick={handleGoToLatest}
>
<FastBackwardOutlined /> Go to latest <FastBackwardOutlined /> Go to latest
</Button> </Button>
<Divider type="vertical" /> <Divider type="vertical" />
<Button size="small" type="link" onClick={handleNavigatePrevious}> <Button
loading={isLoading}
size="small"
type="link"
disabled={isNextAndPreviousDisabled}
onClick={handleNavigatePrevious}
>
<LeftOutlined /> Previous <LeftOutlined /> Previous
</Button> </Button>
<Button size="small" type="link" onClick={handleNavigateNext}> <Button
loading={isLoading}
size="small"
type="link"
disabled={isNextAndPreviousDisabled}
onClick={handleNavigateNext}
>
Next <RightOutlined /> Next <RightOutlined />
</Button> </Button>
<Select <Select
style={{ width: 120 }} loading={isLoading}
value={logLinesPerPage} value={logLinesPerPage}
onChange={handleLogLinesPerPageChange} onChange={handleLogLinesPerPageChange}
> >
{ITEMS_PER_PAGE_OPTIONS.map((count) => { {ITEMS_PER_PAGE_OPTIONS.map((count) => (
return <Option key={count} value={count}>{`${count} / page`}</Option>; <Option key={count} value={count}>{`${count} / page`}</Option>
})} ))}
</Select> </Select>
</Container> </Container>
); );

View File

@ -16,7 +16,12 @@ import { getLogs } from 'store/actions/logs/getLogs';
import { getLogsAggregate } from 'store/actions/logs/getLogsAggregate'; import { getLogsAggregate } from 'store/actions/logs/getLogsAggregate';
import { AppState } from 'store/reducers'; import { AppState } from 'store/reducers';
import AppActions from 'types/actions'; import AppActions from 'types/actions';
import { FLUSH_LOGS, TOGGLE_LIVE_TAIL } from 'types/actions/logs'; import {
FLUSH_LOGS,
SET_LOADING,
SET_LOADING_AGGREGATE,
TOGGLE_LIVE_TAIL,
} from 'types/actions/logs';
import { GlobalReducer } from 'types/reducer/globalTime'; import { GlobalReducer } from 'types/reducer/globalTime';
import { ILogsReducer } from 'types/reducer/logs'; import { ILogsReducer } from 'types/reducer/logs';
@ -121,7 +126,17 @@ function SearchFilter({
const urlQueryString = urlQuery.get('q'); const urlQueryString = urlQuery.get('q');
useEffect(() => { useEffect(() => {
dispatch({
type: SET_LOADING,
payload: true,
});
dispatch({
type: SET_LOADING_AGGREGATE,
payload: true,
});
const debouncedHandleSearch = debounce(handleSearch, 600); const debouncedHandleSearch = debounce(handleSearch, 600);
debouncedHandleSearch(urlQueryString || ''); debouncedHandleSearch(urlQueryString || '');
return (): void => { return (): void => {