admin-frontend/app/realm/RealmStore.ts
2026-04-04 19:40:30 +02:00

45 lines
1.3 KiB
TypeScript

import {Realm, RealmCreation} from "~/realm/Realm";
import {useGet} from "~/utils/HttpUtils";
import {usePopup} from "~/components/ui/popup/Popup";
export const useRealmStore = defineStore('realms', {
state: () => ({
realms: useGet<Realm[]>("/api/realms").value.data
}),
getters: {
getRealms: (state) => {
return () => {
return state.realms;
}
}
},
actions: {
init()
{
const realms = useGet<Realm[]>("/api/realms");
watchEffect(() => {
this.realms = realms.value.data;
})
},
add(realm: RealmCreation)
{
usePost<Realm, RealmCreation>("/api/realms", realm, (response) => {
this.realms?.push(response);
usePopup().close();
useRouter().push("/realms/" + response.key + '/home');
});
},
delete(realm: Realm)
{
useDelete("/api/realms/" + realm.key, () => {
const index = this.realms?.findIndex(item => item.key === realm.key);
if (index && index >= 0)
{
this.realms?.splice(index, 1);
usePopup().close();
useRouter().push("/");
}
})
}
},
})