pl-fe: Migrate bookmark folders to tanstack query

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-11-09 23:34:45 +01:00
parent 42f7226594
commit 1d76c05844
11 changed files with 48 additions and 90 deletions

View file

@ -1,32 +1,5 @@
import { Entities } from 'pl-fe/entity-store/entities';
import { selectEntity } from 'pl-fe/entity-store/selectors';
import { useAppSelector } from 'pl-fe/hooks/use-app-selector';
import { useBookmarkFolders } from './use-bookmark-folders';
import type{ BookmarkFolder } from 'pl-api';
const useBookmarkFolder = (folderId?: string) => {
const {
isError,
isFetched,
isFetching,
isLoading,
invalidate,
} = useBookmarkFolders();
const bookmarkFolder = useAppSelector(state => folderId
? selectEntity<BookmarkFolder>(state, Entities.BOOKMARK_FOLDERS, folderId)
: undefined);
return {
bookmarkFolder,
isError,
isFetched,
isFetching,
isLoading,
invalidate,
};
};
const useBookmarkFolder = (folderId?: string) => useBookmarkFolders((data) => folderId ? data.find(folder => folder.id === folderId) : undefined);
export { useBookmarkFolder };

View file

@ -1,26 +1,22 @@
import { Entities } from 'pl-fe/entity-store/entities';
import { useEntities } from 'pl-fe/entity-store/hooks/use-entities';
import { useQuery } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks/use-client';
import { useFeatures } from 'pl-fe/hooks/use-features';
import type { BookmarkFolder } from 'pl-api';
const useBookmarkFolders = () => {
const useBookmarkFolders = <T>(
select?: ((data: Array<BookmarkFolder>) => T),
) => {
const client = useClient();
const features = useFeatures();
const { entities, ...result } = useEntities<BookmarkFolder>(
[Entities.BOOKMARK_FOLDERS],
() => client.myAccount.getBookmarkFolders(),
{ enabled: features.bookmarkFolders },
);
const bookmarkFolders = entities;
return {
...result,
bookmarkFolders,
};
return useQuery({
queryKey: ['bookmarkFolders'],
queryFn: () => client.myAccount.getBookmarkFolders(),
enabled: features.bookmarkFolders,
select,
});
};
export { useBookmarkFolders };

View file

@ -1,6 +1,7 @@
import { Entities } from 'pl-fe/entity-store/entities';
import { useCreateEntity } from 'pl-fe/entity-store/hooks/use-create-entity';
import { useMutation } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks/use-client';
import { queryClient } from 'pl-fe/queries/client';
interface CreateBookmarkFolderParams {
name: string;
@ -10,16 +11,11 @@ interface CreateBookmarkFolderParams {
const useCreateBookmarkFolder = () => {
const client = useClient();
const { createEntity, ...rest } = useCreateEntity(
[Entities.BOOKMARK_FOLDERS],
(params: CreateBookmarkFolderParams) =>
client.myAccount.createBookmarkFolder(params),
);
return {
createBookmarkFolder: createEntity,
...rest,
};
return useMutation({
mutationKey: ['bookmarkFolders', 'create'],
mutationFn: (params: CreateBookmarkFolderParams) => client.myAccount.createBookmarkFolder(params),
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
});
};
export { useCreateBookmarkFolder };

View file

@ -1,19 +1,16 @@
import { Entities } from 'pl-fe/entity-store/entities';
import { useDeleteEntity } from 'pl-fe/entity-store/hooks/use-delete-entity';
import { useMutation } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks/use-client';
import { queryClient } from 'pl-fe/queries/client';
const useDeleteBookmarkFolder = () => {
const client = useClient();
const { deleteEntity, isSubmitting } = useDeleteEntity(
Entities.BOOKMARK_FOLDERS,
(folderId: string) => client.myAccount.deleteBookmarkFolder(folderId),
);
return {
deleteBookmarkFolder: deleteEntity,
isSubmitting,
};
return useMutation({
mutationKey: ['bookmarkFolders', 'delete'],
mutationFn: (folderId: string) => client.myAccount.deleteBookmarkFolder(folderId),
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
});
};
export { useDeleteBookmarkFolder };

View file

@ -1,6 +1,7 @@
import { Entities } from 'pl-fe/entity-store/entities';
import { useCreateEntity } from 'pl-fe/entity-store/hooks/use-create-entity';
import { useMutation } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks/use-client';
import { queryClient } from 'pl-fe/queries/client';
interface UpdateBookmarkFolderParams {
name: string;
@ -10,16 +11,11 @@ interface UpdateBookmarkFolderParams {
const useUpdateBookmarkFolder = (folderId: string) => {
const client = useClient();
const { createEntity, ...rest } = useCreateEntity(
[Entities.BOOKMARK_FOLDERS],
(params: UpdateBookmarkFolderParams) =>
client.myAccount.updateBookmarkFolder(folderId, params),
);
return {
updateBookmarkFolder: createEntity,
...rest,
};
return useMutation({
mutationKey: ['bookmarkFolders', 'update', folderId],
mutationFn: (params: UpdateBookmarkFolderParams) => client.myAccount.updateBookmarkFolder(folderId, params),
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
});
};
export { useUpdateBookmarkFolder };

View file

@ -20,7 +20,7 @@ const NewFolderForm: React.FC = () => {
const name = useTextField();
const { createBookmarkFolder, isSubmitting } = useCreateBookmarkFolder();
const { mutate: createBookmarkFolder, isPending } = useCreateBookmarkFolder();
const handleSubmit = (e: React.FormEvent<Element>) => {
e.preventDefault();
@ -47,13 +47,13 @@ const NewFolderForm: React.FC = () => {
<Input
type='text'
placeholder={label}
disabled={isSubmitting}
disabled={isPending}
{...name}
/>
</label>
<Button
disabled={isSubmitting}
disabled={isPending}
onClick={handleSubmit}
theme='primary'
>

View file

@ -22,7 +22,7 @@ const BookmarkFolders: React.FC = () => {
const intl = useIntl();
const features = useFeatures();
const { bookmarkFolders, isFetching } = useBookmarkFolders();
const { data: bookmarkFolders, isFetching } = useBookmarkFolders(data => data);
if (!features.bookmarkFolders) return <Redirect to='/bookmarks/all' />;

View file

@ -49,8 +49,8 @@ const Bookmarks: React.FC<IBookmarks> = ({ params }) => {
const folderId = params?.id;
const { openModal } = useModalsStore();
const { bookmarkFolder: folder } = useBookmarkFolder(folderId);
const { deleteBookmarkFolder } = useDeleteBookmarkFolder();
const { data: folder } = useBookmarkFolder(folderId);
const { mutate: deleteBookmarkFolder } = useDeleteBookmarkFolder();
const bookmarksKey = folderId ? `bookmarks:${folderId}` : 'bookmarks';

View file

@ -98,8 +98,8 @@ interface EditBookmarkFolderModalProps {
const EditBookmarkFolderModal: React.FC<BaseModalProps & EditBookmarkFolderModalProps> = ({ folderId, onClose }) => {
const intl = useIntl();
const { bookmarkFolder } = useBookmarkFolder(folderId);
const { updateBookmarkFolder, isSubmitting } = useUpdateBookmarkFolder(folderId);
const { data: bookmarkFolder } = useBookmarkFolder(folderId);
const { mutate: updateBookmarkFolder, isPending } = useUpdateBookmarkFolder(folderId);
const [emoji, setEmoji] = useState(bookmarkFolder?.emoji || undefined);
const [emojiUrl, setEmojiUrl] = useState(bookmarkFolder?.emoji_url || undefined);
@ -154,7 +154,7 @@ const EditBookmarkFolderModal: React.FC<BaseModalProps & EditBookmarkFolderModal
<Input
type='text'
placeholder={label}
disabled={isSubmitting}
disabled={isPending}
{...name}
/>
</label>

View file

@ -28,7 +28,7 @@ const SelectBookmarkFolderModal: React.FC<SelectBookmarkFolderModalProps & BaseM
const [selectedFolder, setSelectedFolder] = useState(status.bookmark_folder);
const { isFetching, bookmarkFolders } = useBookmarkFolders();
const { isFetching, data: bookmarkFolders } = useBookmarkFolders(data => data);
const onChange: React.ChangeEventHandler<HTMLInputElement> = e => {
const folderId = e.target.value;
@ -58,7 +58,7 @@ const SelectBookmarkFolderModal: React.FC<SelectBookmarkFolderModalProps & BaseM
];
if (!isFetching) {
items.push(...(bookmarkFolders.map((folder) => (
items.push(...((bookmarkFolders || []).map((folder) => (
<RadioItem
key={folder.id}
label={

View file

@ -1,4 +1,4 @@
import type{ RecursiveKeyValuePair } from 'tailwindcss/types/config';
import type { RecursiveKeyValuePair } from 'tailwindcss/types/config';
/** https://tailwindcss.com/docs/customizing-colors#using-css-variables */
const withOpacityValue = (variable: string): string => `rgb(var(${variable}) / <alpha-value>)`;