2023-01-07 18:23:10 -08:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
import { HTTPError } from 'ky';
|
|
|
|
|
import { api } from '/@/renderer/api';
|
2023-01-08 00:52:53 -08:00
|
|
|
import { FavoriteArgs, LibraryItem, RawFavoriteResponse } from '/@/renderer/api/types';
|
2023-01-07 18:23:10 -08:00
|
|
|
import { MutationOptions } from '/@/renderer/lib/react-query';
|
2023-01-08 00:52:53 -08:00
|
|
|
import { useCurrentServer, useSetAlbumListItemDataById } from '/@/renderer/store';
|
2023-01-07 18:23:10 -08:00
|
|
|
|
|
|
|
|
export const useCreateFavorite = (options?: MutationOptions) => {
|
|
|
|
|
const server = useCurrentServer();
|
2023-01-08 00:52:53 -08:00
|
|
|
const setAlbumListData = useSetAlbumListItemDataById();
|
2023-01-07 18:23:10 -08:00
|
|
|
|
|
|
|
|
return useMutation<RawFavoriteResponse, HTTPError, Omit<FavoriteArgs, 'server'>, null>({
|
|
|
|
|
mutationFn: (args) => api.controller.createFavorite({ ...args, server }),
|
2023-01-08 00:52:53 -08:00
|
|
|
onSuccess: (_data, variables) => {
|
|
|
|
|
for (const id of variables.query.id) {
|
|
|
|
|
// Set the userFavorite property to true for the album in the album list data store
|
|
|
|
|
if (variables.query.type === LibraryItem.ALBUM) {
|
|
|
|
|
setAlbumListData(id, { userFavorite: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-01-07 18:23:10 -08:00
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
};
|