-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: types for componentsbuilder files
- Loading branch information
Showing
1 changed file
with
54 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,76 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck | ||
type Component = { | ||
__component: string; | ||
[key: string]: unknown; | ||
}; | ||
|
||
type Section = { | ||
id: number; | ||
type: string; | ||
elements_par_ligne: 'Deux' | 'Trois'; | ||
espacement_bas: string; | ||
[key: string]: unknown; | ||
}; | ||
|
||
type BuildComponentResult = { | ||
type: string; | ||
[key: string]: unknown; | ||
}; | ||
|
||
type BuildSectionResult = { | ||
rows: number; | ||
elements: Section[]; | ||
sectionId: number; | ||
espacement_bas: string; | ||
}; | ||
|
||
type BuildSectionsResult = [BuildSectionResult[], Section[]]; | ||
|
||
const rows = { | ||
const rows: Record<'Deux' | 'Trois', number> = { | ||
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[]): BuildComponentResult[] => { | ||
return components.map((c) => { | ||
const json: BuildComponentResult = { type: c.__component.split('.')[1] }; | ||
|
||
for (const key in c) { | ||
if (key !== '__component') { | ||
json[key] = c[key]; | ||
} | ||
} | ||
|
||
return json; | ||
}); | ||
return arr; | ||
}; | ||
|
||
export const buildSections = (array) => { | ||
const resp = []; | ||
const elementsToSplice = []; | ||
export const buildSections = (array: Section[]): BuildSectionsResult => { | ||
const resp: BuildSectionResult[] = []; | ||
const elementsToSplice: number[] = []; | ||
|
||
array.forEach((el, index) => { | ||
const temp = []; | ||
if (el.type == 'section') { | ||
if (el.type === 'section') { | ||
const rowsCount = rows[el.elements_par_ligne]; | ||
const espacement_bas = el.espacement_bas; | ||
const temp: Section[] = []; | ||
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 = array.filter((_, i) => !elementsToSplice.includes(i)); | ||
|
||
return [resp, arrayResp]; | ||
}; |