diff --git a/pkgs/clan-app/ui/src/util.ts b/pkgs/clan-app/ui/src/util.ts index 38718f7af..f2055a48e 100644 --- a/pkgs/clan-app/ui/src/util.ts +++ b/pkgs/clan-app/ui/src/util.ts @@ -6,3 +6,35 @@ export const pick = (obj: T, keys: K[]): Pick => }, {} as Pick, ); + +export const removeEmptyStrings = (obj: T): T => { + if (obj === null || obj === undefined) { + return obj; + } + + if (typeof obj === "string") { + return obj; + } + + if (Array.isArray(obj)) { + return obj.map((item) => removeEmptyStrings(item)) as T; + } + + if (typeof obj === "object") { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const result: any = {}; + for (const key in obj) { + // eslint-disable-next-line no-prototype-builtins + if (obj.hasOwnProperty(key)) { + const value = obj[key]; + if (value !== "") { + result[key] = removeEmptyStrings(value); + } + } + } + + return result; + } + + return obj; +}; diff --git a/pkgs/clan-app/ui/src/workflows/AddMachine/AddMachine.tsx b/pkgs/clan-app/ui/src/workflows/AddMachine/AddMachine.tsx index 1676cbcab..585514903 100644 --- a/pkgs/clan-app/ui/src/workflows/AddMachine/AddMachine.tsx +++ b/pkgs/clan-app/ui/src/workflows/AddMachine/AddMachine.tsx @@ -41,7 +41,7 @@ export interface AddMachineProps { export interface AddMachineStoreType { general: GeneralForm; deploy: { - targetHost: string; + targetHost?: string; }; tags: { tags: string[]; diff --git a/pkgs/clan-app/ui/src/workflows/AddMachine/StepTags.tsx b/pkgs/clan-app/ui/src/workflows/AddMachine/StepTags.tsx index 4cc0fb840..1cdbb3f5b 100644 --- a/pkgs/clan-app/ui/src/workflows/AddMachine/StepTags.tsx +++ b/pkgs/clan-app/ui/src/workflows/AddMachine/StepTags.tsx @@ -16,6 +16,7 @@ import { MachineTags } from "@/src/components/Form/MachineTags"; import { Button } from "@/src/components/Button/Button"; import { useApiClient } from "@/src/hooks/ApiClient"; import { useClanURI } from "@/src/hooks/clan"; +import { removeEmptyStrings } from "@/src/util"; const TagsSchema = v.object({ tags: v.array(v.string()), @@ -41,16 +42,20 @@ export const StepTags = (props: { onDone: () => void }) => { ...values, })); + const machine = removeEmptyStrings({ + ...store.general, + ...store.tags, + deploy: store.deploy, + }); + + console.log("machine", machine); + const call = apiClient.fetch("create_machine", { opts: { clan_dir: { identifier: clanURI, }, - machine: { - ...store.general, - ...store.tags, - deploy: store.deploy, - }, + machine, }, });