From ca04b1ece428851e88e99bbea5063de78cb7f8d1 Mon Sep 17 00:00:00 2001 From: Laura Miguel Date: Mon, 2 Sep 2024 15:12:04 +0200 Subject: [PATCH] fix: restaure componentsbuilder file --- src/functions/componentsbuilder.ts | 77 ++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/src/functions/componentsbuilder.ts b/src/functions/componentsbuilder.ts index 5b80514..e7bc88e 100644 --- a/src/functions/componentsbuilder.ts +++ b/src/functions/componentsbuilder.ts @@ -1,46 +1,71 @@ -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-nocheck +'use client'; -const rows = { +type Rows = { + [key: string]: number; +}; + +type Component = { + __component: string; + [key: string]: string | number | boolean | undefined; +}; + +type SectionElement = { + type: string; + id?: string; + elements_par_ligne?: string; + espacement_bas?: string | number; + [key: string]: string | number | boolean | undefined; +}; + +type Section = { + rows: number; + elements: SectionElement[]; + sectionId: string; + espacement_bas?: string | number; +}; + +const rows: Rows = { Deux: 2, Trois: 3 }; -export const buildComponents = (components) => { - const arr = []; - components.map((c) => { - const obj = []; - const json = {}; - obj.push({ type: c.__component.split('.')[1] }); - for (const key in c) if (key != '__component') obj.push({ [key]: c[key] }); - for (const object of obj) for (const prop in object) json[prop] = object[prop]; - arr.push(json); +export const buildComponents = (components: Component[]): SectionElement[] => { + return components.map((c) => { + const { __component, ...rest } = c; + return { + type: __component.split('.')[1], + ...rest + }; }); - return arr; }; -export const buildSections = (array) => { - const resp = []; - const elementsToSplice = []; +export const buildSections = (array: SectionElement[]): [Section[], SectionElement[]] => { + const resp: Section[] = []; + const elementsToSplice: number[] = []; + array.forEach((el, index) => { - const temp = []; - if (el.type == 'section') { - const rowsCount = rows[el.elements_par_ligne]; - const espacement_bas = el.espacement_bas; + if (el.type === 'section') { + const rowsCount = rows[el.elements_par_ligne ?? ''] || 1; // Default to 1 if not found + const espacement_bas = el.espacement_bas ?? ''; + const temp: SectionElement[] = []; let i = index + 1; - while (i < array.length - 1 && array[i].type != 'fin-de-section') { + + while (i < array.length && array[i].type !== 'fin-de-section') { elementsToSplice.push(i); temp.push(array[i]); i++; } - resp.push({ rows: rowsCount, elements: temp, sectionId: el.id, espacement_bas: espacement_bas }); + + resp.push({ + rows: rowsCount, + elements: temp, + sectionId: el.id ?? '', + espacement_bas + }); } }); - const arrayResp = []; - array.forEach((e, i) => { - if (elementsToSplice.indexOf(i) == -1) arrayResp.push(e); - }); + const arrayResp: SectionElement[] = array.filter((_, i) => !elementsToSplice.includes(i)); return [resp, arrayResp]; };