Fix favorites: optimistic remove + no loading flash
All checks were successful
Build frontend / build (push) Successful in 54s
All checks were successful
Build frontend / build (push) Successful in 54s
This commit is contained in:
@ -84,7 +84,8 @@ export const FavoritesProvider = ({ children }) => {
|
|||||||
if (!AuthService.isAuthenticated()) return false;
|
if (!AuthService.isAuthenticated()) return false;
|
||||||
try {
|
try {
|
||||||
await addFavoriteProperty(propId);
|
await addFavoriteProperty(propId);
|
||||||
await fetchFavorites(); // refresh list
|
// Refresh to get the full object with faveId
|
||||||
|
await fetchFavorites();
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[Favorites] Add failed:', err);
|
console.error('[Favorites] Add failed:', err);
|
||||||
@ -94,15 +95,18 @@ export const FavoritesProvider = ({ children }) => {
|
|||||||
|
|
||||||
const removeFavorite = async (propId) => {
|
const removeFavorite = async (propId) => {
|
||||||
if (!AuthService.isAuthenticated()) return false;
|
if (!AuthService.isAuthenticated()) return false;
|
||||||
// Find the faveId for this property
|
|
||||||
const fav = favorites.find(f => f.id === propId);
|
const fav = favorites.find(f => f.id === propId);
|
||||||
if (!fav) return false;
|
if (!fav) return false;
|
||||||
|
// Optimistic update — remove immediately from UI
|
||||||
|
const previous = [...favorites];
|
||||||
|
setFavorites(prev => prev.filter(f => f.id !== propId));
|
||||||
try {
|
try {
|
||||||
await removeFavoriteProperty(fav.faveId);
|
await removeFavoriteProperty(fav.faveId);
|
||||||
setFavorites(prev => prev.filter(f => f.id !== propId));
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[Favorites] Remove failed:', err);
|
console.error('[Favorites] Remove failed:', err);
|
||||||
|
// Rollback on failure
|
||||||
|
setFavorites(previous);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -26,7 +26,7 @@ export default function FavoritesPage() {
|
|||||||
return amount?.toLocaleString() + ' ل.س';
|
return amount?.toLocaleString() + ' ل.س';
|
||||||
};
|
};
|
||||||
|
|
||||||
if (favoritesLoading) {
|
if (favoritesLoading && favorites.length === 0) {
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex items-center justify-center">
|
<div className="min-h-screen flex items-center justify-center">
|
||||||
<div className="text-center">
|
<div className="text-center">
|
||||||
|
|||||||
Reference in New Issue
Block a user