Skip to content

Commit

Permalink
Merge pull request #16 from fabriciopgl/feature/1
Browse files Browse the repository at this point in the history
🚑 Ajusta loop infinito na busca de favoritos caso o usuário não tenha nenhum local como favorito
  • Loading branch information
fabriciopgl authored Dec 3, 2024
2 parents ca220d5 + 9232c51 commit c090d36
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions app/favoritos/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FaSearch } from "react-icons/fa";

export default function FavoritesPage() {
const [searchValue, setSearchValue] = useState("");
const [favoritesFetched, setFavoritesFetched] = useState(false);

const { user, isAnonymous } = useAuth();
const {
Expand All @@ -25,11 +26,15 @@ export default function FavoritesPage() {
isLoading: favoritesLoading,
} = useFavorites();

// Fetch favorites only once if user is logged in and not anonymous
useEffect(() => {
if (favorites.length === 0 && user && !isAnonymous)
if (user && !isAnonymous && !favoritesFetched) {
fetchFavorites(user?.internalId);
}, [favorites, fetchFavorites, isAnonymous, user]);
setFavoritesFetched(true);
}
}, [user, isAnonymous, fetchFavorites, favoritesFetched]);

// Search logic
useEffect(() => {
const removeAccents = (str: string) => {
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
Expand Down Expand Up @@ -105,12 +110,13 @@ export default function FavoritesPage() {

{!favoritesLoading &&
!placesLoading &&
userFavoritePlaces.length === 0 &&
!isAnonymous && (
userFavoritePlaces.length === 0 && (
<div className="flex justify-center items-center mt-4">
<Card>
<CardBody className="flex flex-row justify-center items-center gap-2">
Nenhum resultado encontrado, tente pesquisar novamente{" "}
{isAnonymous
? "Você não tem nenhum favorito."
: "Nenhum resultado encontrado, tente pesquisar novamente"}{" "}
<FaSearch />
</CardBody>
</Card>
Expand Down

0 comments on commit c090d36

Please sign in to comment.