UI: fix breakage in disk id api

This commit is contained in:
Johannes Kirschbauer
2024-09-13 16:44:35 +02:00
parent 95f587d652
commit 2953aefe9d
4 changed files with 51 additions and 16 deletions

View File

@@ -1,20 +1,26 @@
import { QueryClient } from "@tanstack/query-core";
import { get_inventory } from "./inventory"; import { get_inventory } from "./inventory";
export const instance_name = (machine_name: string) => export const instance_name = (machine_name: string) =>
`${machine_name}-single-disk` as const; `${machine_name}-single-disk` as const;
export async function set_single_disk_id( export async function set_single_disk_id(
client: QueryClient,
base_path: string, base_path: string,
machine_name: string, machine_name: string,
disk_id: string, disk_id: string,
) { ) {
const inventory = await get_inventory(base_path); const r = await get_inventory(client, base_path);
if (!inventory.services) { if (r.status === "error") {
return r;
}
if (!r.data.services) {
return new Error("No services found in inventory"); return new Error("No services found in inventory");
} }
if (!inventory.services["single-disk"]) { const inventory = r.data;
inventory.services["single-disk"] = {}; inventory.services = inventory.services || {};
} inventory.services["single-disk"] = inventory.services["single-disk"] || {};
inventory.services["single-disk"][instance_name(machine_name)] = { inventory.services["single-disk"][instance_name(machine_name)] = {
meta: { meta: {
name: instance_name(machine_name), name: instance_name(machine_name),

View File

@@ -1,5 +1,5 @@
import schema from "@/api/API.json" assert { type: "json" }; import schema from "@/api/API.json" assert { type: "json" };
import { API } from "@/api/API"; import { API, Error } from "@/api/API";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { Schema as Inventory } from "@/api/Inventory"; import { Schema as Inventory } from "@/api/Inventory";
@@ -7,6 +7,14 @@ export type OperationNames = keyof API;
export type OperationArgs<T extends OperationNames> = API[T]["arguments"]; export type OperationArgs<T extends OperationNames> = API[T]["arguments"];
export type OperationResponse<T extends OperationNames> = API[T]["return"]; export type OperationResponse<T extends OperationNames> = API[T]["return"];
export type ApiEnvelope<T> =
| {
status: "success";
data: T;
op_key: string;
}
| Error;
export type Services = NonNullable<Inventory["services"]>; export type Services = NonNullable<Inventory["services"]>;
export type ServiceNames = keyof Services; export type ServiceNames = keyof Services;
export type ClanService<T extends ServiceNames> = Services[T]; export type ClanService<T extends ServiceNames> = Services[T];

View File

@@ -1,12 +1,21 @@
import { QueryClient } from "@tanstack/solid-query"; import { QueryClient } from "@tanstack/solid-query";
import { callApi, ClanServiceInstance, ServiceNames, Services } from "."; import {
ApiEnvelope,
callApi,
ClanServiceInstance,
ServiceNames,
Services,
} from ".";
import { Schema as Inventory } from "@/api/Inventory";
export async function get_inventory(client: QueryClient, base_path: string) { export async function get_inventory(client: QueryClient, base_path: string) {
const data = await client.ensureQueryData({ const data = await client.ensureQueryData({
queryKey: [base_path, "inventory"], queryKey: [base_path, "inventory"],
queryFn: () => { queryFn: () => {
console.log("Refreshing inventory"); console.log("Refreshing inventory");
return callApi("get_inventory", { base_path }); return callApi("get_inventory", { base_path }) as Promise<
ApiEnvelope<Inventory>
>;
}, },
revalidateIfStale: true, revalidateIfStale: true,
staleTime: 60 * 1000, staleTime: 60 * 1000,
@@ -51,7 +60,7 @@ export async function get_single_service<T extends keyof Services>(
client: QueryClient, client: QueryClient,
base_path: string, base_path: string,
service_name: T, service_name: T,
) { ): Promise<ClanServiceInstance<T>> {
const instance_key = await get_first_instance_name( const instance_key = await get_first_instance_name(
client, client,
base_path, base_path,
@@ -59,7 +68,7 @@ export async function get_single_service<T extends keyof Services>(
); );
if (!instance_key) { if (!instance_key) {
return {}; throw new Error("No instance found");
} }
const service: Services[T] | null = await get_service( const service: Services[T] | null = await get_service(
client, client,
@@ -67,10 +76,10 @@ export async function get_single_service<T extends keyof Services>(
service_name, service_name,
); );
if (service) { if (service) {
const clanServiceInstance = service[instance_key]; const clanServiceInstance = service[instance_key] as ClanServiceInstance<T>;
return clanServiceInstance || {}; return clanServiceInstance;
} }
return {}; throw new Error("No service found");
} }
export async function set_single_service<T extends keyof Services>( export async function set_single_service<T extends keyof Services>(
@@ -88,10 +97,12 @@ export async function set_single_service<T extends keyof Services>(
const inventory = r.data; const inventory = r.data;
inventory.services = inventory.services || {}; inventory.services = inventory.services || {};
inventory.services[service_name] = inventory.services[service_name] || {}; inventory.services[service_name] = inventory.services[service_name] || {};
// @ts-expect-error: This doesn't check
// @ts-expect-error: This cannot be undefined, because of the line above
inventory.services[service_name][instance_key] = service_config; inventory.services[service_name][instance_key] = service_config;
console.log("saving inventory", inventory); console.log("saving inventory", inventory);
return callApi("set_inventory", { return callApi("set_inventory", {
// @ts-expect-error: This doesn't check
inventory, inventory,
message: `update_single_service ${service_name}`, message: `update_single_service ${service_name}`,
flake_dir: base_path, flake_dir: base_path,

View File

@@ -21,7 +21,11 @@ import {
setValue, setValue,
} from "@modular-forms/solid"; } from "@modular-forms/solid";
import { useParams } from "@solidjs/router"; import { useParams } from "@solidjs/router";
import { createQuery, QueryObserver } from "@tanstack/solid-query"; import {
createQuery,
QueryObserver,
useQueryClient,
} from "@tanstack/solid-query";
import { import {
createSignal, createSignal,
For, For,
@@ -115,6 +119,7 @@ const InstallMachine = (props: InstallMachineProps) => {
toast.success("Machine installed successfully"); toast.success("Machine installed successfully");
} }
}; };
const queryClient = useQueryClient();
const handleDiskConfirm = async (e: Event) => { const handleDiskConfirm = async (e: Event) => {
e.preventDefault(); e.preventDefault();
@@ -125,7 +130,12 @@ const InstallMachine = (props: InstallMachineProps) => {
return; return;
} }
const r = await set_single_disk_id(curr_uri, props.name, disk_id); const r = await set_single_disk_id(
queryClient,
curr_uri,
props.name,
disk_id,
);
if (!r) { if (!r) {
toast.success("Disk set successfully"); toast.success("Disk set successfully");
setConfirmDisk(true); setConfirmDisk(true);