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);
|
const blocksIdsToRemove = childrenBlocks.map((b) => b.id);
|
||||||
await removeBlocks(notionClient, blocksIdsToRemove);
|
await removeBlocks(notionClient, blocksIdsToRemove);
|
||||||
|
|
||||||
await notionClient.blocks.children.append({
|
const useLayeredAppend = true;
|
||||||
block_id: notionPageId,
|
if (useLayeredAppend) {
|
||||||
children: [...newBlocks],
|
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 { filterFamillesWithOneOfEvenementsOfType } from "../filterFamillesWithOneOfEvenementsOfType";
|
||||||
import { StatsPenales } from "./StatsPenales";
|
import { StatsPenales } from "./StatsPenales";
|
||||||
import { nbFamillesAvecPagesLiees } from "./nbFamillesAvecPagesLiees";
|
import { nbFamillesAvecPagesLiees } from "./nbFamillesAvecPagesLiees";
|
||||||
import { StatsData } from "../desc/StatsDesc";
|
|
||||||
|
|
||||||
type FamilleAvecInfoTribunaux = Famille & {
|
type FamilleAvecInfoTribunaux = Famille & {
|
||||||
evtTribunal1?: EvenementFamille;
|
evtTribunal1?: EvenementFamille;
|
||||||
|
|
@ -148,9 +147,9 @@ function computeCrpc(
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
nbFamilles: nbFamilles(famillesConcernees),
|
nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees),
|
||||||
acceptees: nbFamilles(acceptees),
|
acceptees: nbFamillesAvecPagesLiees(acceptees),
|
||||||
refusees: nbFamilles(refusees),
|
refusees: nbFamillesAvecPagesLiees(refusees),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -171,9 +170,9 @@ function computeCompositionPenales(
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
nbFamilles: nbFamilles(famillesConcernees),
|
nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees),
|
||||||
acceptees: nbFamilles(acceptees),
|
acceptees: nbFamillesAvecPagesLiees(acceptees),
|
||||||
refusees: nbFamilles(refusees),
|
refusees: nbFamillesAvecPagesLiees(refusees),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -284,8 +283,4 @@ function computeIntervalProcureurTribunalCorrectionnel(
|
||||||
|
|
||||||
function isTribunalCorrectionnel(e: EvenementFamille): boolean {
|
function isTribunalCorrectionnel(e: EvenementFamille): boolean {
|
||||||
return e.Type === "Tribunal correctionnel";
|
return e.Type === "Tribunal correctionnel";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function nbFamilles(familles: Famille[]): StatsData {
|
|
||||||
return familles.length;
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue