feat(fc3c): downloads Pinia store + sources.checkNow action + vitests
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { useApi } from '../composables/useApi.js'
|
||||
|
||||
export const useDownloadsStore = defineStore('downloads', () => {
|
||||
const api = useApi()
|
||||
|
||||
const events = ref([])
|
||||
const cursor = ref(null)
|
||||
const hasMore = ref(true)
|
||||
const filter = ref({ status: null, source_id: null, artist_id: null })
|
||||
const selected = ref(null)
|
||||
const loading = ref(false)
|
||||
const error = ref(null)
|
||||
|
||||
function _params(extra = {}) {
|
||||
const out = { limit: 50, ...extra }
|
||||
if (filter.value.status) out.status = filter.value.status
|
||||
if (filter.value.source_id != null) out.source_id = filter.value.source_id
|
||||
if (filter.value.artist_id != null) out.artist_id = filter.value.artist_id
|
||||
return out
|
||||
}
|
||||
|
||||
async function loadFirst() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const body = await api.get('/api/downloads', { params: _params() })
|
||||
events.value = body
|
||||
cursor.value = body.length ? body[body.length - 1].id : null
|
||||
hasMore.value = body.length === 50
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (!hasMore.value || cursor.value == null) return
|
||||
loading.value = true
|
||||
try {
|
||||
const body = await api.get('/api/downloads', { params: _params({ before: cursor.value }) })
|
||||
events.value.push(...body)
|
||||
cursor.value = body.length ? body[body.length - 1].id : cursor.value
|
||||
hasMore.value = body.length === 50
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOne(id) {
|
||||
selected.value = await api.get(`/api/downloads/${id}`)
|
||||
return selected.value
|
||||
}
|
||||
|
||||
async function applyFilter(patch) {
|
||||
filter.value = { ...filter.value, ...patch }
|
||||
await loadFirst()
|
||||
}
|
||||
|
||||
function closeDetail() {
|
||||
selected.value = null
|
||||
}
|
||||
|
||||
return {
|
||||
events, cursor, hasMore, filter, selected, loading, error,
|
||||
loadFirst, loadMore, loadOne, applyFilter, closeDetail,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user