+
+ Microsserviço consumido: https://api-ranking-jogos-production.up.railway.app/api/v1
+ · Feito por Gabriel e Kaiky
+
+
+
+ Sistema de validação de token funcional
+
+
@@ -116,6 +138,7 @@ definePageMeta({
})
const RANKINGS_API_BASE_URL = 'https://api-ranking-jogos-production.up.railway.app/api/v1'
+const WISHLIST_API_BASE_URL = 'https://gameverse-wishlist-production.up.railway.app'
const rankingOptions = [
{
@@ -195,6 +218,8 @@ const selectedRanking = ref('weekly')
const rankings = ref([])
const isLoading = ref(false)
const errorMessage = ref('')
+const favoriteIds = ref(new Set())
+const togglingId = ref(null)
const selectedRankingMeta = computed(() => {
return rankingOptions.find((option) => option.value === selectedRanking.value) ?? rankingOptions[0]
@@ -266,8 +291,81 @@ async function fetchRankings() {
}
}
+async function fetchFavorites() {
+ if (!token.value) return
+
+ try {
+ const data = await $fetch(`${WISHLIST_API_BASE_URL}/api/wishlist`, {
+ headers: {
+ Authorization: `Bearer ${token.value}`
+ }
+ })
+
+ const ids = new Set(
+ (data?.data ?? [])
+ .filter((item) => item.is_favorite === 1 || item.is_favorite === true)
+ .map((item) => item.game_id)
+ )
+ favoriteIds.value = ids
+ } catch {
+ // falha silenciosa — favoritos são secundários ao ranking
+ }
+}
+
+async function toggleFavorite(game) {
+ if (!token.value) {
+ await navigateTo('/login')
+ return
+ }
+
+ const gameId = game.name
+ togglingId.value = gameId
+
+ try {
+ if (favoriteIds.value.has(gameId)) {
+ await $fetch(`${WISHLIST_API_BASE_URL}/api/wishlist/${encodeURIComponent(gameId)}`, {
+ method: 'DELETE',
+ headers: {
+ Authorization: `Bearer ${token.value}`
+ }
+ })
+ favoriteIds.value = new Set([...favoriteIds.value].filter((id) => id !== gameId))
+ $toast.success(`${gameId} removido dos favoritos.`, { duration: 4000 })
+ } else {
+ await $fetch(`${WISHLIST_API_BASE_URL}/api/wishlist`, {
+ method: 'POST',
+ headers: {
+ Authorization: `Bearer ${token.value}`
+ },
+ body: {
+ game_id: gameId,
+ is_wishlist: false,
+ is_favorite: true,
+ price_alert: false
+ }
+ })
+ favoriteIds.value = new Set([...favoriteIds.value, gameId])
+ $toast.success(`${gameId} adicionado aos favoritos!`, { duration: 4000 })
+ }
+ } catch (error) {
+ const statusCode = error?.statusCode ?? error?.response?.status ?? error?.data?.statusCode
+
+ if (statusCode === 401) {
+ clearToken()
+ $toast.error('Sua sessão expirou. Faça login novamente.', { duration: 8000 })
+ await navigateTo('/login')
+ return
+ }
+
+ $toast.error('Erro ao atualizar favoritos. Tente novamente.', { duration: 6000 })
+ } finally {
+ togglingId.value = null
+ }
+}
+
onMounted(() => {
fetchRankings()
+ fetchFavorites()
})
watch(selectedRanking, () => {