mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-12-07 08:23:45 +00:00
feat: support arbitrary block depth in updatePageContent
This commit is contained in:
parent
d2b43f5706
commit
d213191fc3
2 changed files with 80 additions and 15 deletions
|
|
@ -16,8 +16,78 @@ export async function updatePageContent(
|
|||
const blocksIdsToRemove = childrenBlocks.map((b) => b.id);
|
||||
await removeBlocks(notionClient, blocksIdsToRemove);
|
||||
|
||||
const useLayeredAppend = true;
|
||||
if (useLayeredAppend) {
|
||||
await appendBlocksByDepths(notionClient, notionPageId, newBlocks);
|
||||
} else {
|
||||
await notionClient.blocks.children.append({
|
||||
block_id: notionPageId,
|
||||
children: [...newBlocks],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This approach works around the depth limit of Notion API by appending children as a second step.
|
||||
* @param notionClient
|
||||
* @param parentBlockId
|
||||
* @param blocksToAppend
|
||||
*/
|
||||
async function appendBlocksByDepths(notionClient: Client,
|
||||
parentBlockId: string,
|
||||
blocksToAppend: BlockObjectRequest[]
|
||||
) {
|
||||
const { blocksWithoutChildren, extractedChildren } = extractChildren(blocksToAppend);
|
||||
|
||||
const appendResponse = await notionClient.blocks.children.append({
|
||||
block_id: parentBlockId,
|
||||
children: [...blocksWithoutChildren],
|
||||
});
|
||||
|
||||
appendResponse.results.forEach((b, index) => {
|
||||
if (extractedChildren[index].length !== 0) {
|
||||
appendBlocksByDepths(notionClient, b.id, extractedChildren[index]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function extractChildren(blocks: BlockObjectRequest[]): {
|
||||
blocksWithoutChildren: BlockObjectRequest[],
|
||||
extractedChildren: BlockObjectRequest[][]
|
||||
} {
|
||||
const extractedChildren: BlockObjectRequest[][] = [];
|
||||
const blocksWithoutChildren = blocks.map(b => {
|
||||
if ("bulleted_list_item" in b) {
|
||||
extractedChildren.push(b.bulleted_list_item.children || []);
|
||||
return {
|
||||
...b,
|
||||
bulleted_list_item: {
|
||||
...b.bulleted_list_item,
|
||||
children: []
|
||||
}};
|
||||
} else if ("toggle" in b) {
|
||||
extractedChildren.push(b.toggle.children|| []);
|
||||
return {
|
||||
...b,
|
||||
toggle: {
|
||||
...b.toggle,
|
||||
children: []
|
||||
}};
|
||||
} else if ("paragraph" in b) {
|
||||
extractedChildren.push(b.paragraph.children || []);
|
||||
return {
|
||||
...b,
|
||||
paragraph: {
|
||||
...b.paragraph,
|
||||
children: []
|
||||
}};
|
||||
} else {
|
||||
extractedChildren.push([]);
|
||||
return b;
|
||||
}
|
||||
});
|
||||
return {
|
||||
blocksWithoutChildren,
|
||||
extractedChildren: extractedChildren
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ import { filterFamillesWithOneOfEvenements } from "../filterFamillesWithOneOfEve
|
|||
import { filterFamillesWithOneOfEvenementsOfType } from "../filterFamillesWithOneOfEvenementsOfType";
|
||||
import { StatsPenales } from "./StatsPenales";
|
||||
import { nbFamillesAvecPagesLiees } from "./nbFamillesAvecPagesLiees";
|
||||
import { StatsData } from "../desc/StatsDesc";
|
||||
|
||||
type FamilleAvecInfoTribunaux = Famille & {
|
||||
evtTribunal1?: EvenementFamille;
|
||||
|
|
@ -148,9 +147,9 @@ function computeCrpc(
|
|||
);
|
||||
|
||||
return {
|
||||
nbFamilles: nbFamilles(famillesConcernees),
|
||||
acceptees: nbFamilles(acceptees),
|
||||
refusees: nbFamilles(refusees),
|
||||
nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees),
|
||||
acceptees: nbFamillesAvecPagesLiees(acceptees),
|
||||
refusees: nbFamillesAvecPagesLiees(refusees),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -171,9 +170,9 @@ function computeCompositionPenales(
|
|||
);
|
||||
|
||||
return {
|
||||
nbFamilles: nbFamilles(famillesConcernees),
|
||||
acceptees: nbFamilles(acceptees),
|
||||
refusees: nbFamilles(refusees),
|
||||
nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees),
|
||||
acceptees: nbFamillesAvecPagesLiees(acceptees),
|
||||
refusees: nbFamillesAvecPagesLiees(refusees),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -285,7 +284,3 @@ function computeIntervalProcureurTribunalCorrectionnel(
|
|||
function isTribunalCorrectionnel(e: EvenementFamille): boolean {
|
||||
return e.Type === "Tribunal correctionnel";
|
||||
}
|
||||
|
||||
export function nbFamilles(familles: Famille[]): StatsData {
|
||||
return familles.length;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue