import { defineStore } from 'pinia'; // Define types for our data interface StorageSpace { id: number; parent: { valid: boolean; int64: number; } | null; location: string | null; name: string | null; } interface StorageObject { id: number; storagespaceId: number; name: string; description: string | null; serialnumber: string | null; } export const useStorageStore = defineStore('storage', { state: () => ({ storageSpaces: [] as StorageSpace[], objects: [] as StorageObject[], loading: false, error: null as string | null, }), getters: { getStorageById: (state) => (id: number): StorageSpace | undefined => { return state.storageSpaces.find(storage => storage.id === id); }, getObjectsInStorage: (state) => (storageId: number): StorageObject[] => { return state.objects.filter(obj => obj.storagespaceId === storageId); }, getChildStorages: (state) => (parentId: number): StorageSpace[] => { return state.storageSpaces.filter(storage => storage.parent && storage.parent.valid && storage.parent.int64 === parentId ); } }, actions: { async createStorageSpace(data: { name: string; location: string; parentId: number | null }): Promise { this.loading = true; this.error = null; try { const parentData = data.parentId ? { valid: true, int64: data.parentId } : null; const response = await fetch('/api/v1/storageSpaces', { method: 'POST', headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ name: data.name, location: data.location, parent: parentData }), }); if (!response.ok) { throw new Error('Failed to create storage space'); } // Refresh the storage spaces list await this.fetchStorageSpaces(); } catch (error: any) { this.error = error.message; throw error; } finally { this.loading = false; } }, async fetchStorageSpaces(): Promise { this.loading = true; this.error = null; try { const response = await fetch('/api/v1/storageSpaces', { headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}`, }, }); if (!response.ok) { throw new Error('Failed to fetch storage spaces'); } const data = await response.json(); this.storageSpaces = data.map((space: any) => ({ id: space.id, parent: space.parent, location: space.location, name: space.name })); } catch (error: any) { this.error = error.message; throw error; } finally { this.loading = false; } }, async fetchObjectsInStorage(storageId: number): Promise { this.loading = true; this.error = null; try { const response = await fetch(`/api/v1/storageSpaces/${storageId}/objects`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}`, }, }); if (!response.ok) { throw new Error('Failed to fetch objects'); } const data = await response.json(); this.objects = data as StorageObject[]; } catch (error: any) { this.error = error.message; throw error; } finally { this.loading = false; } }, async fetchStorageHierarchy(rootStorageId: number): Promise { this.loading = true; this.error = null; try { // First, ensure we have all storage spaces if (this.storageSpaces.length === 0) { await this.fetchStorageSpaces(); } // Then fetch all objects in the hierarchy const response = await fetch(`/api/v1/storageSpaces/${rootStorageId}/hierarchy/objects`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}`, }, }); if (!response.ok) { throw new Error('Failed to fetch storage hierarchy'); } const data = await response.json(); this.objects = data as StorageObject[]; } catch (error: any) { this.error = error.message; throw error; } finally { this.loading = false; } } } });