All files / src/stores app.store.ts

36.26% Statements 33/91
75% Branches 3/4
25% Functions 2/8
36.26% Lines 33/91

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 1061x 1x     1x 1x 3x 3x 3x 3x 3x   1x 1x       1x   1x 1x 2x 2x   2x 2x 1x 1x 1x 2x 2x 2x 2x   1x                           1x                               1x                                     1x                             1x       1x 1x   1x
import { defineStore } from 'pinia'
import { appApi } from '@/api/app.api'
import type { App, AppWithUser, AppCreate, AppUpdate } from '@/types'
 
export const useAppStore = defineStore('app', {
  state: () => ({
    apps: [] as App[],
    currentApp: null as AppWithUser | null,
    isLoading: false,
    error: null as string | null,
  }),
 
  getters: {
    myApps: (state) => {
      const authStore = useAuthStore()
      return state.apps.filter((app) => app.userId === authStore.userId)
    },
  },
 
  actions: {
    async fetchApps(userId?: string) {
      this.isLoading = true
      this.error = null
 
      try {
        const { data } = await appApi.list({ userId })
        this.apps = data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to fetch apps'
      } finally {
        this.isLoading = false
      }
    },
 
    async fetchAppById(appId: string) {
      this.isLoading = true
      this.error = null
 
      try {
        const { data } = await appApi.getById(appId)
        this.currentApp = data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to fetch app'
      } finally {
        this.isLoading = false
      }
    },
 
    async createApp(data: AppCreate) {
      this.isLoading = true
      this.error = null
 
      try {
        const { data: app } = await appApi.create(data)
        this.apps.push(app)
        return app
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to create app'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async updateApp(appId: string, data: AppUpdate) {
      this.isLoading = true
      this.error = null
 
      try {
        const { data: app } = await appApi.update(appId, data)
        const index = this.apps.findIndex((a) => a.appId === appId)
        if (index !== -1) {
          this.apps[index] = app
        }
        return app
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to update app'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async deleteApp(appId: string) {
      this.isLoading = true
      this.error = null
 
      try {
        await appApi.delete(appId)
        this.apps = this.apps.filter((a) => a.appId !== appId)
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to delete app'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async fetchAppVariables(appId: string, version: string) {
      const { data } = await appApi.getVariables(appId, version)
      return data
    }
  },
})
 
import { useAuthStore } from './auth.store'