fix(web): VisibilityBadge typed to the union (#38); normalize localized_text to default language on save (#41)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 15:46:48 +02:00
parent 0a29127f7e
commit b4d71b0f80
4 changed files with 83 additions and 27 deletions
+8 -24
View File
@@ -3,7 +3,9 @@ import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useFieldDefinitions } from "../api/queries";
import { useConfig } from "../config/config-context";
import { FieldInput } from "./field-input";
import { pruneFields } from "./prune-fields";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
@@ -58,9 +60,14 @@ export function ObjectForm({
fieldErrorKey?: string | null;
}) {
const { t } = useTranslation();
const { default_language } = useConfig();
const { data: definitions } = useFieldDefinitions();
const localizedTextKeys = new Set(
(definitions ?? []).filter((d) => d.data_type === "localized_text").map((d) => d.key),
);
const form = useForm<FormShape>({
defaultValues: {
core: defaults?.core ?? EMPTY_CORE,
@@ -81,7 +88,7 @@ export function ObjectForm({
}, [fieldErrorKey, form, t]);
const submit = handleSubmit((data) => {
const fields = pruneFields(data.fields);
const fields = pruneFields(data.fields, localizedTextKeys, default_language);
onSubmit(
mode === "create"
@@ -182,26 +189,3 @@ export function ObjectForm({
);
}
function pruneFields(fields: Record<string, unknown>): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const [key, value] of Object.entries(fields)) {
if (value === undefined || value === null || value === "") continue;
if (typeof value === "object" && !Array.isArray(value)) {
const inner = Object.fromEntries(
Object.entries(value as Record<string, unknown>).filter(
([, v]) => v !== undefined && v !== null && v !== "",
),
);
if (Object.keys(inner).length > 0) out[key] = inner;
continue;
}
out[key] = value;
}
return out;
}