feat(series): seriesManage Pinia store + pure moveItem reorder helper

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-16 23:36:44 -04:00
parent a9e7ee9ba9
commit b19aff198e
2 changed files with 160 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
// Pure, unit-tested: move arr[from] to index `to`, returning a new array.
export function moveItem(arr, from, to) {
const next = arr.slice()
const [x] = next.splice(from, 1)
next.splice(to, 0, x)
return next
}
export const useSeriesManageStore = defineStore('seriesManage', () => {
const api = useApi()
const tagId = ref(null)
const series = ref(null)
const pages = ref([]) // [{image_id, page_number, thumbnail_url}]
const picker = ref([]) // gallery scroll results
const pickerCursor = ref(null)
const pickerSelection = ref([]) // image ids
const loading = ref(false)
async function load(id) {
tagId.value = id
loading.value = true
try {
const body = await api.get(`/api/series/${id}/pages`)
series.value = body.series
pages.value = body.pages
} finally {
loading.value = false
}
}
async function refresh() {
if (tagId.value != null) await load(tagId.value)
}
async function loadPicker(reset = false) {
if (reset) { picker.value = []; pickerCursor.value = null }
const params = { limit: 50 }
if (pickerCursor.value) params.cursor = pickerCursor.value
const body = await api.get('/api/gallery/scroll', { params })
picker.value.push(...body.images)
pickerCursor.value = body.next_cursor
}
function togglePick(imageId) {
const i = pickerSelection.value.indexOf(imageId)
if (i === -1) pickerSelection.value.push(imageId)
else pickerSelection.value.splice(i, 1)
}
async function addSelected() {
if (pickerSelection.value.length === 0) return
await api.post(`/api/series/${tagId.value}/pages`, {
body: { image_ids: pickerSelection.value }
})
pickerSelection.value = []
await refresh()
globalThis.window?.__fcToast?.({ text: 'Added to series', type: 'success' })
}
async function remove(imageId) {
await api.post(`/api/series/${tagId.value}/pages/remove`, {
body: { image_ids: [imageId] }
})
await refresh()
}
async function reorder(orderedImageIds) {
await api.post(`/api/series/${tagId.value}/reorder`, {
body: { image_ids: orderedImageIds }
})
await refresh()
}
async function setCover(imageId) {
await api.post(`/api/series/${tagId.value}/cover`, {
body: { image_id: imageId }
})
await refresh()
}
return {
tagId, series, pages, picker, pickerCursor, pickerSelection, loading,
load, refresh, loadPicker, togglePick, addSelected, remove,
reorder, setCover
}
})
+69
View File
@@ -0,0 +1,69 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useSeriesManageStore, moveItem } from '../src/stores/seriesManage.js'
function stubFetch(handler) {
globalThis.fetch = vi.fn(async (url, init) => {
const { status, body } = handler(url, init)
return {
ok: status >= 200 && status < 300,
status, statusText: String(status),
text: async () => (body == null ? '' : JSON.stringify(body))
}
})
}
describe('seriesManage', () => {
beforeEach(() => setActivePinia(createPinia()))
afterEach(() => vi.restoreAllMocks())
it('moveItem reorders immutably', () => {
expect(moveItem([1, 2, 3, 4], 3, 0)).toEqual([4, 1, 2, 3])
expect(moveItem([1, 2, 3], 0, 2)).toEqual([2, 3, 1])
})
it('load fetches pages + series', async () => {
const s = useSeriesManageStore()
stubFetch(() => ({
status: 200,
body: { series: { id: 7, name: 'V' },
pages: [{ image_id: 1, page_number: 1, thumbnail_url: 't' }] }
}))
await s.load(7)
expect(s.series).toEqual({ id: 7, name: 'V' })
expect(s.pages.map(p => p.image_id)).toEqual([1])
})
it('reorder posts the full ordered id list', async () => {
const s = useSeriesManageStore()
s.tagId = 7
s.pages = [{ image_id: 1 }, { image_id: 2 }, { image_id: 3 }]
const calls = []
stubFetch((url, init) => {
calls.push({ url, body: JSON.parse(init.body) })
if (url.includes('/reorder')) return { status: 200, body: { ok: true } }
return { status: 200, body: { series: { id: 7, name: 'V' }, pages: [] } }
})
await s.reorder([3, 1, 2])
const r = calls.find(c => c.url.includes('/reorder'))
expect(r.url).toContain('/api/series/7/reorder')
expect(r.body).toEqual({ image_ids: [3, 1, 2] })
})
it('addSelected posts picker selection then reloads', async () => {
const s = useSeriesManageStore()
s.tagId = 7
s.pickerSelection = [9, 10]
const calls = []
stubFetch((url, init) => {
calls.push({ url, body: init.body ? JSON.parse(init.body) : null })
if (url.includes('/pages') && init.method === 'POST')
return { status: 200, body: { added_count: 2 } }
return { status: 200, body: { series: { id: 7, name: 'V' }, pages: [] } }
})
await s.addSelected()
const add = calls.find(c => c.url.endsWith('/api/series/7/pages'))
expect(add.body).toEqual({ image_ids: [9, 10] })
expect(s.pickerSelection).toEqual([])
})
})