Feature/library manga actions (#506)
* Extend "IMangaGridProps" from "DefaultGridProps"
* Move library manga filtering into hook
* Add logic to select mangas
* Add manga actions
* Remove cache only policy for category mangas
Unclear why this was added
* Prevent SelectionFAB from being hidden by the mobile footer
On e.g. the library page, the footer is visible and thus, the fab was not completely visible
* Update categories of manga only after clicking "OK"
Previously, selecting a category resulted in an immediate mutation.
To be able to reuse the "CategorySelect" component to change the categories of multiple mangas at once, this behaviour is not suited.
* Add action to change categories of multiple mangas
* Cancel selection mode after removal from library
The mangas would still be selected and other action could get performed. However, this should only be possible for mangas that are in the library
* Remove unintentionally added console log
Accidentally added with 980da657d9
This commit is contained in:
@@ -8,34 +8,92 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
export const useSelectableCollection = <Id extends number | string>(totalCount: number) => {
|
||||
const [selectedItemIds, setSelectedItemIds] = useState<Id[]>([]);
|
||||
export type SelectableCollectionReturnType<Id extends number | string, Key extends string = string> = {
|
||||
selectedItemIds: Id[];
|
||||
keySelectedItemIds: Id[];
|
||||
areAllItemsSelected: boolean;
|
||||
areNoItemsSelected: boolean;
|
||||
areAllItemsForKeySelected: boolean;
|
||||
areNoItemsForKeySelected: boolean;
|
||||
handleSelection: (id: Id, selected: boolean, key?: Key) => void;
|
||||
handleSelectAll: (selectAll: boolean, itemIds: Id[], key?: Key) => void;
|
||||
setSelectionForKey: (key: Key, itemIds: Id[]) => void;
|
||||
getSelectionForKey: (key: Key) => Id[];
|
||||
};
|
||||
|
||||
export const useSelectableCollection = <Id extends number | string, Key extends string = 'default'>(
|
||||
totalCount: number,
|
||||
{
|
||||
keyCount = totalCount,
|
||||
currentKey,
|
||||
initialState = {} as Record<Key, Id[]>,
|
||||
}: {
|
||||
keyCount?: number;
|
||||
currentKey: Key;
|
||||
initialState?: Record<Key, Id[]>;
|
||||
},
|
||||
): SelectableCollectionReturnType<Id, Key> => {
|
||||
const [keyToSelectedItemIds, setKeyToSelectedItemIds] = useState<Record<string, Id[]>>(initialState);
|
||||
|
||||
const selectedItemIds = Object.values(keyToSelectedItemIds).flat();
|
||||
const areAllItemsSelected = selectedItemIds.length === totalCount;
|
||||
const areNoItemsSelected = !selectedItemIds.length;
|
||||
|
||||
const handleSelection = (id: Id, selected: boolean) => {
|
||||
const keySelectedItemIds = keyToSelectedItemIds[currentKey] ?? [];
|
||||
const areAllItemsForKeySelected = keySelectedItemIds.length === keyCount;
|
||||
const areNoItemsForKeySelected = keySelectedItemIds.length === 0;
|
||||
|
||||
const handleSelection = (id: Id, selected: boolean, key: Key = currentKey) => {
|
||||
const deselect = !selected;
|
||||
if (deselect) {
|
||||
setSelectedItemIds(selectedItemIds.filter((selectedItemId) => selectedItemId !== id));
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: prevState[key].filter((selectedItemId) => selectedItemId !== id),
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedItemIds([...new Set([...selectedItemIds, id])]);
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [...new Set([...(prevState[key] ?? []), id])],
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSelectAll = (selectAll: boolean, itemIds: Id[]) => {
|
||||
const handleSelectAll = (selectAll: boolean, itemIds: Id[], key: Key = currentKey) => {
|
||||
switch (selectAll) {
|
||||
case true:
|
||||
setSelectedItemIds([...itemIds]);
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [...itemIds],
|
||||
}));
|
||||
break;
|
||||
case false:
|
||||
setSelectedItemIds([]);
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [],
|
||||
}));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
return { selectedItemIds, handleSelection, handleSelectAll, areAllItemsSelected, areNoItemsSelected };
|
||||
const setSelectionForKey = (key: Key, itemIds: Id[]) => {
|
||||
keyToSelectedItemIds[key] = itemIds;
|
||||
};
|
||||
|
||||
const getSelectionForKey = (key: Key) => keyToSelectedItemIds[key];
|
||||
|
||||
return {
|
||||
selectedItemIds,
|
||||
keySelectedItemIds,
|
||||
handleSelection,
|
||||
handleSelectAll,
|
||||
areAllItemsSelected,
|
||||
areNoItemsSelected,
|
||||
areAllItemsForKeySelected,
|
||||
areNoItemsForKeySelected,
|
||||
setSelectionForKey,
|
||||
getSelectionForKey,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user