refactor(dry-F1): shared useAsyncAction lifecycle helper for stores
New composables/useAsyncAction.js owns the loading/error/try-finally lifecycle. Migrated 11 stores: credentials, downloads, sources, posts (error=raw) + ml, artistDirectory, tagDirectory, showcase, suggestions, seriesReader, modal (error=message). The errorAs option preserves each store's existing error shape so store.error keeps the same type for components (~50 consumption sites unchanged). Stores whose catch also reset data (suggestions/seriesReader/modal) clear it upfront instead. Deliberately NOT migrated (special control flow, would change behavior): artist (conditional 404 catch + dual loading states), migration (rethrows), gallery (inflight-id stale-response guard), and the Shape-B no-catch / loaded-guard / keyed-cache stores. Net -77 lines. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
// Owns the loading/error lifecycle shared by most Pinia stores. `run`
|
||||||
|
// flips loading on, clears error, runs `fn`, captures any throw into error
|
||||||
|
// (swallowed so callers can read `store.error`), and always resets loading.
|
||||||
|
// Returns fn's resolved value on success, or undefined if it threw.
|
||||||
|
//
|
||||||
|
// `errorAs` matches each store's existing convention so migrations don't
|
||||||
|
// change what `store.error` holds: 'raw' keeps the thrown object (the
|
||||||
|
// ApiError, with .status/.body), 'message' keeps just the string.
|
||||||
|
export function useAsyncAction({ errorAs = 'raw' } = {}) {
|
||||||
|
const loading = ref(false)
|
||||||
|
const error = ref(null)
|
||||||
|
|
||||||
|
async function run(fn) {
|
||||||
|
loading.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
return await fn()
|
||||||
|
} catch (e) {
|
||||||
|
error.value = errorAs === 'message' ? (e?.message ?? String(e)) : e
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { loading, error, run }
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
const PAGE = 60
|
const PAGE = 60
|
||||||
|
|
||||||
@@ -8,8 +9,7 @@ export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
|
|||||||
const api = useApi()
|
const api = useApi()
|
||||||
const cards = ref([])
|
const cards = ref([])
|
||||||
const nextCursor = ref(null)
|
const nextCursor = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
const q = ref('')
|
const q = ref('')
|
||||||
const platform = ref(null)
|
const platform = ref(null)
|
||||||
let started = false
|
let started = false
|
||||||
@@ -17,9 +17,7 @@ export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
|
|||||||
async function loadMore() {
|
async function loadMore() {
|
||||||
if (loading.value) return
|
if (loading.value) return
|
||||||
if (started && nextCursor.value === null) return
|
if (started && nextCursor.value === null) return
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const params = { limit: PAGE }
|
const params = { limit: PAGE }
|
||||||
if (q.value) params.q = q.value
|
if (q.value) params.q = q.value
|
||||||
if (platform.value) params.platform = platform.value
|
if (platform.value) params.platform = platform.value
|
||||||
@@ -28,11 +26,7 @@ export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
|
|||||||
cards.value.push(...body.cards)
|
cards.value.push(...body.cards)
|
||||||
nextCursor.value = body.next_cursor
|
nextCursor.value = body.next_cursor
|
||||||
started = true
|
started = true
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function reset() {
|
async function reset() {
|
||||||
|
|||||||
@@ -1,28 +1,22 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
export const useCredentialsStore = defineStore('credentials', () => {
|
export const useCredentialsStore = defineStore('credentials', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
const byPlatform = ref(new Map())
|
const byPlatform = ref(new Map())
|
||||||
const extensionKey = ref(null)
|
const extensionKey = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction()
|
||||||
const error = ref(null)
|
|
||||||
|
|
||||||
async function loadAll() {
|
async function loadAll() {
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const arr = await api.get('/api/credentials')
|
const arr = await api.get('/api/credentials')
|
||||||
const m = new Map()
|
const m = new Map()
|
||||||
for (const c of arr) m.set(c.platform, c)
|
for (const c of arr) m.set(c.platform, c)
|
||||||
byPlatform.value = m
|
byPlatform.value = m
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upload(platform, credential_type, data) {
|
async function upload(platform, credential_type, data) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
export const useDownloadsStore = defineStore('downloads', () => {
|
export const useDownloadsStore = defineStore('downloads', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
@@ -13,8 +14,7 @@ export const useDownloadsStore = defineStore('downloads', () => {
|
|||||||
from_date: null, to_date: null,
|
from_date: null, to_date: null,
|
||||||
})
|
})
|
||||||
const selected = ref(null)
|
const selected = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction()
|
||||||
const error = ref(null)
|
|
||||||
const stats = ref({ pending: 0, running: 0, ok: 0, error: 0, skipped: 0 })
|
const stats = ref({ pending: 0, running: 0, ok: 0, error: 0, skipped: 0 })
|
||||||
const activity = ref({ hours: 24, buckets: [] })
|
const activity = ref({ hours: 24, buckets: [] })
|
||||||
const failing = ref([])
|
const failing = ref([])
|
||||||
@@ -28,33 +28,22 @@ export const useDownloadsStore = defineStore('downloads', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadFirst() {
|
async function loadFirst() {
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const body = await api.get('/api/downloads', { params: _params() })
|
const body = await api.get('/api/downloads', { params: _params() })
|
||||||
events.value = body
|
events.value = body
|
||||||
cursor.value = body.length ? body[body.length - 1].id : null
|
cursor.value = body.length ? body[body.length - 1].id : null
|
||||||
hasMore.value = body.length === 50
|
hasMore.value = body.length === 50
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadMore() {
|
async function loadMore() {
|
||||||
if (!hasMore.value || cursor.value == null) return
|
if (!hasMore.value || cursor.value == null) return
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
try {
|
|
||||||
const body = await api.get('/api/downloads', { params: _params({ before: cursor.value }) })
|
const body = await api.get('/api/downloads', { params: _params({ before: cursor.value }) })
|
||||||
events.value.push(...body)
|
events.value.push(...body)
|
||||||
cursor.value = body.length ? body[body.length - 1].id : cursor.value
|
cursor.value = body.length ? body[body.length - 1].id : cursor.value
|
||||||
hasMore.value = body.length === 50
|
hasMore.value = body.length === 50
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadOne(id) {
|
async function loadOne(id) {
|
||||||
|
|||||||
@@ -1,23 +1,17 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
export const useMLStore = defineStore('ml', () => {
|
export const useMLStore = defineStore('ml', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
const settings = ref(null)
|
const settings = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
|
|
||||||
async function loadSettings() {
|
async function loadSettings() {
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
settings.value = await api.get('/api/ml/settings')
|
settings.value = await api.get('/api/ml/settings')
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function patchSettings(patch) {
|
async function patchSettings(patch) {
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ import { defineStore } from 'pinia'
|
|||||||
import { toast } from '../utils/toast.js'
|
import { toast } from '../utils/toast.js'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
export const useModalStore = defineStore('modal', () => {
|
export const useModalStore = defineStore('modal', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
const currentImageId = ref(null)
|
const currentImageId = ref(null)
|
||||||
const current = ref(null)
|
const current = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
|
|
||||||
// Post-scoped cycle. When set, prev/next cycles within this array
|
// Post-scoped cycle. When set, prev/next cycles within this array
|
||||||
// (used by PostCard's expanded-mosaic PostImageGrid clicks). When
|
// (used by PostCard's expanded-mosaic PostImageGrid clicks). When
|
||||||
@@ -20,8 +20,7 @@ export const useModalStore = defineStore('modal', () => {
|
|||||||
|
|
||||||
async function open (id, opts = {}) {
|
async function open (id, opts = {}) {
|
||||||
currentImageId.value = id
|
currentImageId.value = id
|
||||||
loading.value = true
|
current.value = null // cleared upfront so it stays null on error
|
||||||
error.value = null
|
|
||||||
// Update post-scoped state if caller passed it; otherwise clear so
|
// Update post-scoped state if caller passed it; otherwise clear so
|
||||||
// the next open() from gallery context uses neighbors mode.
|
// the next open() from gallery context uses neighbors mode.
|
||||||
if (opts.postImageIds != null) {
|
if (opts.postImageIds != null) {
|
||||||
@@ -32,14 +31,9 @@ export const useModalStore = defineStore('modal', () => {
|
|||||||
postImageIds.value = null
|
postImageIds.value = null
|
||||||
postImageIndex.value = 0
|
postImageIndex.value = 0
|
||||||
}
|
}
|
||||||
try {
|
await run(async () => {
|
||||||
current.value = await api.get(`/api/gallery/image/${id}`)
|
current.value = await api.get(`/api/gallery/image/${id}`)
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
current.value = null
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function close () {
|
async function close () {
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
export const usePostsStore = defineStore('posts', () => {
|
export const usePostsStore = defineStore('posts', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
const items = ref([])
|
const items = ref([])
|
||||||
const cursor = ref(null)
|
const cursor = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction()
|
||||||
const done = ref(false)
|
const done = ref(false)
|
||||||
const error = ref(null)
|
|
||||||
const filters = ref({ artist_id: null, platform: null })
|
const filters = ref({ artist_id: null, platform: null })
|
||||||
|
|
||||||
function _qs() {
|
function _qs() {
|
||||||
@@ -38,18 +38,12 @@ export const usePostsStore = defineStore('posts', () => {
|
|||||||
|
|
||||||
async function loadMore() {
|
async function loadMore() {
|
||||||
if (loading.value || done.value) return
|
if (loading.value || done.value) return
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const body = await api.get('/api/posts', { params: _qs() })
|
const body = await api.get('/api/posts', { params: _qs() })
|
||||||
items.value.push(...body.items)
|
items.value.push(...body.items)
|
||||||
cursor.value = body.next_cursor
|
cursor.value = body.next_cursor
|
||||||
if (body.next_cursor == null) done.value = true
|
if (body.next_cursor == null) done.value = true
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPostFull(id) {
|
async function getPostFull(id) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
// IR reader.js "viewport-center-in-third" rule, pure for unit tests.
|
// IR reader.js "viewport-center-in-third" rule, pure for unit tests.
|
||||||
// metrics: [{page_number, top, height}] in page order.
|
// metrics: [{page_number, top, height}] in page order.
|
||||||
@@ -33,23 +34,16 @@ export const useSeriesReaderStore = defineStore('seriesReader', () => {
|
|||||||
|
|
||||||
const series = ref(null)
|
const series = ref(null)
|
||||||
const pages = ref([])
|
const pages = ref([])
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
|
|
||||||
async function load(tagId) {
|
async function load(tagId) {
|
||||||
loading.value = true
|
series.value = null // cleared upfront so they stay reset on error
|
||||||
error.value = null
|
pages.value = []
|
||||||
try {
|
await run(async () => {
|
||||||
const body = await api.get(`/api/series/${tagId}/pages`)
|
const body = await api.get(`/api/series/${tagId}/pages`)
|
||||||
series.value = body.series
|
series.value = body.series
|
||||||
pages.value = body.pages
|
pages.value = body.pages
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
series.value = null
|
|
||||||
pages.value = []
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { series, pages, loading, error, load }
|
return { series, pages, loading, error, load }
|
||||||
|
|||||||
@@ -1,32 +1,26 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
const PAGE = 60
|
const PAGE = 60
|
||||||
|
|
||||||
export const useShowcaseStore = defineStore('showcase', () => {
|
export const useShowcaseStore = defineStore('showcase', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
const images = ref([])
|
const images = ref([])
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
const exhausted = ref(false)
|
const exhausted = ref(false)
|
||||||
const seen = new Set()
|
const seen = new Set()
|
||||||
|
|
||||||
async function fetchPage() {
|
async function fetchPage() {
|
||||||
if (loading.value) return
|
if (loading.value) return
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const body = await api.get('/api/showcase', { params: { limit: PAGE } })
|
const body = await api.get('/api/showcase', { params: { limit: PAGE } })
|
||||||
const fresh = body.images.filter(i => !seen.has(i.id))
|
const fresh = body.images.filter(i => !seen.has(i.id))
|
||||||
if (fresh.length === 0) { exhausted.value = true; return }
|
if (fresh.length === 0) { exhausted.value = true; return }
|
||||||
for (const i of fresh) seen.add(i.id)
|
for (const i of fresh) seen.add(i.id)
|
||||||
images.value.push(...fresh)
|
images.value.push(...fresh)
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function shuffle() {
|
async function shuffle() {
|
||||||
|
|||||||
@@ -1,42 +1,30 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
export const useSourcesStore = defineStore('sources', () => {
|
export const useSourcesStore = defineStore('sources', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
// keyed cache: null => all sources, number => artist_id filtered
|
// keyed cache: null => all sources, number => artist_id filtered
|
||||||
const byArtist = ref(new Map())
|
const byArtist = ref(new Map())
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction()
|
||||||
const error = ref(null)
|
|
||||||
const scheduleStatus = ref(null)
|
const scheduleStatus = ref(null)
|
||||||
|
|
||||||
const allSources = computed(() => byArtist.value.get(null) ?? [])
|
const allSources = computed(() => byArtist.value.get(null) ?? [])
|
||||||
|
|
||||||
async function loadAll() {
|
async function loadAll() {
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const body = await api.get('/api/sources')
|
const body = await api.get('/api/sources')
|
||||||
byArtist.value.set(null, body)
|
byArtist.value.set(null, body)
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadForArtist(artistId) {
|
async function loadForArtist(artistId) {
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const body = await api.get('/api/sources', { params: { artist_id: artistId } })
|
const body = await api.get('/api/sources', { params: { artist_id: artistId } })
|
||||||
byArtist.value.set(artistId, body)
|
byArtist.value.set(artistId, body)
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _invalidate(artistId) {
|
function _invalidate(artistId) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
|
|||||||
import { toast } from '../utils/toast.js'
|
import { toast } from '../utils/toast.js'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
// Category display order: people/sources first, general last.
|
// Category display order: people/sources first, general last.
|
||||||
export const CATEGORY_ORDER = ['artist', 'character', 'copyright', 'general']
|
export const CATEGORY_ORDER = ['artist', 'character', 'copyright', 'general']
|
||||||
@@ -15,23 +16,16 @@ export const CATEGORY_LABELS = {
|
|||||||
export const useSuggestionsStore = defineStore('suggestions', () => {
|
export const useSuggestionsStore = defineStore('suggestions', () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
const byCategory = ref({}) // { category: [suggestion, ...] }
|
const byCategory = ref({}) // { category: [suggestion, ...] }
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
let currentImageId = null
|
let currentImageId = null
|
||||||
|
|
||||||
async function load(imageId) {
|
async function load(imageId) {
|
||||||
currentImageId = imageId
|
currentImageId = imageId
|
||||||
loading.value = true
|
byCategory.value = {} // cleared upfront so it stays empty on error
|
||||||
error.value = null
|
await run(async () => {
|
||||||
try {
|
|
||||||
const body = await api.get(`/api/images/${imageId}/suggestions`)
|
const body = await api.get(`/api/images/${imageId}/suggestions`)
|
||||||
byCategory.value = body.by_category || {}
|
byCategory.value = body.by_category || {}
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
byCategory.value = {}
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _drop(category, predicate) {
|
function _drop(category, predicate) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useApi } from '../composables/useApi.js'
|
import { useApi } from '../composables/useApi.js'
|
||||||
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||||
|
|
||||||
const PAGE = 60
|
const PAGE = 60
|
||||||
|
|
||||||
@@ -8,8 +9,7 @@ export const useTagDirectoryStore = defineStore('tagDirectory', () => {
|
|||||||
const api = useApi()
|
const api = useApi()
|
||||||
const cards = ref([])
|
const cards = ref([])
|
||||||
const nextCursor = ref(null)
|
const nextCursor = ref(null)
|
||||||
const loading = ref(false)
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
||||||
const error = ref(null)
|
|
||||||
const kind = ref(null)
|
const kind = ref(null)
|
||||||
const q = ref('')
|
const q = ref('')
|
||||||
let started = false
|
let started = false
|
||||||
@@ -17,9 +17,7 @@ export const useTagDirectoryStore = defineStore('tagDirectory', () => {
|
|||||||
async function loadMore() {
|
async function loadMore() {
|
||||||
if (loading.value) return
|
if (loading.value) return
|
||||||
if (started && nextCursor.value === null) return
|
if (started && nextCursor.value === null) return
|
||||||
loading.value = true
|
await run(async () => {
|
||||||
error.value = null
|
|
||||||
try {
|
|
||||||
const params = { limit: PAGE }
|
const params = { limit: PAGE }
|
||||||
if (kind.value) params.kind = kind.value
|
if (kind.value) params.kind = kind.value
|
||||||
if (q.value) params.q = q.value
|
if (q.value) params.q = q.value
|
||||||
@@ -28,11 +26,7 @@ export const useTagDirectoryStore = defineStore('tagDirectory', () => {
|
|||||||
cards.value.push(...body.cards)
|
cards.value.push(...body.cards)
|
||||||
nextCursor.value = body.next_cursor
|
nextCursor.value = body.next_cursor
|
||||||
started = true
|
started = true
|
||||||
} catch (e) {
|
})
|
||||||
error.value = e.message
|
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function reset() {
|
async function reset() {
|
||||||
|
|||||||
Reference in New Issue
Block a user