Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 1x 11x 11x 11x 10x 10x 10x 10x 3x 3x 10x 10x 8x 8x 2x 1x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x | import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'
import { listService } from '../services/list'
import { CacheService } from '../services/cache'
import { cacheDb } from '../services/db'
import type { ShoppingList, CreateListRequest, UpdateListRequest } from '../types'
function isNetworkError(e: unknown): boolean {
return axios.isAxiosError(e) && !e.response
}
export const useListsStore = defineStore('lists', () => {
const lists = ref<ShoppingList[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
async function fetchAll() {
loading.value = true
error.value = null
// Serve cached data immediately so the UI is never blank
const cached = await CacheService.getLists()
if (cached.length > 0) {
lists.value = cached
loading.value = false // stop skeleton once cache is ready
}
try {
const fresh = await listService.getAll()
lists.value = fresh
await CacheService.saveLists(fresh)
} catch {
if (lists.value.length === 0) {
error.value = 'Listen konnten nicht geladen werden'
}
// Silently stay on cached data when offline
} finally {
loading.value = false
}
}
async function create(req: CreateListRequest): Promise<ShoppingList> {
try {
const list = await listService.create(req)
lists.value.unshift(list)
await CacheService.saveList(list)
return list
} catch (e) {
if (!isNetworkError(e)) throw e
// Offline: create locally with a client-generated UUID, queue for sync on reconnect
const tempId = crypto.randomUUID()
const now = new Date().toISOString()
const list: ShoppingList = {
id: tempId,
name: req.name,
emoji: req.emoji ?? '🛒',
shareToken: null,
itemCount: 0,
checkedCount: 0,
participantCount: 1,
createdAt: now,
updatedAt: now,
}
lists.value.unshift(list)
await CacheService.saveList(list)
await cacheDb.pendingLists.put({ tempId, name: req.name, emoji: list.emoji, createdAt: Date.now() })
return list
}
}
async function update(listId: string, req: UpdateListRequest): Promise<void> {
const updated = await listService.update(listId, req)
const idx = lists.value.findIndex(l => l.id === listId)
Eif (idx !== -1) lists.value[idx] = updated
await CacheService.saveList(updated)
}
async function remove(listId: string): Promise<void> {
await listService.delete(listId)
lists.value = lists.value.filter(l => l.id !== listId)
await CacheService.removeList(listId)
}
function getById(listId: string): ShoppingList | undefined {
return lists.value.find(l => l.id === listId)
}
function patchCounts(listId: string, itemCount: number, checkedCount: number) {
const list = lists.value.find(l => l.id === listId)
Eif (list) {
list.itemCount = itemCount
list.checkedCount = checkedCount
CacheService.saveList(list)
}
}
async function duplicate(listId: string): Promise<ShoppingList> {
const copy = await listService.duplicate(listId)
lists.value.push(copy)
await CacheService.saveList(copy)
return copy
}
return { lists, loading, error, fetchAll, create, update, remove, getById, patchCounts, duplicate }
})
|