fix: add error rollback and error state hygiene in digest store
This commit is contained in:
parent
bc4d6df8f0
commit
30d668f692
1 changed files with 12 additions and 2 deletions
|
|
@ -12,6 +12,7 @@ export interface DigestEntry {
|
||||||
body: string | null
|
body: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Extracted link from a digest email body. Used by DigestView.vue. */
|
||||||
export interface DigestLink {
|
export interface DigestLink {
|
||||||
url: string
|
url: string
|
||||||
score: number // 2 = job-likely, 1 = other
|
score: number // 2 = job-likely, 1 = other
|
||||||
|
|
@ -24,16 +25,25 @@ export const useDigestStore = defineStore('digest', () => {
|
||||||
const error = ref<string | null>(null)
|
const error = ref<string | null>(null)
|
||||||
|
|
||||||
async function fetchAll() {
|
async function fetchAll() {
|
||||||
|
error.value = null
|
||||||
loading.value = true
|
loading.value = true
|
||||||
const { data, error: err } = await useApiFetch<DigestEntry[]>('/api/digest-queue')
|
const { data, error: err } = await useApiFetch<DigestEntry[]>('/api/digest-queue')
|
||||||
loading.value = false
|
loading.value = false
|
||||||
if (err) { error.value = 'Could not load digest queue'; return }
|
if (err) {
|
||||||
|
error.value = err.kind === 'network' ? 'Network error' : `Error ${err.status}`
|
||||||
|
return
|
||||||
|
}
|
||||||
entries.value = data ?? []
|
entries.value = data ?? []
|
||||||
}
|
}
|
||||||
|
|
||||||
async function remove(id: number) {
|
async function remove(id: number) {
|
||||||
|
const snapshot = entries.value.find(e => e.id === id)
|
||||||
entries.value = entries.value.filter(e => e.id !== id)
|
entries.value = entries.value.filter(e => e.id !== id)
|
||||||
await useApiFetch(`/api/digest-queue/${id}`, { method: 'DELETE' })
|
const { error: err } = await useApiFetch(`/api/digest-queue/${id}`, { method: 'DELETE' })
|
||||||
|
if (err) {
|
||||||
|
if (snapshot) entries.value = [...entries.value, snapshot]
|
||||||
|
error.value = err.kind === 'network' ? 'Network error' : `Error ${err.status}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { entries, loading, error, fetchAll, remove }
|
return { entries, loading, error, fetchAll, remove }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue