2022-12-31 12:40:11 -08:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
import { HTTPError } from 'ky';
|
|
|
|
|
import { api } from '/@/renderer/api';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
2023-04-27 21:44:25 -07:00
|
|
|
import { UpdatePlaylistArgs, UpdatePlaylistResponse } 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 useUpdatePlaylist = (args: MutationHookArgs) => {
|
|
|
|
|
const { options } = args || {};
|
2022-12-31 12:40:11 -08:00
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
2023-04-30 18:00:50 -07:00
|
|
|
return useMutation<
|
|
|
|
|
UpdatePlaylistResponse,
|
|
|
|
|
HTTPError,
|
|
|
|
|
Omit<UpdatePlaylistArgs, 'server' | 'apiClientProps'>,
|
|
|
|
|
null
|
|
|
|
|
>({
|
2023-04-27 21:44:25 -07:00
|
|
|
mutationFn: (args) => {
|
|
|
|
|
const server = getServerById(args.serverId);
|
|
|
|
|
if (!server) throw new Error('Server not found');
|
|
|
|
|
return api.controller.updatePlaylist({ ...args, apiClientProps: { server } });
|
|
|
|
|
},
|
|
|
|
|
onSuccess: (_data, variables) => {
|
|
|
|
|
const { query, serverId } = variables;
|
2023-04-30 18:00:50 -07:00
|
|
|
|
|
|
|
|
if (!serverId) return;
|
|
|
|
|
|
2023-04-27 21:44:25 -07:00
|
|
|
queryClient.invalidateQueries(queryKeys.playlists.list(serverId));
|
2022-12-31 12:40:11 -08:00
|
|
|
|
2023-04-27 21:44:25 -07:00
|
|
|
if (query?.id) {
|
|
|
|
|
queryClient.invalidateQueries(queryKeys.playlists.detail(serverId, query.id));
|
2022-12-31 12:40:11 -08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
};
|