@ -7,10 +7,12 @@ import { queryClient } from 'pl-fe/queries/client';
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/queries/utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from 'pl-fe/queries/utils/minify-list';
|
||||
|
||||
import { filterById } from '../utils/filter-id';
|
||||
|
||||
import type { PaginatedResponse, PlApiClient } from 'pl-api';
|
||||
|
||||
const appendFollowRequest = (accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(['accountsLists', 'followRequests'], (data) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(['accountsLists', 'followRequests'], (data) => {
|
||||
if (!data || data.pages.some(page => page.items.includes(accountId))) return data;
|
||||
|
||||
return {
|
||||
@ -20,10 +22,7 @@ const appendFollowRequest = (accountId: string) =>
|
||||
});
|
||||
|
||||
const removeFollowRequest = (accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(['accountsLists', 'followRequests'], (data) => data ? {
|
||||
...data,
|
||||
pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== accountId) })),
|
||||
} : undefined);
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(['accountsLists', 'followRequests'], filterById(accountId));
|
||||
|
||||
const makeUseFollowRequests = <T>(select: ((data: InfiniteData<PaginatedResponse<string>>) => T)) => makePaginatedResponseQuery(
|
||||
() => ['accountsLists', 'followRequests'],
|
||||
|
||||
@ -5,6 +5,8 @@ import { queryClient } from 'pl-fe/queries/client';
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/queries/utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from 'pl-fe/queries/utils/minify-list';
|
||||
|
||||
import { filterById } from '../utils/filter-id';
|
||||
|
||||
import { removeGroupMember } from './use-group-members';
|
||||
|
||||
const appendGroupBlock = (groupId: string, accountId: string) =>
|
||||
@ -18,10 +20,7 @@ const appendGroupBlock = (groupId: string, accountId: string) =>
|
||||
});
|
||||
|
||||
const removeGroupBlock = (groupId: string, accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(['accountsLists', 'groupBlocks', groupId], (data) => data ? {
|
||||
...data,
|
||||
pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== accountId) })),
|
||||
} : undefined);
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(['accountsLists', 'groupBlocks', groupId], filterById(accountId));
|
||||
|
||||
const useGroupBlocks = makePaginatedResponseQuery(
|
||||
(groupId: string) => ['accountsLists', 'groupBlocks', groupId],
|
||||
|
||||
@ -11,6 +11,8 @@ import { minifyAccountList } from 'pl-fe/queries/utils/minify-list';
|
||||
import { useModalsStore } from 'pl-fe/stores/modals';
|
||||
import toast, { IToastOptions } from 'pl-fe/toast';
|
||||
|
||||
import { filterById } from '../utils/filter-id';
|
||||
|
||||
import type { PaginatedResponse } from 'pl-api';
|
||||
|
||||
const messages = defineMessages({
|
||||
@ -180,10 +182,8 @@ const useBookmarkStatus = (statusId: string) => {
|
||||
if (previousFolder) {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(
|
||||
['statusLists', 'bookmarks', previousFolder],
|
||||
(data) => data ? {
|
||||
...data,
|
||||
pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== statusId) })),
|
||||
} : undefined);
|
||||
filterById(statusId),
|
||||
);
|
||||
}
|
||||
|
||||
if (!previouslyBookmarked) {
|
||||
@ -227,10 +227,7 @@ const useUnbookmarkStatus = (statusId: string) => {
|
||||
queryClient.setQueriesData<InfiniteData<PaginatedResponse<string>>>({
|
||||
queryKey: ['statusLists', 'bookmarks'],
|
||||
exact: false,
|
||||
}, (data) => data ? {
|
||||
...data,
|
||||
pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== statusId) })),
|
||||
} : undefined);
|
||||
}, filterById(statusId));
|
||||
|
||||
toast.success(messages.bookmarkRemoved);
|
||||
},
|
||||
|
||||
@ -1,20 +1,23 @@
|
||||
import { create } from 'mutative';
|
||||
|
||||
import type { InfiniteData } from '@tanstack/react-query';
|
||||
import type { PaginatedResponse } from 'pl-api';
|
||||
|
||||
const filterById = (filteredId: string) => (data: InfiniteData<PaginatedResponse<string, true>, unknown> | undefined) => {
|
||||
if (data) {
|
||||
let found = false;
|
||||
let pages = data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => {
|
||||
if (id === filteredId) found = true;
|
||||
return id !== filteredId;
|
||||
}) }));
|
||||
return create((data), data => {
|
||||
let found = false;
|
||||
data.pages.forEach((page) => {
|
||||
page.items = page.items.filter((id) => {
|
||||
if (id === filteredId) found = true;
|
||||
return id !== filteredId;
|
||||
});
|
||||
});
|
||||
|
||||
if (found) pages = pages.map(({ total, ...page }) => ({ total: total ? total - 1 : total, ...page }));
|
||||
|
||||
return {
|
||||
...data,
|
||||
pages,
|
||||
};
|
||||
if (found) data.pages.forEach((page) => {
|
||||
if (page.total) page.total -= 1;
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user