fix: refresh favorites after unfavorite
This commit is contained in:
@@ -25,7 +25,7 @@ type FavoritesContextValue = {
|
||||
pendingIds: Set<string>;
|
||||
statusFor: (resourceId: string) => FavoriteStatus;
|
||||
ensureFavoriteIds: (resourceIds: string[]) => Promise<void>;
|
||||
toggleFavorite: (resourceId: string) => Promise<void>;
|
||||
toggleFavorite: (resourceId: string) => Promise<boolean | null>;
|
||||
markFavorite: (resourceId: string, favorited: boolean) => void;
|
||||
};
|
||||
|
||||
@@ -158,17 +158,19 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
|
||||
);
|
||||
|
||||
const runFavoriteMutation = useCallback(
|
||||
async (resourceId: string) => {
|
||||
if (!token) return;
|
||||
async (resourceId: string): Promise<boolean | null> => {
|
||||
if (!token) return null;
|
||||
const currentlyFavorite = favoriteIds.has(resourceId);
|
||||
const nextFavorited = !currentlyFavorite;
|
||||
setPendingIds((prev) => new Set(prev).add(resourceId));
|
||||
markFavorite(resourceId, !currentlyFavorite);
|
||||
markFavorite(resourceId, nextFavorited);
|
||||
try {
|
||||
if (currentlyFavorite) await removeFavorite(token, resourceId);
|
||||
else await addFavorite(token, resourceId);
|
||||
showToast(
|
||||
currentlyFavorite ? t("favoriteRemoved") : t("favoriteAdded"),
|
||||
);
|
||||
return nextFavorited;
|
||||
} catch (error) {
|
||||
markFavorite(resourceId, currentlyFavorite);
|
||||
if (isFavoritesAuthError(error)) handleAuthError();
|
||||
@@ -191,9 +193,9 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
|
||||
pendingAfterLoginRef.current = resourceId;
|
||||
openLoginModal();
|
||||
showToast(t("favoriteLoginRequired"));
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
await runFavoriteMutation(resourceId);
|
||||
return runFavoriteMutation(resourceId);
|
||||
},
|
||||
[openLoginModal, runFavoriteMutation, showToast, status, t, token],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user