partey_workadventure/desktop/local-app/src/store.ts
2022-02-20 20:30:53 +01:00

31 lines
861 B
TypeScript

import { writable, get } from "svelte/store";
type Server = {
_id: string;
name: string;
url: string;
};
export const newServer = writable<Omit<Server, "_id">>({
name: "",
url: "",
});
export const servers = writable<Server[]>([]);
export const selectedServer = writable<string | undefined>(undefined);
export async function selectServer(server: Server) {
await window.WorkAdventureDesktopApi.selectServer(server._id);
selectedServer.set(server._id);
}
export async function addServer(event: Event) {
const addedServer = await window?.WorkAdventureDesktopApi?.addServer(get(newServer));
newServer.set({ name: "", url: "" });
servers.update((s) => [...s, addedServer]);
await selectServer(addedServer);
}
export async function loadServers() {
servers.set(await window.WorkAdventureDesktopApi.getServers());
}