Files
nuxt-frontend/app/pages/(protected)/ranking-jogos/index.vue

389 lines
13 KiB
Vue

<template>
<div
class="relative min-h-screen overflow-x-hidden bg-[#f5f5f5] px-3 py-8 font-sans text-[#0c0a09] sm:px-4 md:px-6 md:py-12 xl:px-8 xl:py-16">
<div
class="pointer-events-none absolute left-1/2 top-8 h-[460px] w-[min(92vw,820px)] -translate-x-1/2 rounded-[48px] bg-[radial-gradient(circle_at_20%_30%,rgba(167,229,211,0.62),transparent_28%),radial-gradient(circle_at_72%_24%,rgba(244,197,168,0.5),transparent_30%),radial-gradient(circle_at_52%_78%,rgba(168,200,232,0.56),transparent_34%)] blur-2xl"
aria-hidden="true"></div>
<main class="relative mx-auto grid w-full min-w-0 max-w-[1120px] gap-6 md:gap-8" aria-labelledby="ranking-title">
<header class="flex flex-col gap-5 sm:flex-row sm:items-end sm:justify-between">
<div>
<p
class="m-0 inline-flex rounded-full bg-[rgba(244,197,168,0.56)] px-3 py-1 text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#0c0a09]">
Rankings
</p>
<h1 id="ranking-title"
class="my-5 max-w-[720px] break-words font-serif text-[34px] font-light leading-[1.08] tracking-[-0.96px] text-[#0c0a09] sm:text-[40px] md:text-[48px]">
Ranking de jogos
</h1>
<p class="m-0 max-w-[560px] text-base font-normal leading-6 tracking-[0.16px] text-[#4e4e4e]">
Acompanhe os jogos por período, plataforma e volume de jogadores ativos.
</p>
</div>
</header>
<section
class="grid min-w-0 gap-6 rounded-2xl border border-[#e7e5e4] bg-white p-4 shadow-[0_4px_16px_rgba(0,0,0,0.04)] sm:p-5 md:p-6 xl:p-8"
aria-label="Lista de ranking de jogos">
<div class="grid min-w-0 gap-5 xl:grid-cols-[minmax(0,1fr)_minmax(240px,320px)] xl:items-end">
<div>
<span
class="inline-flex min-h-6 items-center rounded-full bg-[rgba(167,229,211,0.62)] px-2.5 py-1 text-xs font-semibold uppercase leading-none tracking-[0.96px] text-[#0c0a09]">
{{ selectedRankingMeta.badge }}
</span>
<h2 class="mt-4 font-serif text-3xl font-light leading-[1.13] tracking-[-0.32px] text-[#0c0a09]">
{{ selectedRankingMeta.title }}
</h2>
</div>
<AppSelect id="ranking-filter" v-model="selectedRanking" label="Visualização" :options="rankingOptions"
:disabled="isLoading" />
</div>
<div v-if="isLoading" class="grid gap-3" role="status" aria-live="polite">
<div v-for="item in 4" :key="item" class="h-20 animate-pulse rounded-xl bg-[#f0efed]"></div>
</div>
<div v-else-if="errorMessage"
class="rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-[15px] leading-[1.47] tracking-[0.15px] text-red-700"
role="alert">
{{ errorMessage }}
</div>
<div v-else-if="!rankings.length"
class="rounded-xl border border-[#e7e5e4] bg-[#fafafa] px-4 py-6 text-center text-[15px] leading-[1.47] tracking-[0.15px] text-[#777169]">
Nenhum jogo encontrado para este ranking.
</div>
<div v-else class="overflow-hidden rounded-xl border border-[#e7e5e4]">
<div
class="hidden bg-[#fafafa] px-4 py-3 text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169] xl:grid xl:grid-cols-[72px_minmax(180px,1.5fr)_minmax(130px,1fr)_repeat(4,minmax(100px,1fr))] xl:gap-4">
<span>Posição</span>
<span>Jogo</span>
<span>Plataforma</span>
<span>Jogadores</span>
<span>Semanal</span>
<span>Mensal</span>
<span>Anual</span>
</div>
<ol class="grid divide-y divide-[#e7e5e4]">
<li v-for="(game, index) in rankings" :key="game.id"
class="grid min-w-0 gap-4 bg-white p-4 xl:grid-cols-[72px_minmax(180px,1.5fr)_minmax(130px,1fr)_repeat(4,minmax(100px,1fr))] xl:items-center xl:gap-4">
<div class="flex items-center justify-between gap-3 xl:block">
<span class="text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169] xl:hidden">
Posição
</span>
<span
class="inline-flex h-9 min-w-9 items-center justify-center rounded-full bg-[#292524] px-3 text-[15px] font-medium leading-none text-white">
{{ index + 1 }}
</span>
</div>
<div class="grid gap-1">
<span class="text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169] xl:hidden">
Jogo
</span>
<div class="flex min-w-0 items-center gap-2">
<strong class="min-w-0 break-words text-base font-medium leading-6 tracking-[0.16px] text-[#0c0a09]">
{{ game.name }}
</strong>
<button type="button" :disabled="togglingId === game.name" :aria-label="favoriteIds.has(game.name)
? 'Remover dos favoritos'
: 'Adicionar aos favoritos'
" @click="toggleFavorite(game)"
class="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full transition hover:bg-[#f0efed] disabled:cursor-not-allowed disabled:opacity-50">
<Icon :name="togglingId === game.name
? 'mdi:loading'
: favoriteIds.has(game.name)
? 'mdi:heart'
: 'mdi:heart-outline'
" :class="[
'text-base',
togglingId === game.name
? 'animate-spin text-[#777169]'
: favoriteIds.has(game.name)
? 'text-rose-500'
: 'text-[#777169]'
]" />
</button>
</div>
</div>
<div v-for="stat in getGameStats(game)" :key="stat.label"
class="flex min-w-0 items-center justify-between gap-3 xl:block">
<span class="text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169] xl:hidden">
{{ stat.label }}
</span>
<span
class="min-w-0 break-words text-right text-[15px] leading-[1.47] tracking-[0.15px] text-[#292524] xl:text-left">
{{ stat.value }}
</span>
</div>
</li>
</ol>
</div>
</section>
<div class="flex flex-col items-center gap-2 text-center text-xs leading-[1.4] tracking-[0.16px] text-[#777169]">
<p>
Microsserviço consumido:
<span class="font-medium text-[#4e4e4e]">https://api-ranking-jogos-production.up.railway.app/api/v1</span>
&nbsp;·&nbsp; Feito por Gabriel e Kaiky
</p>
<span class="inline-flex items-center gap-1.5">
<span class="h-1.5 w-1.5 rounded-full bg-emerald-500"></span>
Sistema de validação de token funcional
</span>
</div>
</main>
</div>
</template>
<script setup>
definePageMeta({
middleware: 'auth',
layout: 'protected'
})
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 = [
{
label: 'Semanal',
value: 'weekly',
endpoint: '/rankings/weekly',
badge: 'Semana',
title: 'Ranking semanal'
},
{
label: 'Mensal',
value: 'monthly',
endpoint: '/rankings/monthly',
badge: 'Mês',
title: 'Ranking mensal'
},
{
label: 'Anual',
value: 'yearly',
endpoint: '/rankings/yearly',
badge: 'Ano',
title: 'Ranking anual'
},
{
label: 'Steam',
value: 'steam',
endpoint: '/rankings/platforms/Steam',
badge: 'Steam',
title: 'Ranking da Steam'
},
{
label: 'Epic Games',
value: 'epic-games',
endpoint: '/rankings/platforms/Epic Games',
badge: 'Epic Games',
title: 'Ranking da Epic Games'
},
{
label: 'Riot Launcher',
value: 'riot-launcher',
endpoint: '/rankings/platforms/Riot Launcher',
badge: 'Riot Launcher',
title: 'Ranking da Riot Launcher'
},
{
label: 'Multiplataforma',
value: 'multi-platform',
endpoint: '/rankings/platforms/Multiplataforma',
badge: 'Multiplataforma',
title: 'Ranking multiplataforma'
},
{
label: 'Battle.net',
value: 'battle-net',
endpoint: '/rankings/platforms/Battle.net',
badge: 'Battle.net',
title: 'Ranking da Battle.net'
},
{
label: 'Mais jogados',
value: 'most-played',
endpoint: '/games/most-played',
badge: 'Ativos',
title: 'Jogos mais jogados'
}
]
const { $toast } = useNuxtApp()
const token = useCookie('token', {
secure: true,
sameSite: 'lax',
maxAge: 900
})
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]
)
})
const clearToken = () => {
token.value = null
}
const formatNumber = (value) => {
return new Intl.NumberFormat('pt-BR').format(Number(value ?? 0))
}
const getGameStats = (game) => [
{
label: 'Plataforma',
value: game.platform
},
{
label: 'Jogadores',
value: formatNumber(game.active_players)
},
{
label: 'Semanal',
value: formatNumber(game.weekly_points)
},
{
label: 'Mensal',
value: formatNumber(game.monthly_points)
},
{
label: 'Anual',
value: formatNumber(game.yearly_points)
}
]
async function fetchRankings() {
if (!token.value) {
await navigateTo('/login')
return
}
isLoading.value = true
errorMessage.value = ''
try {
const data = await $fetch(`${RANKINGS_API_BASE_URL}${selectedRankingMeta.value.endpoint}`, {
headers: {
Authorization: `Bearer ${token.value}`
}
})
rankings.value = Array.isArray(data) ? data : []
} 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
}
rankings.value = []
errorMessage.value =
error?.data?.message ?? 'Erro ao carregar o ranking de jogos. Tente novamente.'
$toast.error(errorMessage.value, { duration: 8000 })
} finally {
isLoading.value = false
}
}
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, () => {
if (import.meta.client) {
fetchRankings()
}
})
</script>