From 109bdb8b154377dc2ced0acf98f2324e54854cd1 Mon Sep 17 00:00:00 2001 From: Fabricio Pagliarini Date: Mon, 25 Nov 2024 23:32:39 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adiciona=20l=C3=B3gica=20de=20favor?= =?UTF-8?q?itos=20do=20usu=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 4 +- .github/workflows/deploy.yml | 2 + app/favoritos/page.tsx | 1 - app/page.tsx | 57 ++++++--- app/providers.tsx | 9 +- components/Footer/index.tsx | 1 - components/Login/index.tsx | 2 +- components/NavbarSearch/index.tsx | 2 - components/PlaceCard/index.tsx | 27 ++++- contexts/FavoritePlacesContext.tsx | 101 ++++++++++++++++ contexts/PlacesContext.tsx | 56 +++++++++ domains/Places/types.ts | 9 ++ hooks/usePlaces.tsx | 188 ----------------------------- 13 files changed, 244 insertions(+), 215 deletions(-) create mode 100644 contexts/FavoritePlacesContext.tsx create mode 100644 contexts/PlacesContext.tsx create mode 100644 domains/Places/types.ts delete mode 100644 hooks/usePlaces.tsx diff --git a/.env.development b/.env.development index 141be67..327d04f 100644 --- a/.env.development +++ b/.env.development @@ -1,2 +1,4 @@ NEXT_PUBLIC_GOOGLE_CLIENT_ID=xxxx -NEXT_PUBLIC_GOOGLE_USER_SCRIPT_URL=xxx \ No newline at end of file +NEXT_PUBLIC_GOOGLE_USER_SCRIPT_URL=xxxx +NEXT_PUBLIC_GOOGLE_PLACES_SCRIPT_URL=xxxx +NEXT_PUBLIC_GOOGLE_FAVORITE_PLACES_SCRIPT_URL=xxxx \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a9bf156..5fdbc54 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -27,6 +27,8 @@ jobs: env: NEXT_PUBLIC_GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} NEXT_PUBLIC_GOOGLE_USER_SCRIPT_URL: ${{ secrets.GOOGLE_USER_SCRIPT_URL }} + NEXT_PUBLIC_GOOGLE_PLACES_SCRIPT_URL: ${{ secrets.GOOGLE_PLACES_SCRIPT_URL }} + NEXT_PUBLIC_GOOGLE_FAVORITE_PLACES_SCRIPT_URL: ${{ secrets.GOOGLE_FAVORITE_PLACES_SCRIPT_URL }} run: yarn build - name: Deploy to GitHub Pages diff --git a/app/favoritos/page.tsx b/app/favoritos/page.tsx index 35ba7f5..95249c0 100644 --- a/app/favoritos/page.tsx +++ b/app/favoritos/page.tsx @@ -1,4 +1,3 @@ -// app/routes/about/page.tsx export default function FavoritesPage() { return (
diff --git a/app/page.tsx b/app/page.tsx index c74efed..8435e59 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,16 +1,19 @@ "use client"; -import PlaceCard from "@/components/PlaceCard"; import Filter from "@/components/Filters"; import NavbarSearch from "@/components/NavbarSearch"; - -import usePlaces, { Place } from "@/hooks/usePlaces"; - +import PlaceCard from "@/components/PlaceCard"; +import { useAuth } from "@/contexts/AuthContext"; +import { useFavorites } from "@/contexts/FavoritePlacesContext"; +import { usePlacesContext } from "@/contexts/PlacesContext"; +import { Place } from "@/domains/Places/types"; import { Button, Card, CardBody } from "@nextui-org/react"; import { useEffect, useState } from "react"; import { FaArrowLeft, FaArrowRight, FaSearch } from "react-icons/fa"; export default function HomePage() { - const { places } = usePlaces(); + const { user, isAnonymous } = useAuth(); + const { favorites, fetchFavorites, isLoading } = useFavorites(); + const { places, loading } = usePlacesContext(); const [currentPage, setCurrentPage] = useState(1); const [filteredPlaces, setFilteredPlaces] = useState(places); const [searchValue, setSearchValue] = useState(""); @@ -45,6 +48,12 @@ export default function HomePage() { setCurrentPage(1); }, [searchValue, places]); + useEffect(() => { + if (user && !isAnonymous && favorites.length === 0) + fetchFavorites(user.internalId); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [user, isAnonymous]); + const handleSearch = (value: string) => { setSearchValue(value); }; @@ -74,17 +83,37 @@ export default function HomePage() {
- {currentPlaces.map((place) => ( - - ))} + {loading || isLoading + ? Array.from({ length: 9 }).map((_, index) => ( + +
+ +
+
+
+
+
+
+
+
+
+ )) + : currentPlaces.map((place) => ( + f === place.id)} + /> + ))}
- {currentPlaces.length > 0 ? ( + {!loading && currentPlaces.length > 0 ? (