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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 4x 1x 12x 12x 12x 8x 7x 7x 7x 7x 1x 1x 7x 7x 5x 5x 2x 1x 7x 4x 4x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 2x 2x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 12x | import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'
import { itemService } from '../services/item'
import { CacheService } from '../services/cache'
import { LocalClockService } from '../services/clock'
import { OperationQueue } from '../crdt/OperationQueue'
import { getDeviceId } from '../services/device'
import { useListsStore } from './lists'
import type { Item, CreateItemRequest, UpdateItemRequest } from '../types'
function isNetworkError(e: unknown): boolean {
return axios.isAxiosError(e) && !e.response
}
export const useItemsStore = defineStore('items', () => {
// Items keyed by listId for multi-list caching
const itemsByList = ref<Record<string, Item[]>>({})
const loading = ref(false)
const error = ref<string | null>(null)
function getItems(listId: string): Item[] {
return itemsByList.value[listId] ?? []
}
async function fetchAll(listId: string) {
loading.value = true
error.value = null
// Serve cached items immediately so the list feels instant
const cached = await CacheService.getItems(listId)
if (cached.length > 0) {
itemsByList.value[listId] = cached
loading.value = false
}
try {
const fresh = await itemService.getAll(listId)
itemsByList.value[listId] = fresh
await CacheService.saveItems(listId, fresh)
} catch {
if ((itemsByList.value[listId] ?? []).length === 0) {
error.value = 'Items konnten nicht geladen werden'
}
// Silently stay on cached data when offline
} finally {
loading.value = false
}
}
async function create(listId: string, req: CreateItemRequest): Promise<Item> {
try {
const item = await itemService.create(listId, req)
Eif (!itemsByList.value[listId]) itemsByList.value[listId] = []
itemsByList.value[listId].push(item)
await CacheService.saveItem(item)
syncCounts(listId)
return item
} catch (e) {
if (!isNetworkError(e)) throw e
// Offline: apply locally with a client-generated UUID, queue for later sync
const deviceId = await getDeviceId()
const itemId = crypto.randomUUID()
const now = new Date().toISOString()
const item: Item = {
id: itemId,
listId,
name: req.name,
checked: false,
position: (itemsByList.value[listId] ?? []).length,
categoryId: req.categoryId ?? null,
categoryName: null,
categoryColor: null,
quantity: req.quantity ?? null,
quantityUnit: req.quantityUnit ?? null,
price: req.price ?? null,
imageUrl: req.imageUrl ?? null,
labels: [],
createdAt: now,
updatedAt: now,
deletedAt: null,
createdByDeviceId: null,
}
if (!itemsByList.value[listId]) itemsByList.value[listId] = []
itemsByList.value[listId].push(item)
await CacheService.saveItem(item)
syncCounts(listId)
const vectorClock = await LocalClockService.getNextClock(listId, deviceId)
await OperationQueue.enqueue({
id: crypto.randomUUID(),
listId,
deviceId,
operationType: 'ITEM_CREATE',
payload: { itemId, name: req.name, position: item.position, timestamp: Date.now() },
vectorClock,
createdAt: Date.now(),
})
return item
}
}
async function update(listId: string, itemId: string, req: UpdateItemRequest): Promise<void> {
try {
const updated = await itemService.update(listId, itemId, req)
const items = itemsByList.value[listId] ?? []
const idx = items.findIndex(i => i.id === itemId)
if (idx !== -1) items[idx] = updated
await CacheService.saveItem(updated)
} catch (e) {
if (!isNetworkError(e)) throw e
const items = itemsByList.value[listId] ?? []
const idx = items.findIndex(i => i.id === itemId)
if (idx === -1) return
const patched = {
...items[idx]!,
name: req.name,
quantity: req.quantity ?? null,
quantityUnit: req.quantityUnit ?? null,
price: req.price ?? null,
imageUrl: req.imageUrl ?? null,
updatedAt: new Date().toISOString(),
}
items[idx] = patched
await CacheService.saveItem(patched)
const deviceId = await getDeviceId()
const vectorClock = await LocalClockService.getNextClock(listId, deviceId)
await OperationQueue.enqueue({
id: crypto.randomUUID(),
listId,
deviceId,
operationType: 'ITEM_UPDATE',
payload: { itemId, name: req.name, timestamp: Date.now() },
vectorClock,
createdAt: Date.now(),
})
}
}
async function toggleCheck(listId: string, itemId: string): Promise<void> {
try {
const updated = await itemService.toggleCheck(listId, itemId)
const items = itemsByList.value[listId] ?? []
const idx = items.findIndex(i => i.id === itemId)
if (idx !== -1) items[idx] = updated
await CacheService.saveItem(updated)
syncCounts(listId)
} catch (e) {
Iif (!isNetworkError(e)) throw e
const items = itemsByList.value[listId] ?? []
const idx = items.findIndex(i => i.id === itemId)
Iif (idx === -1) return
const toggled = { ...items[idx]!, checked: !items[idx]!.checked }
items[idx] = toggled
await CacheService.saveItem(toggled)
syncCounts(listId)
const deviceId = await getDeviceId()
const vectorClock = await LocalClockService.getNextClock(listId, deviceId)
await OperationQueue.enqueue({
id: crypto.randomUUID(),
listId,
deviceId,
operationType: 'ITEM_CHECK',
payload: { itemId, checked: toggled.checked, timestamp: Date.now() },
vectorClock,
createdAt: Date.now(),
})
}
}
async function remove(listId: string, itemId: string): Promise<void> {
try {
await itemService.delete(listId, itemId)
Eif (itemsByList.value[listId]) {
itemsByList.value[listId] = itemsByList.value[listId].filter(i => i.id !== itemId)
}
await CacheService.removeItem(itemId)
syncCounts(listId)
} catch (e) {
Iif (!isNetworkError(e)) throw e
// Offline: remove locally and queue delete for sync
Eif (itemsByList.value[listId]) {
itemsByList.value[listId] = itemsByList.value[listId].filter(i => i.id !== itemId)
}
await CacheService.removeItem(itemId)
syncCounts(listId)
const deviceId = await getDeviceId()
const vectorClock = await LocalClockService.getNextClock(listId, deviceId)
await OperationQueue.enqueue({
id: crypto.randomUUID(),
listId,
deviceId,
operationType: 'ITEM_DELETE',
payload: { itemId, timestamp: Date.now() },
vectorClock,
createdAt: Date.now(),
})
}
}
function syncCounts(listId: string) {
const items = itemsByList.value[listId] ?? []
useListsStore().patchCounts(listId, items.length, items.filter(i => i.checked).length)
}
return { itemsByList, loading, error, getItems, fetchAll, create, update, toggleCheck, remove }
})
|