73 lines
1.5 KiB
JavaScript
73 lines
1.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { getDashboard, getNextSmokeTime } from '@/api/smoke'
|
|
|
|
export const useDashboardStore = defineStore('dashboard', {
|
|
state: () => ({
|
|
todayCount: 0,
|
|
minutesSinceLast: 0,
|
|
weekly: [],
|
|
nextSmokeTime: null,
|
|
lastFetchTime: 0,
|
|
cacheExpiry: 30 * 1000,
|
|
loading: false
|
|
}),
|
|
|
|
getters: {
|
|
isCacheValid: (state) => {
|
|
return Date.now() - state.lastFetchTime < state.cacheExpiry
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
async fetchDashboard(forceRefresh = false) {
|
|
if (!forceRefresh && this.isCacheValid) {
|
|
return
|
|
}
|
|
|
|
this.loading = true
|
|
try {
|
|
const res = await getDashboard()
|
|
this.todayCount = res.data.today_count || 0
|
|
this.minutesSinceLast = res.data.minutes_since_last || 0
|
|
this.weekly = res.data.weekly || []
|
|
this.lastFetchTime = Date.now()
|
|
} catch (e) {
|
|
console.error('fetchDashboard error:', e)
|
|
throw e
|
|
} finally {
|
|
this.loading = false
|
|
}
|
|
},
|
|
|
|
async fetchNextSmokeTime() {
|
|
try {
|
|
const res = await getNextSmokeTime()
|
|
this.nextSmokeTime = res.data
|
|
return res.data
|
|
} catch (e) {
|
|
console.error('fetchNextSmokeTime error:', e)
|
|
throw e
|
|
}
|
|
},
|
|
|
|
setDashboard(data) {
|
|
this.todayCount = data.today_count || 0
|
|
this.minutesSinceLast = data.minutes_since_last || 0
|
|
this.weekly = data.weekly || []
|
|
this.lastFetchTime = Date.now()
|
|
},
|
|
|
|
setNextSmokeTime(data) {
|
|
this.nextSmokeTime = data
|
|
},
|
|
|
|
incrementTodayCount() {
|
|
this.todayCount++
|
|
},
|
|
|
|
resetTimer() {
|
|
this.minutesSinceLast = 0
|
|
}
|
|
}
|
|
})
|