Skip to content

Commit

Permalink
feat: add descriptionFields labels
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusdemoura committed Jan 16, 2025
1 parent d390019 commit e449399
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/imports/lib/buildI18N/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { MetaObject } from '../../model/MetaObject';
import { User } from '../../model/User';
import { MetaObjectType } from '../../types/metadata';
import { getAccessFor } from '../../utils/accessUtils';
import { Field } from '@imports/model/Field';

const negative: Label = {
en: 'Not',
de: 'Nicht',
Expand Down Expand Up @@ -78,11 +80,43 @@ export const buildI18N = async (user: User): Promise<Record<string, unknown>> =>
const label = get(fieldLabel, 'label');
const options = get(fieldLabel, 'options');
const type = get(fieldLabel, 'type');
if (metaProp === 'fields' && options != null) {
Object.entries(options).forEach(entry => {
const [option, optionLabels] = entry as [string, Label];
Object.entries(optionLabels).forEach(([lang, value]) => set(acc, [fixISO(lang), ...keyPath, 'options', field, option], value as string));
});
if (metaProp === 'fields') {
if (options != null) {
Object.entries(options).forEach(entry => {
const [option, optionLabels] = entry as [string, Label];
Object.entries(optionLabels).forEach(([lang, value]) => set(acc, [fixISO(lang), ...keyPath, 'options', field, option], value as string));
});
}
if (type === "lookup") {
const findLookupSubField: (lookup: {document: string, name: string}) => Field | null = ({document, name}) => {
if(name.includes('.')) {
const [subField, ... fieldParts] = name.split('.');
const subFieldDoc = findLookupSubField({document, name: subField});
return findLookupSubField({document: subFieldDoc?.document ?? '', name: fieldParts.join('.')});
}
const lookupMeta = metas.find(meta => meta.name === document) ?? {};
const subFieldLabel = get(lookupMeta, `fields.${name}`, null);
return subFieldLabel as Field | null;
}
const fieldDocument = get(fieldLabel, 'document');
const descriptionFields = get(fieldLabel, 'descriptionFields', []) as string[];
const recursiveDescriptionFields = descriptionFields.reduce((acc, field) => {
const fieldParts = field.split('.');
const recursiveParts = fieldParts.map((part, index) => fieldParts.slice(0, index + 1).join('.'));
return [...acc, ...recursiveParts];
}, [] as string[]);
if (fieldDocument != null && Array.isArray(recursiveDescriptionFields)) {
recursiveDescriptionFields.forEach(subField => {
const subFieldLabel = findLookupSubField({document: fieldDocument, name: subField});
if(subFieldLabel != null) {
const label: Label = get(subFieldLabel, 'label', {});
Object.entries(label).forEach(([lang, value]) => set(acc, [fixISO(lang), ...keyPath, 'fields', `${field}.${subField}`], value as string));
}
});
}
}


}
if (label != null) {
Object.entries(label).forEach(([lang, value]) => {
Expand Down

0 comments on commit e449399

Please sign in to comment.