2023-06-19 15:57:58 +03:00
|
|
|
import { LeftOutlined, RightOutlined } from '@ant-design/icons';
|
|
|
|
|
import { Button, Select } from 'antd';
|
2023-07-04 08:24:34 +03:00
|
|
|
import { DEFAULT_PER_PAGE_OPTIONS, Pagination } from 'hooks/queryPagination';
|
2023-06-19 15:57:58 +03:00
|
|
|
import { memo, useMemo } from 'react';
|
2023-09-25 19:39:10 +05:30
|
|
|
import { popupContainer } from 'utils/selectPopupContainer';
|
2023-06-19 15:57:58 +03:00
|
|
|
|
2023-07-04 08:24:34 +03:00
|
|
|
import { defaultSelectStyle } from './config';
|
2023-06-19 15:57:58 +03:00
|
|
|
import { Container } from './styles';
|
|
|
|
|
|
2023-06-23 21:39:59 +03:00
|
|
|
function Controls({
|
|
|
|
|
offset = 0,
|
2023-07-04 08:24:34 +03:00
|
|
|
perPageOptions = DEFAULT_PER_PAGE_OPTIONS,
|
2023-06-23 21:39:59 +03:00
|
|
|
isLoading,
|
|
|
|
|
totalCount,
|
|
|
|
|
countPerPage,
|
|
|
|
|
handleNavigatePrevious,
|
|
|
|
|
handleNavigateNext,
|
|
|
|
|
handleCountItemsPerPageChange,
|
2024-02-20 16:21:07 +05:30
|
|
|
isLogPanel = false,
|
2023-06-23 21:39:59 +03:00
|
|
|
}: ControlsProps): JSX.Element | null {
|
2023-06-19 15:57:58 +03:00
|
|
|
const isNextAndPreviousDisabled = useMemo(
|
2023-06-23 21:39:59 +03:00
|
|
|
() => isLoading || countPerPage < 0 || totalCount === 0,
|
|
|
|
|
[isLoading, countPerPage, totalCount],
|
2023-06-19 15:57:58 +03:00
|
|
|
);
|
2024-02-20 16:21:07 +05:30
|
|
|
const isPreviousDisabled = useMemo(
|
|
|
|
|
() => (isLogPanel ? false : offset <= 0 || isNextAndPreviousDisabled),
|
|
|
|
|
[isLogPanel, isNextAndPreviousDisabled, offset],
|
|
|
|
|
);
|
|
|
|
|
const isNextDisabled = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
isLogPanel ? false : totalCount < countPerPage || isNextAndPreviousDisabled,
|
|
|
|
|
[countPerPage, isLogPanel, isNextAndPreviousDisabled, totalCount],
|
|
|
|
|
);
|
2023-06-19 15:57:58 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Container>
|
|
|
|
|
<Button
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
size="small"
|
|
|
|
|
type="link"
|
2024-02-20 16:21:07 +05:30
|
|
|
disabled={isPreviousDisabled}
|
2023-06-19 15:57:58 +03:00
|
|
|
onClick={handleNavigatePrevious}
|
|
|
|
|
>
|
|
|
|
|
<LeftOutlined /> Previous
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
size="small"
|
|
|
|
|
type="link"
|
2024-02-20 16:21:07 +05:30
|
|
|
disabled={isNextDisabled}
|
2023-06-19 15:57:58 +03:00
|
|
|
onClick={handleNavigateNext}
|
|
|
|
|
>
|
|
|
|
|
Next <RightOutlined />
|
|
|
|
|
</Button>
|
2023-06-23 21:39:59 +03:00
|
|
|
<Select<Pagination['limit']>
|
2023-06-19 15:57:58 +03:00
|
|
|
style={defaultSelectStyle}
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
value={countPerPage}
|
|
|
|
|
onChange={handleCountItemsPerPageChange}
|
2023-09-25 19:39:10 +05:30
|
|
|
getPopupContainer={popupContainer}
|
2023-06-19 15:57:58 +03:00
|
|
|
>
|
2023-07-04 08:24:34 +03:00
|
|
|
{perPageOptions.map((count) => (
|
2023-06-19 15:57:58 +03:00
|
|
|
<Select.Option
|
|
|
|
|
key={count}
|
|
|
|
|
value={count}
|
|
|
|
|
>{`${count} / page`}</Select.Option>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 21:39:59 +03:00
|
|
|
Controls.defaultProps = {
|
|
|
|
|
offset: 0,
|
2023-07-04 08:24:34 +03:00
|
|
|
perPageOptions: DEFAULT_PER_PAGE_OPTIONS,
|
2024-02-20 16:21:07 +05:30
|
|
|
isLogPanel: false,
|
2023-06-23 21:39:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface ControlsProps {
|
|
|
|
|
offset?: Pagination['offset'];
|
2023-07-04 08:24:34 +03:00
|
|
|
perPageOptions?: number[];
|
2023-06-23 21:39:59 +03:00
|
|
|
totalCount: number;
|
|
|
|
|
countPerPage: Pagination['limit'];
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
handleNavigatePrevious: () => void;
|
|
|
|
|
handleNavigateNext: () => void;
|
|
|
|
|
handleCountItemsPerPageChange: (value: Pagination['limit']) => void;
|
2024-02-20 16:21:07 +05:30
|
|
|
isLogPanel?: boolean;
|
2023-06-23 21:39:59 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:57:58 +03:00
|
|
|
export default memo(Controls);
|