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:
@@ -6,3 +6,35 @@ export const pick = <T, K extends keyof T>(obj: T, keys: K[]): 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;
|
||||
};
|
||||
|
||||
@@ -41,7 +41,7 @@ export interface AddMachineProps {
|
||||
export interface AddMachineStoreType {
|
||||
general: GeneralForm;
|
||||
deploy: {
|
||||
targetHost: string;
|
||||
targetHost?: string;
|
||||
};
|
||||
tags: {
|
||||
tags: string[];
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user