refactoring: server actions data fetching -> api routes
This commit is contained in:
1 parent
1111712d2c
commit
5cbf93b3b8
38 files changed
+326
-308
No files matched your search
+6
-12
@@ -1,25 +1,19 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { account } from '@/api';
|
||||
import { useSession } from '@/hooks/useSession';
|
||||
|
||||
const KEY = ['account'];
|
||||
import accountQuery from '@/api/queries/account';
|
||||
|
||||
const useAccount = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const session = useSession();
|
||||
|
||||
const query = useQuery({
|
||||
enabled: Boolean(session),
|
||||
staleTime: Infinity,
|
||||
notifyOnChangeProps: ['data'],
|
||||
queryKey: KEY,
|
||||
queryFn: () => account()
|
||||
});
|
||||
const options = accountQuery({ session });
|
||||
|
||||
const query = useQuery(options);
|
||||
|
||||
if (session === null) {
|
||||
queryClient.removeQueries({ queryKey: KEY });
|
||||
queryClient.resetQueries({ queryKey: KEY });
|
||||
queryClient.removeQueries({ queryKey: options.queryKey });
|
||||
queryClient.resetQueries({ queryKey: options.queryKey });
|
||||
}
|
||||
|
||||
return session ? query.data : undefined;
|
||||
|
||||
@@ -5,7 +5,7 @@ interface Props {
|
||||
suspense?: boolean;
|
||||
queryKey: QueryKey;
|
||||
enabled?: boolean;
|
||||
queryFn: ({ page }: { page: string }) => Promise<Pagination<Show>>;
|
||||
queryFn: ({ pageParam }: { pageParam: string }) => Promise<Pagination<Show>>;
|
||||
}
|
||||
|
||||
const useInfiniteList = ({ queryKey, queryFn, enabled, suspense }: Props) => {
|
||||
@@ -14,7 +14,7 @@ const useInfiniteList = ({ queryKey, queryFn, enabled, suspense }: Props) => {
|
||||
)({
|
||||
enabled,
|
||||
queryKey,
|
||||
queryFn: ({ pageParam }) => queryFn({ page: pageParam }),
|
||||
queryFn: ({ pageParam }) => queryFn({ pageParam }),
|
||||
getNextPageParam(pagination) {
|
||||
if (!pagination || pagination.page === pagination.total) return undefined;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { accountStates, updateAccountStates } from '@/api';
|
||||
|
||||
import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists';
|
||||
import { useSession } from './useSession';
|
||||
import { KEY as USERS_SHOWS_KEY } from './useUsersShows';
|
||||
import { KEY as USERS_SHOWS_KEY } from '@/api/queries/usersShows';
|
||||
|
||||
const KEY = ['show-state'];
|
||||
|
||||
@@ -50,7 +50,7 @@ export const useMutateShowState = (show: Show) => {
|
||||
queryClient.setQueryData<ShowState>(key, snapshot);
|
||||
},
|
||||
onSuccess(_, { list, showType }) {
|
||||
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType }), refetchType: 'all' });
|
||||
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType, session }), refetchType: 'all' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+2
-11
@@ -1,15 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { usersShows } from '@/api';
|
||||
|
||||
import useInfiniteList from './useInfiniteList';
|
||||
import { useFavorites, useWatchList } from './useLocalUsersLists';
|
||||
import { useSession } from './useSession';
|
||||
|
||||
export const KEY = ({ list, showType }: { showType: Show['type']; list: 'favorite' | 'watchlist' }) => [
|
||||
`users-${list}`,
|
||||
showType
|
||||
];
|
||||
import usersShowsQuery from '@/api/queries/usersShows';
|
||||
|
||||
const useUsersShows = ({ showType, list = 'favorite' }: { showType: Show['type']; list: 'favorite' | 'watchlist' }) => {
|
||||
const session = useSession();
|
||||
@@ -17,10 +11,7 @@ const useUsersShows = ({ showType, list = 'favorite' }: { showType: Show['type']
|
||||
const { shows: favorite } = useFavorites();
|
||||
const { shows: watchlist } = useWatchList();
|
||||
|
||||
const query = useInfiniteList({
|
||||
queryKey: KEY({ list, showType }),
|
||||
queryFn: ({ page }) => usersShows({ showType, list, page })
|
||||
});
|
||||
const query = useInfiniteList(usersShowsQuery({ showType, list, session }));
|
||||
|
||||
return session
|
||||
? query
|
||||
|
||||
Reference in new issue
Block a user