From 6158e82f43cbbb79f0139bd7f6898c3ebba5fcee Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 6 Aug 2024 14:22:24 +0200 Subject: [PATCH] Clan-app: refine create machine workflow via query operation --- pkgs/clan-cli/clan_cli/machines/create.py | 2 + pkgs/webview-ui/app/src/api.ts | 5 +- .../app/src/routes/machines/create.tsx | 106 ++++++++++--- .../app/src/routes/machines/view.tsx | 142 +++++------------- 4 files changed, 126 insertions(+), 129 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index cea2412a7..f5b858602 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -31,6 +31,8 @@ def create_machine(flake: FlakeId, machine: Machine) -> None: if machine.name in full_inventory.machines.keys(): raise ClanError(f"Machine with the name {machine.name} already exists") + print(f"Define machine {machine.name}", machine) + inventory.machines.update({machine.name: machine}) save_inventory(inventory, flake.path, f"Create machine {machine.name}") diff --git a/pkgs/webview-ui/app/src/api.ts b/pkgs/webview-ui/app/src/api.ts index 1ced2a193..f12d95c8e 100644 --- a/pkgs/webview-ui/app/src/api.ts +++ b/pkgs/webview-ui/app/src/api.ts @@ -120,13 +120,10 @@ export const callApi = ( method: K, args: OperationArgs, ) => { - return new Promise>((resolve, reject) => { + return new Promise>((resolve) => { const id = nanoid(); pyApi[method].receive((response) => { console.log("Received response: ", { response }); - if (response.status === "error") { - reject(response); - } resolve(response); }, id); diff --git a/pkgs/webview-ui/app/src/routes/machines/create.tsx b/pkgs/webview-ui/app/src/routes/machines/create.tsx index 0a379c0b1..7ab203d6c 100644 --- a/pkgs/webview-ui/app/src/routes/machines/create.tsx +++ b/pkgs/webview-ui/app/src/routes/machines/create.tsx @@ -1,36 +1,69 @@ -import { callApi, OperationArgs, pyApi } from "@/src/api"; -import { activeURI } from "@/src/App"; -import { createForm, required } from "@modular-forms/solid"; +import { callApi, OperationArgs, pyApi, OperationResponse } from "@/src/api"; +import { activeURI, setRoute } from "@/src/App"; +import { createForm, required, reset } from "@modular-forms/solid"; +import { createQuery } from "@tanstack/solid-query"; +import { Match, Switch } from "solid-js"; import toast from "solid-toast"; type CreateMachineForm = OperationArgs<"create_machine">; export function CreateMachine() { - const [formStore, { Form, Field }] = createForm({}); + const [formStore, { Form, Field }] = createForm({ + initialValues: { + flake: { + loc: activeURI() || "", + }, + machine: { + deploy: { + targetHost: "", + }, + name: "", + description: "", + }, + }, + }); + + const { refetch: refetchMachines } = createQuery(() => ({ + queryKey: [activeURI(), "list_inventory_machines"], + })); const handleSubmit = async (values: CreateMachineForm) => { const active_dir = activeURI(); if (!active_dir) { - toast.error("Open a clan to create the machine in"); + toast.error("Open a clan to create the machine within"); return; } - callApi("create_machine", { + console.log("submitting", values); + + const response = await callApi("create_machine", { + ...values, flake: { loc: active_dir, }, - machine: { - name: "jon", - deploy: { - targetHost: null, - }, - }, }); - console.log("submit", values); + + if (response.status === "success") { + toast.success(`Successfully created ${values.machine.name}`); + reset(formStore); + refetchMachines(); + setRoute("machines"); + } else { + toast.error( + `Error: ${response.errors[0].message}. Machine ${values.machine.name} could not be created`, + ); + } }; return (
Create new Machine +
{(field, props) => ( <> -