feat: favorite & watch lists
This commit is contained in:
1 parent
960a7a6242
commit
8858e47d38
51 files changed
+561
-290
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import { accountStates, updateAccountStates } from '@/api';
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists';
|
||||
import { useSession } from './useSession';
|
||||
|
||||
import { KEY as USERS_SHOWS_KEY } from './useUsersShows';
|
||||
|
||||
const KEY = ['show-state'];
|
||||
|
||||
const useShowState = ({ showId, showType = 'movie' }: { showId?: Show['id']; showType?: Show['type'] }) => {
|
||||
const session = useSession();
|
||||
|
||||
const { map: favorites } = useFavorites();
|
||||
const { map: watchList } = useWatchList();
|
||||
|
||||
const query = useQuery({
|
||||
enabled: Boolean(session && showId && showType),
|
||||
queryKey: KEY.concat(showId!.toString(), showType),
|
||||
queryFn: () => accountStates({ showId: showId!, showType }),
|
||||
notifyOnChangeProps: ['data'],
|
||||
staleTime: Infinity
|
||||
});
|
||||
|
||||
return session ? query.data : { id: showId, watchlist: watchList.has(showId!), favorite: favorites.has(showId!) };
|
||||
};
|
||||
|
||||
export const useMutateShowState = (show: Show) => {
|
||||
const session = useSession();
|
||||
|
||||
const { toggle: toggleFavorite } = useFavoritesStateToggle(show);
|
||||
const { toggle: toggleWatchList } = useWatchListStateToggle(show);
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: updateAccountStates,
|
||||
onMutate({ showId, showType, list, value }) {
|
||||
const key = KEY.concat(showId.toString(), showType);
|
||||
|
||||
const snapshot = queryClient.getQueryData<ShowState>(key);
|
||||
|
||||
queryClient.setQueryData<ShowState>(key, (state) => ({ ...state, [list]: value }) as ShowState);
|
||||
|
||||
return snapshot;
|
||||
},
|
||||
onError(_, { showId, showType }, snapshot) {
|
||||
const key = KEY.concat(showId.toString(), showType);
|
||||
|
||||
queryClient.setQueryData<ShowState>(key, snapshot);
|
||||
},
|
||||
onSuccess(_, { list, showType }) {
|
||||
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType }) });
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
list: mutation.variables?.list,
|
||||
isPending: session ? mutation.isPending : false,
|
||||
mutate: ({ list, value }: { list: 'favorite' | 'watchlist'; value: boolean }) => {
|
||||
if (session) return mutation.mutate({ showId: show.id, showType: show.type, list, value });
|
||||
|
||||
if (list === 'favorite') return toggleFavorite();
|
||||
|
||||
return toggleWatchList();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default useShowState;
|
||||
Reference in new issue
Block a user