4002ff9fbf
CI / test (push) Has been cancelled
Web app: new entities (Image, RenderedAsset, SharedImage, Token, DeviceImageHistory), enums, repositories, controllers, message handlers, migrations, tests, frontend upload/library/sticker UI, Vue components. Firmware: EPD background screen binaries + gen scripts, setup_bg header. Infra: ddev config, test bundle, gitignore coverage dir. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import type { Device } from '@/types'
|
|
|
|
export const useDevicesStore = defineStore('devices', () => {
|
|
const devices = ref<Device[]>([])
|
|
const loading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
async function fetchDevices() {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
const res = await fetch('/api/devices')
|
|
if (!res.ok) throw new Error('Failed to load devices')
|
|
devices.value = await res.json()
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'Unknown error'
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function updateDevice(id: number, patch: Partial<Pick<Device, 'name' | 'orientation' | 'rotationIntervalMinutes' | 'wakeHour' | 'timezone' | 'uniquenessWindow'>>) {
|
|
const res = await fetch(`/api/devices/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(patch),
|
|
})
|
|
if (!res.ok) throw new Error('Failed to update device')
|
|
const updated: Device = await res.json()
|
|
const idx = devices.value.findIndex(d => d.id === id)
|
|
if (idx !== -1) devices.value[idx] = updated
|
|
return updated
|
|
}
|
|
|
|
async function lockImage(deviceId: number, imageId: number): Promise<Device> {
|
|
const res = await fetch(`/api/devices/${deviceId}/lock`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ imageId }),
|
|
})
|
|
if (!res.ok) throw new Error('Failed to lock image')
|
|
const updated: Device = await res.json()
|
|
const idx = devices.value.findIndex(d => d.id === deviceId)
|
|
if (idx !== -1) devices.value[idx] = updated
|
|
return updated
|
|
}
|
|
|
|
async function unlockImage(deviceId: number): Promise<Device> {
|
|
const res = await fetch(`/api/devices/${deviceId}/lock`, { method: 'DELETE' })
|
|
if (!res.ok) throw new Error('Failed to unlock')
|
|
const updated: Device = await res.json()
|
|
const idx = devices.value.findIndex(d => d.id === deviceId)
|
|
if (idx !== -1) devices.value[idx] = updated
|
|
return updated
|
|
}
|
|
|
|
return { devices, loading, error, fetchDevices, updateDevice, lockImage, unlockImage }
|
|
})
|