2022-10-03 01:23:59 +03:00
|
|
|
import React, { FC, useCallback, useEffect, useRef } from 'react';
|
2022-08-06 01:36:13 +03:00
|
|
|
import IconButtonElement from '../../elements/IconButtonElement';
|
2022-10-14 02:07:54 +03:00
|
|
|
import { QueryI } from './interface';
|
2022-08-06 01:36:13 +03:00
|
|
|
|
2022-10-14 02:07:54 +03:00
|
|
|
interface SortProps {
|
2022-08-21 03:09:22 +03:00
|
|
|
getSortMenuOptions: () => {
|
|
|
|
name: string;
|
2022-10-03 01:23:59 +03:00
|
|
|
value: string;
|
|
|
|
}[]
|
|
|
|
getSortValues: () => {
|
|
|
|
sortBy: string;
|
|
|
|
sortOrder: string;
|
|
|
|
}
|
2022-08-21 03:09:22 +03:00
|
|
|
getSettingsKey: () => string;
|
2022-10-14 02:07:54 +03:00
|
|
|
setQuery: React.Dispatch<React.SetStateAction<QueryI>>;
|
2022-08-06 01:36:13 +03:00
|
|
|
reloadItems: () => void;
|
|
|
|
}
|
|
|
|
|
2022-10-14 02:07:54 +03:00
|
|
|
const Sort: FC<SortProps> = ({ getSortMenuOptions, getSortValues, getSettingsKey, setQuery, reloadItems }) => {
|
2022-08-06 01:36:13 +03:00
|
|
|
const element = useRef<HTMLDivElement>(null);
|
|
|
|
|
2022-10-03 01:23:59 +03:00
|
|
|
const showSortMenu = useCallback(() => {
|
|
|
|
import('../../components/sortmenu/sortmenu').then(({default: SortMenu}) => {
|
|
|
|
const sortMenu = new SortMenu();
|
|
|
|
sortMenu.show({
|
|
|
|
settingsKey: getSettingsKey(),
|
|
|
|
settings: getSortValues(),
|
|
|
|
sortOptions: getSortMenuOptions()
|
|
|
|
}).then(() => {
|
2022-10-14 02:07:54 +03:00
|
|
|
setQuery({StartIndex: 0});
|
2022-10-03 01:23:59 +03:00
|
|
|
reloadItems();
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 02:07:54 +03:00
|
|
|
}, [getSettingsKey, getSortMenuOptions, getSortValues, reloadItems, setQuery]);
|
2022-10-03 01:23:59 +03:00
|
|
|
|
2022-08-06 01:36:13 +03:00
|
|
|
useEffect(() => {
|
|
|
|
const btnSort = element.current?.querySelector('.btnSort');
|
|
|
|
|
2022-10-03 01:23:59 +03:00
|
|
|
btnSort?.addEventListener('click', showSortMenu);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
btnSort?.removeEventListener('click', showSortMenu);
|
|
|
|
};
|
|
|
|
}, [showSortMenu]);
|
2022-08-06 01:36:13 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={element}>
|
|
|
|
<IconButtonElement
|
|
|
|
is='paper-icon-button-light'
|
|
|
|
className='btnSort autoSize'
|
|
|
|
title='Sort'
|
|
|
|
icon='material-icons sort_by_alpha'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Sort;
|