|
@@ -1,8 +1,8 @@
|
|
|
-import { getOperationVariablesFields } from '@/framework/internal/document-introspection/get-document-structure.js';
|
|
|
|
|
|
|
+import { getOperationVariablesFields } from '@/framework/document-introspection/get-document-structure.js';
|
|
|
import {
|
|
import {
|
|
|
createFormSchemaFromFields,
|
|
createFormSchemaFromFields,
|
|
|
getDefaultValuesFromFields,
|
|
getDefaultValuesFromFields,
|
|
|
-} from '@/framework/internal/form-engine/form-schema-tools.js';
|
|
|
|
|
|
|
+} from '@/framework/form-engine/form-schema-tools.js';
|
|
|
import { useServerConfig } from '@/providers/server-config.js';
|
|
import { useServerConfig } from '@/providers/server-config.js';
|
|
|
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
@@ -44,7 +44,7 @@ export function useGeneratedForm<
|
|
|
const schema = createFormSchemaFromFields(updateFields);
|
|
const schema = createFormSchemaFromFields(updateFields);
|
|
|
const defaultValues = getDefaultValuesFromFields(updateFields);
|
|
const defaultValues = getDefaultValuesFromFields(updateFields);
|
|
|
const processedEntity = ensureTranslationsForAllLanguages(entity, availableLanguages);
|
|
const processedEntity = ensureTranslationsForAllLanguages(entity, availableLanguages);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const form = useForm({
|
|
const form = useForm({
|
|
|
resolver: zodResolver(schema),
|
|
resolver: zodResolver(schema),
|
|
|
defaultValues,
|
|
defaultValues,
|
|
@@ -62,50 +62,52 @@ export function useGeneratedForm<
|
|
|
return { form, submitHandler };
|
|
return { form, submitHandler };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* Ensures that an entity with translations has entries for all available languages.
|
|
* Ensures that an entity with translations has entries for all available languages.
|
|
|
* If a language is missing, it creates an empty translation based on the structure of existing translations.
|
|
* If a language is missing, it creates an empty translation based on the structure of existing translations.
|
|
|
*/
|
|
*/
|
|
|
function ensureTranslationsForAllLanguages<E extends Record<string, any>>(
|
|
function ensureTranslationsForAllLanguages<E extends Record<string, any>>(
|
|
|
entity: E | null | undefined,
|
|
entity: E | null | undefined,
|
|
|
- availableLanguages: string[] = []
|
|
|
|
|
|
|
+ availableLanguages: string[] = [],
|
|
|
): E | null | undefined {
|
|
): E | null | undefined {
|
|
|
- if (!entity || !('translations' in entity) || !Array.isArray((entity as any).translations) || !availableLanguages.length) {
|
|
|
|
|
|
|
+ if (
|
|
|
|
|
+ !entity ||
|
|
|
|
|
+ !('translations' in entity) ||
|
|
|
|
|
+ !Array.isArray((entity as any).translations) ||
|
|
|
|
|
+ !availableLanguages.length
|
|
|
|
|
+ ) {
|
|
|
return entity;
|
|
return entity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Create a deep copy of the entity to avoid mutation
|
|
// Create a deep copy of the entity to avoid mutation
|
|
|
const processedEntity = { ...entity } as any;
|
|
const processedEntity = { ...entity } as any;
|
|
|
const translations = [...(processedEntity.translations || [])];
|
|
const translations = [...(processedEntity.translations || [])];
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Get existing language codes
|
|
// Get existing language codes
|
|
|
- const existingLanguageCodes = new Set(
|
|
|
|
|
- translations.map((t: any) => t.languageCode)
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
|
|
+ const existingLanguageCodes = new Set(translations.map((t: any) => t.languageCode));
|
|
|
|
|
+
|
|
|
// Add missing language translations
|
|
// Add missing language translations
|
|
|
for (const langCode of availableLanguages) {
|
|
for (const langCode of availableLanguages) {
|
|
|
if (!existingLanguageCodes.has(langCode)) {
|
|
if (!existingLanguageCodes.has(langCode)) {
|
|
|
// Find a translation to use as template for field structure
|
|
// Find a translation to use as template for field structure
|
|
|
const template = translations[0] || {};
|
|
const template = translations[0] || {};
|
|
|
- const emptyTranslation: Record<string, any> = {
|
|
|
|
|
- languageCode: langCode
|
|
|
|
|
|
|
+ const emptyTranslation: Record<string, any> = {
|
|
|
|
|
+ languageCode: langCode,
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Add empty fields based on template (excluding languageCode)
|
|
// Add empty fields based on template (excluding languageCode)
|
|
|
Object.keys(template).forEach(key => {
|
|
Object.keys(template).forEach(key => {
|
|
|
if (key !== 'languageCode') {
|
|
if (key !== 'languageCode') {
|
|
|
emptyTranslation[key] = '';
|
|
emptyTranslation[key] = '';
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
translations.push(emptyTranslation);
|
|
translations.push(emptyTranslation);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Update the processed entity with complete translations
|
|
// Update the processed entity with complete translations
|
|
|
processedEntity.translations = translations;
|
|
processedEntity.translations = translations;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return processedEntity as E;
|
|
return processedEntity as E;
|
|
|
-}
|
|
|
|
|
|
|
+}
|