frontend/stores/NamespaceStore.ts

40 lines
1.0 KiB
TypeScript

import type { Namespace } from "~/classes/Namespace"
import { getNamespaces } from "~/requests/namespace"
export const useNamespaceStore = defineStore('namespace', {
state: () => ({
namespaces: undefined as Namespace[] | undefined,
currentNamespace: undefined as Namespace | undefined
}),
getters: {
getNamespaces: (state) => {
return (): Namespace[] | undefined => {
return state.namespaces;
}
},
getCurrentNamespace: (state) => {
return (): Namespace | undefined => state.currentNamespace;
}
},
actions: {
init() {
getNamespaces((namespaces: Namespace[]) => {
this.namespaces = namespaces;
});
},
selectNamespace(namespace: Namespace | undefined) {
if(namespace == null)
{
this.currentNamespace = undefined;
}
else if(this.currentNamespace?.metadata?.name !== namespace?.metadata?.name)
{
this.currentNamespace = namespace;
}
else
{
this.currentNamespace = undefined;
}
}
},
})