Merge pull request 'fix(ui): remove empty strings from add machine api call' (#5066) from ui/refine-add-machine-api-call into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/5066
This commit is contained in:
brianmcgee
2025-09-02 14:43:57 +00:00
3 changed files with 43 additions and 6 deletions

View File

@@ -6,3 +6,35 @@ export const pick = <T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> =>
}, },
{} as Pick<T, K>, {} as Pick<T, K>,
); );
export const removeEmptyStrings = <T>(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;
};

View File

@@ -41,7 +41,7 @@ export interface AddMachineProps {
export interface AddMachineStoreType { export interface AddMachineStoreType {
general: GeneralForm; general: GeneralForm;
deploy: { deploy: {
targetHost: string; targetHost?: string;
}; };
tags: { tags: {
tags: string[]; tags: string[];

View File

@@ -16,6 +16,7 @@ import { MachineTags } from "@/src/components/Form/MachineTags";
import { Button } from "@/src/components/Button/Button"; import { Button } from "@/src/components/Button/Button";
import { useApiClient } from "@/src/hooks/ApiClient"; import { useApiClient } from "@/src/hooks/ApiClient";
import { useClanURI } from "@/src/hooks/clan"; import { useClanURI } from "@/src/hooks/clan";
import { removeEmptyStrings } from "@/src/util";
const TagsSchema = v.object({ const TagsSchema = v.object({
tags: v.array(v.string()), tags: v.array(v.string()),
@@ -41,16 +42,20 @@ export const StepTags = (props: { onDone: () => void }) => {
...values, ...values,
})); }));
const machine = removeEmptyStrings({
...store.general,
...store.tags,
deploy: store.deploy,
});
console.log("machine", machine);
const call = apiClient.fetch("create_machine", { const call = apiClient.fetch("create_machine", {
opts: { opts: {
clan_dir: { clan_dir: {
identifier: clanURI, identifier: clanURI,
}, },
machine: { machine,
...store.general,
...store.tags,
deploy: store.deploy,
},
}, },
}); });