mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-09 20:29:36 +02:00
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { AxiosError } from 'axios';
|
|
|
|
import { api } from '/@/renderer/api';
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
|
import { RMutationHookArgs } from '/@/renderer/lib/react-query';
|
|
import { useServerById } from '/@/renderer/store';
|
|
import {
|
|
AddToPlaylistArgs,
|
|
AddToPlaylistResponse,
|
|
} from '/@/shared/types/domain/playlist-domain-types';
|
|
|
|
export const useAddToPlaylist = (args: RMutationHookArgs) => {
|
|
const { options } = args || {};
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation<
|
|
AddToPlaylistResponse,
|
|
AxiosError,
|
|
Omit<AddToPlaylistArgs, 'apiClientProps' | 'server'>,
|
|
null
|
|
>({
|
|
mutationFn: (args) => {
|
|
const server = useServerById(args.serverId);
|
|
if (!server) throw new Error('Server not found');
|
|
return api.controller.addToPlaylist({ ...args, apiClientProps: { server } });
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
const { serverId } = variables;
|
|
|
|
if (!serverId) return;
|
|
|
|
queryClient.invalidateQueries(queryKeys.playlists.list(serverId), { exact: false });
|
|
queryClient.invalidateQueries(queryKeys.playlists.detail(serverId, variables.query.id));
|
|
queryClient.invalidateQueries(
|
|
queryKeys.playlists.detailSongList(serverId, variables.query.id),
|
|
);
|
|
queryClient.invalidateQueries(
|
|
queryKeys.playlists.songList(serverId, variables.query.id),
|
|
);
|
|
},
|
|
...options,
|
|
});
|
|
};
|