-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from fabriciopgl/feature/1
✨ Adiciona lógica de favoritos do usuário
- Loading branch information
Showing
13 changed files
with
244 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
NEXT_PUBLIC_GOOGLE_CLIENT_ID=xxxx | ||
NEXT_PUBLIC_GOOGLE_USER_SCRIPT_URL=xxx | ||
NEXT_PUBLIC_GOOGLE_USER_SCRIPT_URL=xxxx | ||
NEXT_PUBLIC_GOOGLE_PLACES_SCRIPT_URL=xxxx | ||
NEXT_PUBLIC_GOOGLE_FAVORITE_PLACES_SCRIPT_URL=xxxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, { createContext, useContext, useState, useEffect } from "react"; | ||
import axios from "axios"; | ||
import { useAuth } from "./AuthContext"; | ||
|
||
type FavoriteContextType = { | ||
favorites: number[]; | ||
addFavorite: (placeId: number) => void; | ||
removeFavorite: (placeId: number) => void; | ||
fetchFavorites: (userId: number) => void; | ||
isLoading: boolean; | ||
}; | ||
|
||
const FavoriteContext = createContext<FavoriteContextType | undefined>( | ||
undefined | ||
); | ||
|
||
const SCRIPT_URL = process.env.NEXT_PUBLIC_GOOGLE_FAVORITE_PLACES_SCRIPT_URL!; | ||
|
||
export const FavoriteProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [favorites, setFavorites] = useState<number[]>([]); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
const { user, isAnonymous } = useAuth(); | ||
|
||
const fetchFavorites = async (userId: number) => { | ||
try { | ||
setIsLoading(true); | ||
const response = await axios.get(`${SCRIPT_URL}?userId=${userId}`); | ||
setFavorites(response.data); | ||
} catch (error) { | ||
console.error("Error fetching favorites:", error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
const addFavorite = async (placeId: number) => { | ||
setFavorites((prevFavorites) => { | ||
const updatedFavorites = [...prevFavorites, placeId]; | ||
if (user && !isAnonymous) updateFavoriteInSheet(placeId, true); | ||
return updatedFavorites; | ||
}); | ||
}; | ||
|
||
const removeFavorite = async (placeId: number) => { | ||
setFavorites((prevFavorites) => { | ||
const updatedFavorites = prevFavorites.filter((id) => id !== placeId); | ||
if (user && !isAnonymous) updateFavoriteInSheet(placeId, false); | ||
return updatedFavorites; | ||
}); | ||
}; | ||
|
||
const updateFavoriteInSheet = async ( | ||
placeId: number, | ||
isFavorite: boolean | ||
) => { | ||
try { | ||
const userId = user?.internalId; | ||
await axios.post( | ||
SCRIPT_URL, | ||
{ | ||
userId, | ||
placeId, | ||
isFavorite, | ||
}, | ||
{ | ||
headers: { | ||
"Content-Type": "text/plain;charset=utf-8", | ||
}, | ||
} | ||
); | ||
} catch (error) { | ||
console.error("Error updating favorite in sheet:", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<FavoriteContext.Provider | ||
value={{ | ||
favorites, | ||
addFavorite, | ||
removeFavorite, | ||
fetchFavorites, | ||
isLoading, | ||
}} | ||
> | ||
{children} | ||
</FavoriteContext.Provider> | ||
); | ||
}; | ||
|
||
// Hook para acessar o contexto | ||
export const useFavorites = () => { | ||
const context = useContext(FavoriteContext); | ||
if (!context) { | ||
throw new Error("useFavorites must be used within a FavoriteProvider"); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use client"; | ||
import { Place } from "@/domains/Places/types"; | ||
import React, { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
interface PlacesContextType { | ||
places: Place[]; | ||
loading: boolean; | ||
error: string | null; | ||
} | ||
|
||
const PlacesContext = createContext<PlacesContextType | undefined>(undefined); | ||
|
||
const SCRIPT_URL = process.env.NEXT_PUBLIC_GOOGLE_PLACES_SCRIPT_URL!; | ||
|
||
export const PlacesProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [places, setPlaces] = useState<Place[]>([]); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchPlaces = async () => { | ||
setLoading(true); | ||
setError(null); | ||
try { | ||
const response = await fetch(SCRIPT_URL); | ||
if (!response.ok) { | ||
throw new Error(`Error fetching data: ${response.status}`); | ||
} | ||
const places = await response.json(); | ||
setPlaces(places.data || []); | ||
} catch (err: unknown) { | ||
setError((err as Error).message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchPlaces(); | ||
}, []); | ||
|
||
return ( | ||
<PlacesContext.Provider value={{ places, loading, error }}> | ||
{children} | ||
</PlacesContext.Provider> | ||
); | ||
}; | ||
|
||
export const usePlacesContext = () => { | ||
const context = useContext(PlacesContext); | ||
if (!context) { | ||
throw new Error("usePlacesContext deve ser usado dentro de PlacesProvider"); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type PlaceType = "restaurant" | "atraction" | "route"; | ||
|
||
export type Place = { | ||
id: number; | ||
name: string; | ||
description: string; | ||
image: string; | ||
type: PlaceType[]; | ||
}; |
Oops, something went wrong.