Skip to content

Commit

Permalink
fix: loading state reactivity
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Borysenko <[email protected]>
  • Loading branch information
andrey18106 committed Oct 29, 2024
1 parent a7c972b commit 8a9f190
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions apps/settings/src/store/app-api-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import api from './api'
import logger from '../logger'

import type { IAppstoreExApp, IDeployDaemon, IExAppStatus } from '../app-types'
import { reactive } from 'vue'
import Vue from 'vue'

interface AppApiState {
apps: IAppstoreExApp[]
Expand All @@ -31,7 +31,7 @@ export const useAppApiStore = defineStore('app-api-apps', {
state: (): AppApiState => ({
apps: [],
updateCount: loadState('settings', 'appstoreExAppUpdateCount', 0),
loading: reactive({}),
loading: {},
loadingList: false,
statusUpdater: null,
daemonAccessible: loadState('settings', 'defaultDaemonConfigAccessible', false),
Expand Down Expand Up @@ -62,6 +62,10 @@ export const useAppApiStore = defineStore('app-api-apps', {
logger.error(error)
},

setLoading(id: string, value: boolean) {
Vue.set(this.loading, id, value)
},

setError(appId: string | string[], error: string) {
const appIds = Array.isArray(appId) ? appId : [appId]
appIds.forEach((_id) => {
Expand All @@ -73,14 +77,14 @@ export const useAppApiStore = defineStore('app-api-apps', {
},

enableApp(appId: string) {
this.loading[appId] = true
this.loading.install = true
this.setLoading(appId, true)
this.setLoading('install', true)
return confirmPassword().then(() => {

return axios.post(generateUrl(`/apps/app_api/apps/enable/${appId}`))
.then((response) => {
this.loading[appId] = false
this.loading.install = false
this.setLoading(appId, false)
this.setLoading('install', false)

const app = this.apps.find((app) => app.id === appId)
if (app) {
Expand Down Expand Up @@ -123,39 +127,39 @@ export const useAppApiStore = defineStore('app-api-apps', {
})
})
.catch((error) => {
this.loading[appId] = false
this.loading.install = false
this.setLoading(appId, false)
this.setLoading('install', false)
this.setError(appId, error.response.data.data.message)
this.appsApiFailure({ appId, error })
})
})
},

forceEnableApp(appId: string) {
this.loading[appId] = true
this.loading.install = true
this.setLoading(appId, true)
this.setLoading('install', true)
return confirmPassword().then(() => {

return api.post(generateUrl('/apps/app_api/apps/force'), { appId })
.then(() => {
location.reload()
})
.catch((error) => {
this.loading[appId] = false
this.loading.install = false
this.setLoading(appId, false)
this.setLoading('install', false)
this.setError(appId, error.response.data.data.message)
this.appsApiFailure({ appId, error })
})
})
},

disableApp(appId: string) {
this.loading[appId] = true
this.setLoading(appId, true)
return confirmPassword().then(() => {

return api.get(generateUrl(`apps/app_api/apps/disable/${appId}`))
.then(() => {
this.loading[appId] = false
this.setLoading(appId, false)
const app = this.apps.find((app) => app.id === appId)
if (app) {
app.active = false
Expand All @@ -166,19 +170,19 @@ export const useAppApiStore = defineStore('app-api-apps', {
return true
})
.catch((error) => {
this.loading[appId] = false
this.setLoading(appId, false)
this.appsApiFailure({ appId, error })
})
})
},

uninstallApp(appId: string, removeData: boolean) {
this.loading[appId] = true
this.setLoading(appId, true)
return confirmPassword().then(() => {

return api.get(generateUrl(`/apps/app_api/apps/uninstall/${appId}?removeData=${removeData}`))
.then(() => {
this.loading[appId] = false
this.setLoading(appId, false)
const app = this.apps.find((app) => app.id === appId)
if (app) {
app.active = false
Expand All @@ -196,21 +200,21 @@ export const useAppApiStore = defineStore('app-api-apps', {
return true
})
.catch((error) => {
this.loading[appId] = false
this.setLoading(appId, false)
this.appsApiFailure({ appId, error })
})
})
},

updateApp(appId: string) {
this.loading[appId] = true
this.loading.install = true
this.setLoading(appId, true)
this.setLoading('install', true)
return confirmPassword().then(() => {

return api.get(generateUrl(`/apps/app_api/apps/update/${appId}`))
.then(() => {
this.loading.install = false
this.loading[appId] = false
this.setLoading(appId, false)
this.setLoading('install', false)
const app = this.apps.find((app) => app.id === appId)
if (app) {
const version = app.update
Expand All @@ -229,8 +233,8 @@ export const useAppApiStore = defineStore('app-api-apps', {
return true
})
.catch((error) => {
this.loading[appId] = false
this.loading.install = false
this.setLoading(appId, false)
this.setLoading('install', false)
this.appsApiFailure({ appId, error })
})
})
Expand Down

0 comments on commit 8a9f190

Please sign in to comment.