mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-23 20:37:42 +02:00
add better type safety for favorite/rating update
This commit is contained in:
@@ -50,7 +50,9 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumDetailResponse | undefined) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -59,7 +61,8 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -75,7 +78,9 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
listQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumListResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumListResponse | undefined) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -88,7 +93,8 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -108,20 +114,33 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: { pageParams: string[]; pages: AlbumListResponse[] }) => {
|
||||
(
|
||||
prev:
|
||||
| undefined
|
||||
| { pageParams: string[]; pages: AlbumListResponse[] },
|
||||
) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
pages: prev.pages.map((page: AlbumListResponse) => {
|
||||
pages: prev.pages.map(
|
||||
(page: AlbumListResponse | undefined) => {
|
||||
if (page) {
|
||||
return {
|
||||
...page,
|
||||
items: page.items.map((item: Album) => {
|
||||
return itemIdSet.has(item.id)
|
||||
? { ...item, userFavorite: isFavorite }
|
||||
? {
|
||||
...item,
|
||||
userFavorite: isFavorite,
|
||||
}
|
||||
: item;
|
||||
}),
|
||||
};
|
||||
}),
|
||||
}
|
||||
|
||||
return page;
|
||||
},
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -146,7 +165,9 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumArtistDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumArtistDetailResponse | undefined) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -155,7 +176,8 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -170,20 +192,27 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
listQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumArtistListResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumArtistListResponse | undefined) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
items: prev.items.map((item: AlbumArtist) => {
|
||||
items: prev.items.map((item: AlbumArtist | undefined) => {
|
||||
if (item) {
|
||||
return itemIdSet.has(item.id)
|
||||
? { ...item, userFavorite: isFavorite }
|
||||
: item;
|
||||
}
|
||||
|
||||
return item;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -203,23 +232,39 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: { pageParams: string[]; pages: AlbumArtistListResponse[] }) => {
|
||||
(
|
||||
prev:
|
||||
| undefined
|
||||
| { pageParams: string[]; pages: AlbumArtistListResponse[] },
|
||||
) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
pages: prev.pages.map((page: AlbumArtistListResponse) => {
|
||||
pages: prev.pages.map(
|
||||
(page: AlbumArtistListResponse | undefined) => {
|
||||
if (page) {
|
||||
return {
|
||||
...page,
|
||||
items: page.items.map((item: AlbumArtist) => {
|
||||
items: page.items.map(
|
||||
(item: AlbumArtist) => {
|
||||
return itemIdSet.has(item.id)
|
||||
? {
|
||||
...item,
|
||||
userFavorite: isFavorite,
|
||||
}
|
||||
: item;
|
||||
}),
|
||||
},
|
||||
),
|
||||
};
|
||||
}),
|
||||
}
|
||||
|
||||
return page;
|
||||
},
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
return prev;
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -229,31 +274,6 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
break;
|
||||
}
|
||||
case LibraryItem.ARTIST: {
|
||||
const detailQueryKey = queryKeys.artists.detail(variables.apiClientProps.serverId);
|
||||
|
||||
const detailQueries = queryClient.getQueriesData({
|
||||
exact: false,
|
||||
queryKey: detailQueryKey,
|
||||
});
|
||||
|
||||
if (detailQueries.length) {
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumArtistDetailResponse) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return {
|
||||
...prev,
|
||||
userFavorite: isFavorite,
|
||||
};
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const listQueryKey = queryKeys.artists.list(variables.apiClientProps.serverId);
|
||||
|
||||
const listQueries = queryClient.getQueriesData({
|
||||
@@ -265,7 +285,10 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
listQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: ArtistListResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: ArtistListResponse | undefined) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
items: prev.items.map((item: Artist) => {
|
||||
@@ -274,7 +297,11 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
: item;
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return prev;
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -294,7 +321,12 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: { pageParams: string[]; pages: ArtistListResponse[] }) => {
|
||||
(
|
||||
prev:
|
||||
| undefined
|
||||
| { pageParams: string[]; pages: ArtistListResponse[] },
|
||||
) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
pages: prev.pages.map((page: ArtistListResponse) => {
|
||||
@@ -308,6 +340,9 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return prev;
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -330,7 +365,9 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
albumDetailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumDetailResponse | undefined) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -343,7 +380,8 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -359,7 +397,9 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: SongDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: SongDetailResponse | undefined) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -368,7 +408,8 @@ export const applyFavoriteOptimisticUpdates = (
|
||||
}
|
||||
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -47,12 +47,15 @@ export const applyRatingOptimisticUpdates = (
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumDetailResponse | undefined) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return { ...prev, userRating: rating };
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -131,12 +134,15 @@ export const applyRatingOptimisticUpdates = (
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumArtistDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumArtistDetailResponse | undefined) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return { ...prev, userRating: rating };
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -203,27 +209,6 @@ export const applyRatingOptimisticUpdates = (
|
||||
break;
|
||||
}
|
||||
case LibraryItem.ARTIST: {
|
||||
const detailQueryKey = queryKeys.artists.detail(variables.apiClientProps.serverId);
|
||||
|
||||
const detailQueries = queryClient.getQueriesData({
|
||||
exact: false,
|
||||
queryKey: detailQueryKey,
|
||||
});
|
||||
|
||||
if (detailQueries.length) {
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumArtistDetailResponse) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return { ...prev, userRating: rating };
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const listQueryKey = queryKeys.artists.list(variables.apiClientProps.serverId);
|
||||
|
||||
const listQueries = queryClient.getQueriesData({
|
||||
@@ -299,7 +284,9 @@ export const applyRatingOptimisticUpdates = (
|
||||
albumDetailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: AlbumDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: AlbumDetailResponse | undefined) => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -311,7 +298,8 @@ export const applyRatingOptimisticUpdates = (
|
||||
};
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -327,12 +315,15 @@ export const applyRatingOptimisticUpdates = (
|
||||
detailQueries.forEach(([queryKey, data]) => {
|
||||
if (data) {
|
||||
previousQueries.push({ data, queryKey });
|
||||
queryClient.setQueryData(queryKey, (prev: SongDetailResponse) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(prev: SongDetailResponse | undefined) => {
|
||||
if (prev && itemIdSet.has(prev.id)) {
|
||||
return { ...prev, userRating: rating };
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user