1 parent
6674a6fa2a
commit
4caaf74b9f
5 files changed
+213
-6
No files matched your search
+34
-1
@@ -3,7 +3,9 @@ import { ref } from 'vue'
|
||||
import {
|
||||
type CreateIssueInput,
|
||||
type Issue,
|
||||
type DailyIssueStats,
|
||||
itemCreate,
|
||||
itemDailyStat,
|
||||
itemDelete,
|
||||
itemGetByKey,
|
||||
itemsList,
|
||||
@@ -17,6 +19,7 @@ import {
|
||||
export const useIssueStore = defineStore('item', () => {
|
||||
const issues = ref<Issue[]>([])
|
||||
const stats = ref<IssueStat>()
|
||||
const dailyStats = ref<DailyIssueStats>()
|
||||
const error = ref<string | null>(null)
|
||||
const lastMeta = ref<Meta>({
|
||||
page: 1,
|
||||
@@ -85,5 +88,35 @@ export const useIssueStore = defineStore('item', () => {
|
||||
}
|
||||
}
|
||||
|
||||
return { stats, list, getByKey, create, update, remove, getStat }
|
||||
let dailyStatsRequestId = 0
|
||||
|
||||
/** Stats for the last `days` days including today, in the user's local time zone */
|
||||
async function getDailyStat(projectId?: string | null, days = 30): Promise<DailyIssueStats | undefined> {
|
||||
// Ignore responses that arrive after the project or period has changed again
|
||||
const requestId = ++dailyStatsRequestId
|
||||
try {
|
||||
const res = await itemDailyStat({
|
||||
projectId: projectId ?? undefined,
|
||||
from: localDateDaysAgo(days - 1),
|
||||
// Day boundaries are computed on the backend in the user's local time zone; `to` defaults to today there
|
||||
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
})
|
||||
if (requestId !== dailyStatsRequestId) return
|
||||
dailyStats.value = res
|
||||
|
||||
return dailyStats.value
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'error getting daily issue stat'
|
||||
}
|
||||
}
|
||||
|
||||
return { stats, dailyStats, list, getByKey, create, update, remove, getStat, getDailyStat }
|
||||
})
|
||||
|
||||
/** Local YYYY-MM-DD `days` days before today */
|
||||
function localDateDaysAgo(days: number): string {
|
||||
const date = new Date()
|
||||
date.setDate(date.getDate() - days)
|
||||
const pad = (value: number) => String(value).padStart(2, '0')
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
||||
}
|
||||
Reference in new issue
Block a user