partey_workadventure/desktop/local-app/src/store.ts

29 lines
823 B
TypeScript
Raw Normal View History

2022-02-20 20:30:53 +01:00
import { writable, get } from "svelte/store";
2022-02-22 12:02:56 +01:00
import { api, Server } from "~/lib/ipc";
2022-02-20 20:30:53 +01:00
export const newServer = writable<Omit<Server, "_id">>({
name: "",
url: "",
});
export const servers = writable<Server[]>([]);
2022-02-22 16:57:56 +01:00
export const selectedServer = writable<string>("");
2022-02-20 20:30:53 +01:00
2022-02-22 16:57:56 +01:00
export async function selectServer(serverId: string) {
await api.selectServer(serverId);
selectedServer.set(serverId);
2022-02-20 20:30:53 +01:00
}
2022-02-22 10:05:21 +01:00
export async function addServer() {
2022-02-22 12:02:56 +01:00
const addedServer = await api.addServer(get(newServer));
if (addedServer instanceof Error) {
throw new Error(addedServer as unknown as string);
2022-02-22 10:05:21 +01:00
}
2022-02-20 20:30:53 +01:00
newServer.set({ name: "", url: "" });
servers.update((s) => [...s, addedServer]);
2022-02-22 16:57:56 +01:00
await selectServer(addedServer._id);
2022-02-20 20:30:53 +01:00
}
export async function loadServers() {
2022-02-22 12:02:56 +01:00
servers.set(await api.getServers());
2022-02-20 20:30:53 +01:00
}