2023-08-01 20:14:03 +03:00
|
|
|
import { useLayoutEffect, useState } from 'react';
|
2023-07-25 20:27:29 +03:00
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
import BackendError from '../../components/BackendError'
|
|
|
|
import { Loader } from '../../components/Common/Loader'
|
2023-08-01 20:14:03 +03:00
|
|
|
import { useLibrary } from '../../context/LibraryContext';
|
2023-08-25 22:51:20 +03:00
|
|
|
import { ILibraryFilter, ILibraryItem } from '../../utils/models';
|
2023-08-26 19:39:49 +03:00
|
|
|
import SearchPanel from './SearchPanel';
|
2023-07-28 00:03:37 +03:00
|
|
|
import ViewLibrary from './ViewLibrary';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-28 00:03:37 +03:00
|
|
|
function LibraryPage() {
|
2023-08-01 20:14:03 +03:00
|
|
|
const library = useLibrary();
|
|
|
|
|
|
|
|
const [ filterParams, setFilterParams ] = useState<ILibraryFilter>({});
|
2023-08-25 22:51:20 +03:00
|
|
|
const [ items, setItems ] = useState<ILibraryItem[]>([]);
|
2023-08-01 20:14:03 +03:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
const filter = filterParams;
|
|
|
|
setItems(library.filter(filter));
|
2023-08-26 19:39:49 +03:00
|
|
|
}, [library, filterParams]);
|
2023-07-25 20:27:29 +03:00
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
return (
|
2023-07-20 17:11:03 +03:00
|
|
|
<div className='w-full'>
|
2023-08-01 20:14:03 +03:00
|
|
|
{ library.loading && <Loader /> }
|
|
|
|
{ library.error && <BackendError error={library.error} />}
|
2023-08-26 19:39:49 +03:00
|
|
|
{ !library.loading && library.items &&
|
|
|
|
<div className='flex flex-col w-full'>
|
|
|
|
<SearchPanel
|
|
|
|
filter={filterParams}
|
|
|
|
setFilter={setFilterParams}
|
|
|
|
/>
|
|
|
|
<ViewLibrary
|
|
|
|
cleanQuery={() => setFilterParams({})}
|
|
|
|
items={items}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:03:37 +03:00
|
|
|
export default LibraryPage;
|