diff --git a/app/pages/(protected)/ranking-plataforma/index.vue b/app/pages/(protected)/ranking-plataforma/index.vue index 799fd7d..a58cc58 100644 --- a/app/pages/(protected)/ranking-plataforma/index.vue +++ b/app/pages/(protected)/ranking-plataforma/index.vue @@ -80,6 +80,14 @@ class="inline-flex items-center justify-center rounded-full bg-[#292524] px-2 py-0.5 text-[12px] font-semibold leading-none text-white"> {{ game.score }} + @@ -108,6 +116,14 @@ {{ game.name }} + @@ -154,6 +170,7 @@ useHead({ }) const COMPARE_API_BASE_URL = 'https://ranking-plataforma.onrender.com' +const WISHLIST_API_BASE_URL = 'https://gameverse-wishlist-production.up.railway.app' const platformOptions = [ { label: 'Todas as plataformas', value: 'all' }, @@ -182,6 +199,8 @@ const selectedPlatform = ref('all') const rankings = ref([]) const isLoading = ref(false) const errorMessage = ref('') +const favoriteIds = ref(new Set()) +const togglingId = ref(null) const filteredRankings = computed(() => { if (selectedPlatform.value === 'all') return rankings.value @@ -232,7 +251,68 @@ async function fetchRanking() { } } +async function fetchFavorites() { + if (!token.value) return + + try { + const data = await $fetch(`${WISHLIST_API_BASE_URL}/api/wishlist`, { + headers: { Authorization: `Bearer ${token.value}` } + }) + + favoriteIds.value = new Set( + (data?.data ?? []) + .filter((item) => item.is_favorite === 1 || item.is_favorite === true) + .map((item) => item.game_id) + ) + } catch (error) { + console.warn('Erro ao carregar os favoritos. Tente novamente.', error) + } +} + +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(() => { fetchRanking() + fetchFavorites() })