Empty product lists were wiping the manual amount on update, and follow-ups were only created for new deals. Leaderboard rows were laid out left-to-right on an RTL page. Co-authored-by: Cursor <cursoragent@cursor.com>
356 lines
7.8 KiB
Vue
356 lines
7.8 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
|
import type { GamificationLeaderboardDto, GamificationLeaderEntryDto, GamificationPeriod } from '@mento/shared';
|
|
import UserAvatar from '@/components/UserAvatar.vue';
|
|
import { api } from '@/lib/api';
|
|
import { toFaDigits } from '@/lib/jalali';
|
|
import { apiError, faNum } from '@/lib/gamification';
|
|
import crownImg from '@/assets/club/crown.png';
|
|
|
|
const PERIODS: Array<{ value: GamificationPeriod; label: string }> = [
|
|
{ value: 'today', label: 'امروز' },
|
|
{ value: 'month', label: 'این ماه' },
|
|
{ value: 'all', label: 'همه زمانها' },
|
|
];
|
|
|
|
const period = ref<GamificationPeriod>('month');
|
|
const board = ref<GamificationLeaderboardDto | null>(null);
|
|
const loading = ref(false);
|
|
const error = ref('');
|
|
|
|
async function load() {
|
|
loading.value = true;
|
|
try {
|
|
const { data } = await api.get<GamificationLeaderboardDto>('/gamification/leaderboard', {
|
|
params: { period: period.value, limit: 50 },
|
|
});
|
|
board.value = data;
|
|
error.value = '';
|
|
} catch (e) {
|
|
error.value = apiError(e, 'بارگذاری جدول امتیازات ممکن نشد.');
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
watch(period, load);
|
|
onMounted(() => {
|
|
void load();
|
|
window.addEventListener('gamification:updated', load);
|
|
});
|
|
onBeforeUnmount(() => window.removeEventListener('gamification:updated', load));
|
|
|
|
const top3 = computed(() => (board.value?.items ?? []).slice(0, 3).filter((x) => x.score > 0));
|
|
/** ترتیب نمایش از راست: سوم، اول، دوم */
|
|
const podium = computed(() => {
|
|
const [a, b, c] = top3.value;
|
|
return [c, a, b].filter((x): x is GamificationLeaderEntryDto => !!x);
|
|
});
|
|
const rest = computed(() => {
|
|
const items = board.value?.items ?? [];
|
|
return top3.value.length ? items.slice(top3.value.length) : items;
|
|
});
|
|
function chgClass(change: number | null) {
|
|
return change && change > 0 ? 'up' : change && change < 0 ? 'down' : '';
|
|
}
|
|
|
|
const meOutside = computed(() => {
|
|
const b = board.value;
|
|
if (!b?.me) return null;
|
|
return b.items.some((i) => i.userId === b.me!.userId) ? null : b.me;
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<section class="board">
|
|
<div class="periods" role="tablist" aria-label="بازهٔ جدول امتیازات">
|
|
<button
|
|
v-for="p in PERIODS"
|
|
:key="p.value"
|
|
type="button"
|
|
role="tab"
|
|
:aria-selected="period === p.value"
|
|
:class="{ on: period === p.value }"
|
|
@click="period = p.value"
|
|
>
|
|
{{ p.label }}
|
|
</button>
|
|
</div>
|
|
|
|
<p v-if="error" class="err">{{ error }}</p>
|
|
|
|
<div v-if="podium.length" class="top3" :class="{ loading }">
|
|
<div
|
|
v-for="e in podium"
|
|
:key="e.userId"
|
|
class="t-card"
|
|
:class="[`r${e.rank}`, { me: e.isMe }]"
|
|
>
|
|
<img v-if="e.rank === 1" :src="crownImg" alt="" class="t-crown" />
|
|
<span class="t-ava">
|
|
<UserAvatar :user-id="e.userId" :has-avatar="e.hasAvatar" :name="e.fullName" :size="e.rank === 1 ? 50 : 46" />
|
|
</span>
|
|
<strong>{{ e.isMe ? 'شما' : e.fullName }}</strong>
|
|
<small>{{ faNum(e.score) }} امتیاز</small>
|
|
</div>
|
|
</div>
|
|
|
|
<ol class="rows" :class="{ loading }">
|
|
<li v-for="e in rest" :key="e.userId" :class="{ me: e.isMe }">
|
|
<span class="rank">{{ toFaDigits(e.rank) }}</span>
|
|
<UserAvatar :user-id="e.userId" :has-avatar="e.hasAvatar" :name="e.fullName" :size="28" />
|
|
<b class="who">{{ e.isMe ? 'شما' : e.fullName }}</b>
|
|
<span class="score">{{ faNum(e.score) }}</span>
|
|
<span class="chg" :class="chgClass(e.change)">
|
|
<template v-if="e.change">{{ e.change > 0 ? '▲' : '▼' }} {{ toFaDigits(Math.abs(e.change)) }}</template>
|
|
</span>
|
|
</li>
|
|
<template v-if="meOutside">
|
|
<li class="sep" aria-hidden="true">⋯</li>
|
|
<li class="me">
|
|
<span class="rank">{{ toFaDigits(meOutside.rank) }}</span>
|
|
<UserAvatar :user-id="meOutside.userId" :has-avatar="meOutside.hasAvatar" :name="meOutside.fullName" :size="28" />
|
|
<b class="who">شما</b>
|
|
<span class="score">{{ faNum(meOutside.score) }}</span>
|
|
<span class="chg" :class="chgClass(meOutside.change)">
|
|
<template v-if="meOutside.change">
|
|
{{ meOutside.change > 0 ? '▲' : '▼' }} {{ toFaDigits(Math.abs(meOutside.change)) }}
|
|
</template>
|
|
</span>
|
|
</li>
|
|
</template>
|
|
<li v-if="board && !board.items.length" class="empty">هنوز کسی در این بازه امتیاز نگرفته است.</li>
|
|
</ol>
|
|
</section>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.board {
|
|
background: var(--surface);
|
|
border: 1px solid var(--line);
|
|
border-radius: 16px;
|
|
padding: 0.75rem 0.9rem 0.5rem;
|
|
box-shadow: var(--shadow-xs);
|
|
display: grid;
|
|
grid-template-columns: minmax(0, 1fr);
|
|
gap: 0.9rem;
|
|
min-width: 0;
|
|
}
|
|
|
|
.periods {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
gap: 4px;
|
|
padding: 3px;
|
|
border-radius: 12px;
|
|
background: #f8fafc;
|
|
}
|
|
|
|
.periods button {
|
|
border: 0;
|
|
background: none;
|
|
border-radius: 10px;
|
|
padding: 0.5rem 0.6rem;
|
|
font: inherit;
|
|
font-size: 0.82rem;
|
|
font-weight: 700;
|
|
color: #64748b;
|
|
cursor: pointer;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.periods button.on {
|
|
background: #e0e7ff;
|
|
color: #1e1b4b;
|
|
}
|
|
|
|
.err {
|
|
margin: 0;
|
|
color: var(--danger);
|
|
font-size: var(--fs-sm);
|
|
}
|
|
|
|
.top3 {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
gap: 0.6rem;
|
|
align-items: end;
|
|
padding-top: 0.8rem;
|
|
}
|
|
|
|
.t-card {
|
|
--bg: #e0e7ff;
|
|
--ring: #a5b4fc;
|
|
position: relative;
|
|
display: grid;
|
|
justify-items: center;
|
|
gap: 4px;
|
|
padding: 0 0.4rem 0.7rem;
|
|
border-radius: 14px;
|
|
background: var(--bg);
|
|
text-align: center;
|
|
min-width: 0;
|
|
margin-top: 1.6rem;
|
|
}
|
|
|
|
.t-card.r1 {
|
|
--bg: #fef3c7;
|
|
--ring: #f59e0b;
|
|
padding-bottom: 1rem;
|
|
margin-top: 0;
|
|
}
|
|
|
|
.t-card.r3 {
|
|
--bg: #ffedd5;
|
|
--ring: #fb923c;
|
|
}
|
|
|
|
.t-card.me {
|
|
box-shadow: 0 0 0 2px var(--accent);
|
|
}
|
|
|
|
.t-crown {
|
|
position: absolute;
|
|
top: -1.55rem;
|
|
width: 30px;
|
|
height: 30px;
|
|
object-fit: contain;
|
|
z-index: 1;
|
|
}
|
|
|
|
.t-ava {
|
|
margin-top: -1.4rem;
|
|
padding: 3px;
|
|
border-radius: 999px;
|
|
background: #fff;
|
|
box-shadow: 0 0 0 3px var(--ring);
|
|
display: grid;
|
|
}
|
|
|
|
.t-card.r1 .t-ava {
|
|
margin-top: -0.4rem;
|
|
}
|
|
|
|
.t-card strong {
|
|
margin-top: 0.3rem;
|
|
font-size: 0.92rem;
|
|
font-weight: 800;
|
|
color: #0f172a;
|
|
max-width: 100%;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.t-card small {
|
|
font-size: 0.76rem;
|
|
color: #475569;
|
|
}
|
|
|
|
.t-card.r1 small {
|
|
color: #b45309;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.rows {
|
|
list-style: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
display: grid;
|
|
}
|
|
|
|
.rows li {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.6rem;
|
|
padding: 0.6rem 0.7rem;
|
|
border-bottom: 1px solid #f1f5f9;
|
|
}
|
|
|
|
.rows li:last-child {
|
|
border-bottom: 0;
|
|
}
|
|
|
|
.rows li.me {
|
|
background: #fef9e7;
|
|
border-radius: 10px;
|
|
border-bottom-color: transparent;
|
|
}
|
|
|
|
.rows li.me .score,
|
|
.rows li.me .who {
|
|
font-weight: 800;
|
|
}
|
|
|
|
.rows li.sep {
|
|
justify-content: center;
|
|
color: var(--muted-light);
|
|
padding: 0;
|
|
border: 0;
|
|
}
|
|
|
|
.chg {
|
|
min-width: 1.8rem;
|
|
font-size: 0.74rem;
|
|
font-weight: 800;
|
|
flex: 0 0 auto;
|
|
}
|
|
|
|
.chg.up {
|
|
color: #16a34a;
|
|
}
|
|
|
|
.chg.down {
|
|
color: #ef4444;
|
|
}
|
|
|
|
.score {
|
|
min-width: 2.8rem;
|
|
font-size: 0.84rem;
|
|
font-weight: 600;
|
|
color: #0f172a;
|
|
flex: 0 0 auto;
|
|
}
|
|
|
|
.who {
|
|
flex: 1;
|
|
min-width: 0;
|
|
text-align: start;
|
|
font-size: 0.84rem;
|
|
font-weight: 600;
|
|
color: #0f172a;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.rank {
|
|
width: 1.6rem;
|
|
text-align: center;
|
|
font-size: 0.82rem;
|
|
color: #64748b;
|
|
flex: 0 0 auto;
|
|
}
|
|
|
|
.loading {
|
|
opacity: 0.55;
|
|
}
|
|
|
|
.empty {
|
|
justify-content: center;
|
|
color: var(--muted);
|
|
font-size: var(--fs-sm);
|
|
border: 0 !important;
|
|
}
|
|
|
|
@media (max-width: 520px) {
|
|
.top3 {
|
|
gap: 6px;
|
|
}
|
|
|
|
.t-card strong {
|
|
font-size: 0.8rem;
|
|
}
|
|
}
|
|
</style>
|