Fix favorite mutation
This commit is contained in:
parent
8d5c82b0c6
commit
cef92243f5
5 changed files with 123 additions and 59 deletions
|
@ -19,6 +19,7 @@ export const FavoriteCell = ({ value, data, node }: ICellRendererParams) => {
|
||||||
id: [data.id],
|
id: [data.id],
|
||||||
type: data.itemType,
|
type: data.itemType,
|
||||||
},
|
},
|
||||||
|
serverId: data.serverId,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
@ -33,6 +34,7 @@ export const FavoriteCell = ({ value, data, node }: ICellRendererParams) => {
|
||||||
id: [data.id],
|
id: [data.id],
|
||||||
type: data.itemType,
|
type: data.itemType,
|
||||||
},
|
},
|
||||||
|
serverId: data.serverId,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
|
|
@ -241,6 +241,7 @@ export const AlbumDetailContent = ({ tableRef }: AlbumDetailContentProps) => {
|
||||||
id: [detailQuery.data.id],
|
id: [detailQuery.data.id],
|
||||||
type: LibraryItem.ALBUM,
|
type: LibraryItem.ALBUM,
|
||||||
},
|
},
|
||||||
|
serverId: detailQuery.data.serverId,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
createFavoriteMutation.mutate({
|
createFavoriteMutation.mutate({
|
||||||
|
@ -248,6 +249,7 @@ export const AlbumDetailContent = ({ tableRef }: AlbumDetailContentProps) => {
|
||||||
id: [detailQuery.data.id],
|
id: [detailQuery.data.id],
|
||||||
type: LibraryItem.ALBUM,
|
type: LibraryItem.ALBUM,
|
||||||
},
|
},
|
||||||
|
serverId: detailQuery.data.serverId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -296,6 +296,7 @@ export const AlbumListContent = ({ itemCount, gridRef, tableRef }: AlbumListCont
|
||||||
id,
|
id,
|
||||||
type: itemType,
|
type: itemType,
|
||||||
},
|
},
|
||||||
|
serverId: server?.id,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
createFavoriteMutation.mutate({
|
createFavoriteMutation.mutate({
|
||||||
|
@ -303,6 +304,7 @@ export const AlbumListContent = ({ itemCount, gridRef, tableRef }: AlbumListCont
|
||||||
id,
|
id,
|
||||||
type: itemType,
|
type: itemType,
|
||||||
},
|
},
|
||||||
|
serverId: server?.id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -271,6 +271,7 @@ export const AlbumArtistDetailContent = () => {
|
||||||
id: [detailQuery.data.id],
|
id: [detailQuery.data.id],
|
||||||
type: LibraryItem.ALBUM_ARTIST,
|
type: LibraryItem.ALBUM_ARTIST,
|
||||||
},
|
},
|
||||||
|
serverId: detailQuery.data.serverId,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
createFavoriteMutation.mutate({
|
createFavoriteMutation.mutate({
|
||||||
|
@ -278,6 +279,7 @@ export const AlbumArtistDetailContent = () => {
|
||||||
id: [detailQuery.data.id],
|
id: [detailQuery.data.id],
|
||||||
type: LibraryItem.ALBUM_ARTIST,
|
type: LibraryItem.ALBUM_ARTIST,
|
||||||
},
|
},
|
||||||
|
serverId: detailQuery.data.serverId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -25,7 +25,7 @@ import {
|
||||||
RiStarFill,
|
RiStarFill,
|
||||||
RiCloseCircleLine,
|
RiCloseCircleLine,
|
||||||
} from 'react-icons/ri';
|
} from 'react-icons/ri';
|
||||||
import { AnyLibraryItems, LibraryItem, ServerType } from '/@/renderer/api/types';
|
import { AnyLibraryItems, LibraryItem, ServerType, AnyLibraryItem } from '/@/renderer/api/types';
|
||||||
import {
|
import {
|
||||||
ConfirmModal,
|
ConfirmModal,
|
||||||
ContextMenu,
|
ContextMenu,
|
||||||
|
@ -241,25 +241,28 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => {
|
||||||
const handleAddToFavorites = useCallback(() => {
|
const handleAddToFavorites = useCallback(() => {
|
||||||
if (!ctx.dataNodes && !ctx.data) return;
|
if (!ctx.dataNodes && !ctx.data) return;
|
||||||
|
|
||||||
let itemsToFavorite: AnyLibraryItems = [];
|
|
||||||
let nodesToFavorite: RowNode<any>[] = [];
|
|
||||||
|
|
||||||
if (ctx.dataNodes) {
|
if (ctx.dataNodes) {
|
||||||
nodesToFavorite = ctx.dataNodes.filter((item) => !item.data.userFavorite);
|
const nodesToFavorite = ctx.dataNodes.filter((item) => !item.data.userFavorite);
|
||||||
} else {
|
|
||||||
itemsToFavorite = ctx.data.filter((item) => !item.userFavorite);
|
|
||||||
}
|
|
||||||
|
|
||||||
const idsToFavorite = nodesToFavorite
|
const nodesByServerId = nodesToFavorite.reduce((acc, node) => {
|
||||||
? nodesToFavorite.map((node) => node.data.id)
|
if (!acc[node.data.serverId]) {
|
||||||
: itemsToFavorite.map((item) => item.id);
|
acc[node.data.serverId] = [];
|
||||||
|
}
|
||||||
|
acc[node.data.serverId].push(node);
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, RowNode<any>[]>);
|
||||||
|
|
||||||
|
for (const serverId of Object.keys(nodesByServerId)) {
|
||||||
|
const nodes = nodesByServerId[serverId];
|
||||||
|
const items = nodes.map((node) => node.data);
|
||||||
|
|
||||||
createFavoriteMutation.mutate(
|
createFavoriteMutation.mutate(
|
||||||
{
|
{
|
||||||
query: {
|
query: {
|
||||||
id: idsToFavorite,
|
id: items.map((item) => item.id),
|
||||||
type: ctx.type,
|
type: ctx.type,
|
||||||
},
|
},
|
||||||
|
serverId,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onError: (err) => {
|
onError: (err) => {
|
||||||
|
@ -269,38 +272,69 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
if (ctx.dataNodes) {
|
for (const node of nodes) {
|
||||||
for (const node of nodesToFavorite) {
|
|
||||||
node.setData({ ...node.data, userFavorite: true });
|
node.setData({ ...node.data, userFavorite: true });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const itemsToFavorite = ctx.data.filter((item) => !item.userFavorite);
|
||||||
|
const itemsByServerId = (itemsToFavorite as any[]).reduce((acc, item) => {
|
||||||
|
if (!acc[item.serverId]) {
|
||||||
|
acc[item.serverId] = [];
|
||||||
|
}
|
||||||
|
acc[item.serverId].push(item);
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, AnyLibraryItems>);
|
||||||
|
|
||||||
|
for (const serverId of Object.keys(itemsByServerId)) {
|
||||||
|
const items = itemsByServerId[serverId];
|
||||||
|
|
||||||
|
createFavoriteMutation.mutate(
|
||||||
|
{
|
||||||
|
query: {
|
||||||
|
id: items.map((item: AnyLibraryItem) => item.id),
|
||||||
|
type: ctx.type,
|
||||||
|
},
|
||||||
|
serverId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onError: (err) => {
|
||||||
|
toast.error({
|
||||||
|
message: err.message,
|
||||||
|
title: 'Error adding to favorites',
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}, [createFavoriteMutation, ctx.data, ctx.dataNodes, ctx.type]);
|
}, [createFavoriteMutation, ctx.data, ctx.dataNodes, ctx.type]);
|
||||||
|
|
||||||
const handleRemoveFromFavorites = useCallback(() => {
|
const handleRemoveFromFavorites = useCallback(() => {
|
||||||
if (!ctx.dataNodes && !ctx.data) return;
|
if (!ctx.dataNodes && !ctx.data) return;
|
||||||
|
|
||||||
let itemsToUnfavorite: AnyLibraryItems = [];
|
|
||||||
let nodesToUnfavorite: RowNode<any>[] = [];
|
|
||||||
|
|
||||||
if (ctx.dataNodes) {
|
if (ctx.dataNodes) {
|
||||||
nodesToUnfavorite = ctx.dataNodes.filter((item) => item.data.userFavorite);
|
const nodesToUnfavorite = ctx.dataNodes.filter((item) => item.data.userFavorite);
|
||||||
} else {
|
const nodesByServerId = nodesToUnfavorite.reduce((acc, node) => {
|
||||||
itemsToUnfavorite = ctx.data.filter((item) => item.userFavorite);
|
if (!acc[node.data.serverId]) {
|
||||||
|
acc[node.data.serverId] = [];
|
||||||
}
|
}
|
||||||
|
acc[node.data.serverId].push(node);
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, RowNode<any>[]>);
|
||||||
|
|
||||||
const idsToUnfavorite = nodesToUnfavorite
|
for (const serverId of Object.keys(nodesByServerId)) {
|
||||||
? nodesToUnfavorite.map((node) => node.data.id)
|
const idsToUnfavorite = nodesByServerId[serverId].map((node) => node.data.id);
|
||||||
: itemsToUnfavorite.map((item) => item.id);
|
|
||||||
|
|
||||||
deleteFavoriteMutation.mutate(
|
deleteFavoriteMutation.mutate(
|
||||||
{
|
{
|
||||||
query: {
|
query: {
|
||||||
id: idsToUnfavorite,
|
id: idsToUnfavorite,
|
||||||
type: ctx.type,
|
type: ctx.type,
|
||||||
},
|
},
|
||||||
|
serverId,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
@ -310,6 +344,28 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const itemsToUnfavorite = ctx.data.filter((item) => item.userFavorite);
|
||||||
|
const itemsByServerId = (itemsToUnfavorite as any[]).reduce((acc, item) => {
|
||||||
|
if (!acc[item.serverId]) {
|
||||||
|
acc[item.serverId] = [];
|
||||||
|
}
|
||||||
|
acc[item.serverId].push(item);
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, AnyLibraryItems>);
|
||||||
|
|
||||||
|
for (const serverId of Object.keys(itemsByServerId)) {
|
||||||
|
const idsToUnfavorite = itemsByServerId[serverId].map((item: AnyLibraryItem) => item.id);
|
||||||
|
deleteFavoriteMutation.mutate({
|
||||||
|
query: {
|
||||||
|
id: idsToUnfavorite,
|
||||||
|
type: ctx.type,
|
||||||
|
},
|
||||||
|
serverId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}, [ctx.data, ctx.dataNodes, ctx.type, deleteFavoriteMutation]);
|
}, [ctx.data, ctx.dataNodes, ctx.type, deleteFavoriteMutation]);
|
||||||
|
|
||||||
const handleAddToPlaylist = useCallback(() => {
|
const handleAddToPlaylist = useCallback(() => {
|
||||||
|
|
Reference in a new issue