pl-fe: Migrate bookmark folders to tanstack query
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
42f7226594
commit
1d76c05844
11 changed files with 48 additions and 90 deletions
|
@ -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 { useBookmarkFolders } from './use-bookmark-folders';
|
||||||
|
|
||||||
import type{ BookmarkFolder } from 'pl-api';
|
const useBookmarkFolder = (folderId?: string) => useBookmarkFolders((data) => folderId ? data.find(folder => folder.id === folderId) : undefined);
|
||||||
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export { useBookmarkFolder };
|
export { useBookmarkFolder };
|
||||||
|
|
|
@ -1,26 +1,22 @@
|
||||||
import { Entities } from 'pl-fe/entity-store/entities';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { useEntities } from 'pl-fe/entity-store/hooks/use-entities';
|
|
||||||
import { useClient } from 'pl-fe/hooks/use-client';
|
import { useClient } from 'pl-fe/hooks/use-client';
|
||||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||||
|
|
||||||
import type { BookmarkFolder } from 'pl-api';
|
import type { BookmarkFolder } from 'pl-api';
|
||||||
|
|
||||||
const useBookmarkFolders = () => {
|
const useBookmarkFolders = <T>(
|
||||||
|
select?: ((data: Array<BookmarkFolder>) => T),
|
||||||
|
) => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
const features = useFeatures();
|
const features = useFeatures();
|
||||||
|
|
||||||
const { entities, ...result } = useEntities<BookmarkFolder>(
|
return useQuery({
|
||||||
[Entities.BOOKMARK_FOLDERS],
|
queryKey: ['bookmarkFolders'],
|
||||||
() => client.myAccount.getBookmarkFolders(),
|
queryFn: () => client.myAccount.getBookmarkFolders(),
|
||||||
{ enabled: features.bookmarkFolders },
|
enabled: features.bookmarkFolders,
|
||||||
);
|
select,
|
||||||
|
});
|
||||||
const bookmarkFolders = entities;
|
|
||||||
|
|
||||||
return {
|
|
||||||
...result,
|
|
||||||
bookmarkFolders,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useBookmarkFolders };
|
export { useBookmarkFolders };
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Entities } from 'pl-fe/entity-store/entities';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { useCreateEntity } from 'pl-fe/entity-store/hooks/use-create-entity';
|
|
||||||
import { useClient } from 'pl-fe/hooks/use-client';
|
import { useClient } from 'pl-fe/hooks/use-client';
|
||||||
|
import { queryClient } from 'pl-fe/queries/client';
|
||||||
|
|
||||||
interface CreateBookmarkFolderParams {
|
interface CreateBookmarkFolderParams {
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -10,16 +11,11 @@ interface CreateBookmarkFolderParams {
|
||||||
const useCreateBookmarkFolder = () => {
|
const useCreateBookmarkFolder = () => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
|
|
||||||
const { createEntity, ...rest } = useCreateEntity(
|
return useMutation({
|
||||||
[Entities.BOOKMARK_FOLDERS],
|
mutationKey: ['bookmarkFolders', 'create'],
|
||||||
(params: CreateBookmarkFolderParams) =>
|
mutationFn: (params: CreateBookmarkFolderParams) => client.myAccount.createBookmarkFolder(params),
|
||||||
client.myAccount.createBookmarkFolder(params),
|
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
|
||||||
);
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
createBookmarkFolder: createEntity,
|
|
||||||
...rest,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useCreateBookmarkFolder };
|
export { useCreateBookmarkFolder };
|
||||||
|
|
|
@ -1,19 +1,16 @@
|
||||||
import { Entities } from 'pl-fe/entity-store/entities';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { useDeleteEntity } from 'pl-fe/entity-store/hooks/use-delete-entity';
|
|
||||||
import { useClient } from 'pl-fe/hooks/use-client';
|
import { useClient } from 'pl-fe/hooks/use-client';
|
||||||
|
import { queryClient } from 'pl-fe/queries/client';
|
||||||
|
|
||||||
const useDeleteBookmarkFolder = () => {
|
const useDeleteBookmarkFolder = () => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
|
|
||||||
const { deleteEntity, isSubmitting } = useDeleteEntity(
|
return useMutation({
|
||||||
Entities.BOOKMARK_FOLDERS,
|
mutationKey: ['bookmarkFolders', 'delete'],
|
||||||
(folderId: string) => client.myAccount.deleteBookmarkFolder(folderId),
|
mutationFn: (folderId: string) => client.myAccount.deleteBookmarkFolder(folderId),
|
||||||
);
|
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
|
||||||
|
});
|
||||||
return {
|
|
||||||
deleteBookmarkFolder: deleteEntity,
|
|
||||||
isSubmitting,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useDeleteBookmarkFolder };
|
export { useDeleteBookmarkFolder };
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Entities } from 'pl-fe/entity-store/entities';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { useCreateEntity } from 'pl-fe/entity-store/hooks/use-create-entity';
|
|
||||||
import { useClient } from 'pl-fe/hooks/use-client';
|
import { useClient } from 'pl-fe/hooks/use-client';
|
||||||
|
import { queryClient } from 'pl-fe/queries/client';
|
||||||
|
|
||||||
interface UpdateBookmarkFolderParams {
|
interface UpdateBookmarkFolderParams {
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -10,16 +11,11 @@ interface UpdateBookmarkFolderParams {
|
||||||
const useUpdateBookmarkFolder = (folderId: string) => {
|
const useUpdateBookmarkFolder = (folderId: string) => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
|
|
||||||
const { createEntity, ...rest } = useCreateEntity(
|
return useMutation({
|
||||||
[Entities.BOOKMARK_FOLDERS],
|
mutationKey: ['bookmarkFolders', 'update', folderId],
|
||||||
(params: UpdateBookmarkFolderParams) =>
|
mutationFn: (params: UpdateBookmarkFolderParams) => client.myAccount.updateBookmarkFolder(folderId, params),
|
||||||
client.myAccount.updateBookmarkFolder(folderId, params),
|
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
|
||||||
);
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
updateBookmarkFolder: createEntity,
|
|
||||||
...rest,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useUpdateBookmarkFolder };
|
export { useUpdateBookmarkFolder };
|
||||||
|
|
|
@ -20,7 +20,7 @@ const NewFolderForm: React.FC = () => {
|
||||||
|
|
||||||
const name = useTextField();
|
const name = useTextField();
|
||||||
|
|
||||||
const { createBookmarkFolder, isSubmitting } = useCreateBookmarkFolder();
|
const { mutate: createBookmarkFolder, isPending } = useCreateBookmarkFolder();
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent<Element>) => {
|
const handleSubmit = (e: React.FormEvent<Element>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
@ -47,13 +47,13 @@ const NewFolderForm: React.FC = () => {
|
||||||
<Input
|
<Input
|
||||||
type='text'
|
type='text'
|
||||||
placeholder={label}
|
placeholder={label}
|
||||||
disabled={isSubmitting}
|
disabled={isPending}
|
||||||
{...name}
|
{...name}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
disabled={isSubmitting}
|
disabled={isPending}
|
||||||
onClick={handleSubmit}
|
onClick={handleSubmit}
|
||||||
theme='primary'
|
theme='primary'
|
||||||
>
|
>
|
||||||
|
|
|
@ -22,7 +22,7 @@ const BookmarkFolders: React.FC = () => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const features = useFeatures();
|
const features = useFeatures();
|
||||||
|
|
||||||
const { bookmarkFolders, isFetching } = useBookmarkFolders();
|
const { data: bookmarkFolders, isFetching } = useBookmarkFolders(data => data);
|
||||||
|
|
||||||
if (!features.bookmarkFolders) return <Redirect to='/bookmarks/all' />;
|
if (!features.bookmarkFolders) return <Redirect to='/bookmarks/all' />;
|
||||||
|
|
||||||
|
|
|
@ -49,8 +49,8 @@ const Bookmarks: React.FC<IBookmarks> = ({ params }) => {
|
||||||
const folderId = params?.id;
|
const folderId = params?.id;
|
||||||
|
|
||||||
const { openModal } = useModalsStore();
|
const { openModal } = useModalsStore();
|
||||||
const { bookmarkFolder: folder } = useBookmarkFolder(folderId);
|
const { data: folder } = useBookmarkFolder(folderId);
|
||||||
const { deleteBookmarkFolder } = useDeleteBookmarkFolder();
|
const { mutate: deleteBookmarkFolder } = useDeleteBookmarkFolder();
|
||||||
|
|
||||||
const bookmarksKey = folderId ? `bookmarks:${folderId}` : 'bookmarks';
|
const bookmarksKey = folderId ? `bookmarks:${folderId}` : 'bookmarks';
|
||||||
|
|
||||||
|
|
|
@ -98,8 +98,8 @@ interface EditBookmarkFolderModalProps {
|
||||||
const EditBookmarkFolderModal: React.FC<BaseModalProps & EditBookmarkFolderModalProps> = ({ folderId, onClose }) => {
|
const EditBookmarkFolderModal: React.FC<BaseModalProps & EditBookmarkFolderModalProps> = ({ folderId, onClose }) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
const { bookmarkFolder } = useBookmarkFolder(folderId);
|
const { data: bookmarkFolder } = useBookmarkFolder(folderId);
|
||||||
const { updateBookmarkFolder, isSubmitting } = useUpdateBookmarkFolder(folderId);
|
const { mutate: updateBookmarkFolder, isPending } = useUpdateBookmarkFolder(folderId);
|
||||||
|
|
||||||
const [emoji, setEmoji] = useState(bookmarkFolder?.emoji || undefined);
|
const [emoji, setEmoji] = useState(bookmarkFolder?.emoji || undefined);
|
||||||
const [emojiUrl, setEmojiUrl] = useState(bookmarkFolder?.emoji_url || undefined);
|
const [emojiUrl, setEmojiUrl] = useState(bookmarkFolder?.emoji_url || undefined);
|
||||||
|
@ -154,7 +154,7 @@ const EditBookmarkFolderModal: React.FC<BaseModalProps & EditBookmarkFolderModal
|
||||||
<Input
|
<Input
|
||||||
type='text'
|
type='text'
|
||||||
placeholder={label}
|
placeholder={label}
|
||||||
disabled={isSubmitting}
|
disabled={isPending}
|
||||||
{...name}
|
{...name}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
|
|
@ -28,7 +28,7 @@ const SelectBookmarkFolderModal: React.FC<SelectBookmarkFolderModalProps & BaseM
|
||||||
|
|
||||||
const [selectedFolder, setSelectedFolder] = useState(status.bookmark_folder);
|
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 onChange: React.ChangeEventHandler<HTMLInputElement> = e => {
|
||||||
const folderId = e.target.value;
|
const folderId = e.target.value;
|
||||||
|
@ -58,7 +58,7 @@ const SelectBookmarkFolderModal: React.FC<SelectBookmarkFolderModalProps & BaseM
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!isFetching) {
|
if (!isFetching) {
|
||||||
items.push(...(bookmarkFolders.map((folder) => (
|
items.push(...((bookmarkFolders || []).map((folder) => (
|
||||||
<RadioItem
|
<RadioItem
|
||||||
key={folder.id}
|
key={folder.id}
|
||||||
label={
|
label={
|
||||||
|
|
|
@ -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 */
|
/** https://tailwindcss.com/docs/customizing-colors#using-css-variables */
|
||||||
const withOpacityValue = (variable: string): string => `rgb(var(${variable}) / <alpha-value>)`;
|
const withOpacityValue = (variable: string): string => `rgb(var(${variable}) / <alpha-value>)`;
|
||||||
|
|
Loading…
Reference in a new issue