UI: fix breakage in disk id api

This commit is contained in:
Johannes Kirschbauer
2024-09-13 16:44:35 +02:00
parent 60e4196bb6
commit 04457f1731
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";
export const instance_name = (machine_name: string) =>
`${machine_name}-single-disk` as const;
export async function set_single_disk_id(
client: QueryClient,
base_path: string,
machine_name: string,
disk_id: string,
) {
const inventory = await get_inventory(base_path);
if (!inventory.services) {
const r = await get_inventory(client, base_path);
if (r.status === "error") {
return r;
}
if (!r.data.services) {
return new Error("No services found in inventory");
}
if (!inventory.services["single-disk"]) {
inventory.services["single-disk"] = {};
}
const inventory = r.data;
inventory.services = inventory.services || {};
inventory.services["single-disk"] = inventory.services["single-disk"] || {};
inventory.services["single-disk"][instance_name(machine_name)] = {
meta: {
name: instance_name(machine_name),

View File

@@ -1,5 +1,5 @@
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 { 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 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 ServiceNames = keyof Services;
export type ClanService<T extends ServiceNames> = Services[T];

View File

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

View File

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