Skip to content

Commit

Permalink
Add leaderboard store
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFrama authored and brayo-pip committed Mar 22, 2024
1 parent efc4c25 commit 98992f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/components/Leaderboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@

<script lang="ts">
import { ref, onMounted } from 'vue'
import { getLeaderboard } from '@/firebase/data'
import { useLeaderboardStore } from '@/stores/leaderboard'
import { type ScreenTimeSummary } from '@/types'
import AWLHeader from '@/components/Header.vue'
export default {
name: 'AWLLeaderboard',
setup() {
const entries = ref([] as ScreenTimeSummary[])
const entries = ref([] as ScreenTimeSummary[] | null)
onMounted(async () => {
try {
const data = (await getLeaderboard()) as ScreenTimeSummary[]
data.sort((a, b) => b.total - a.total)
entries.value = data
const { fetchLeaderboardData, leaderboardData} = useLeaderboardStore()
fetchLeaderboardData()
leaderboardData?.sort((a, b) => b.total - a.total)
entries.value = leaderboardData
} catch (error) {
console.error(error)
}
Expand Down
27 changes: 27 additions & 0 deletions src/stores/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import { defineStore } from 'pinia'
import type { ScreenTimeSummary } from '@/types'
import { getLeaderboard } from '@/firebase/data'
import { ref } from 'vue'

export const useLeaderboardStore = defineStore('leaderboard', {
state: () => ({
leaderboardData: ref<ScreenTimeSummary[] | null>(null),
}),
getters: {
leaderboardExists: (state) => Boolean(state.leaderboardData),
getleaderboardData: (state) => state.leaderboardData
},
persist: true,
actions: {
async fetchLeaderboardData() {
getLeaderboard().then((data) => {
this.leaderboardData = data
})
},
async resetStore() {
this.leaderboardData = null
this.$reset()
}
}
})

0 comments on commit 98992f4

Please sign in to comment.