2023-05-08 03:35:51 -07:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
2025-05-18 14:03:18 -07:00
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
|
|
|
|
|
|
import { queryKeys } from '../../../api/query-keys';
|
|
|
|
|
|
2022-12-31 12:40:11 -08:00
|
|
|
import { api } from '/@/renderer/api';
|
2023-04-27 21:44:25 -07:00
|
|
|
import { CreatePlaylistArgs, CreatePlaylistResponse } from '/@/renderer/api/types';
|
|
|
|
|
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
|
|
|
|
import { getServerById } from '/@/renderer/store';
|
2022-12-31 12:40:11 -08:00
|
|
|
|
2023-04-27 21:44:25 -07:00
|
|
|
export const useCreatePlaylist = (args: MutationHookArgs) => {
|
2023-07-01 19:10:05 -07:00
|
|
|
const { options } = args || {};
|
|
|
|
|
const queryClient = useQueryClient();
|
2022-12-31 12:40:11 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return useMutation<
|
|
|
|
|
CreatePlaylistResponse,
|
|
|
|
|
AxiosError,
|
2025-05-18 14:03:18 -07:00
|
|
|
Omit<CreatePlaylistArgs, 'apiClientProps' | 'server'>,
|
2023-07-01 19:10:05 -07:00
|
|
|
null
|
|
|
|
|
>({
|
|
|
|
|
mutationFn: (args) => {
|
|
|
|
|
const server = getServerById(args.serverId);
|
|
|
|
|
if (!server) throw new Error('Server not found');
|
|
|
|
|
return api.controller.createPlaylist({ ...args, apiClientProps: { server } });
|
|
|
|
|
},
|
|
|
|
|
onSuccess: (_args, variables) => {
|
|
|
|
|
const server = getServerById(variables.serverId);
|
|
|
|
|
if (server) {
|
|
|
|
|
queryClient.invalidateQueries(queryKeys.playlists.list(server.id));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
...options,
|
|
|
|
|
});
|
2022-12-31 12:40:11 -08:00
|
|
|
};
|