Skip to content

Commit

Permalink
feat(contribution): un message block peut être vide (#5486)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot authored Dec 15, 2023
1 parent a5b5014 commit 916cdbc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Props = {
};

export const ContributionMessageBlock = ({ message }: Props) => {
if (!message) {
return <></>;
}
return (
<StyledSection>
<Alert>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Accordion, Table as UITable, theme } from "@socialgouv/cdtn-ui";
import { Accordion, Alert, Table as UITable, theme } from "@socialgouv/cdtn-ui";

import styled from "styled-components";
import { FicheServicePublic } from "../fiche-service-public";
import parse, { domToReact } from "html-react-parser";
import parse, {
DOMNode,
domToReact,
Element,
HTMLReactParserOptions,
} from "html-react-parser";
import { xssWrapper } from "../lib";

export const ContentSP = ({ raw }) => {
Expand All @@ -17,9 +22,9 @@ export const ContentSP = ({ raw }) => {
);
};

const mapItem = (titleLevel, domNode, summary) => ({
body: domToReact(domNode.children, options(titleLevel + 1)),
title: domToReact(summary.children, {
const mapItem = (titleLevel: number, domNode: Element, summary: Element) => ({
body: domToReact(domNode.children as DOMNode[], options(titleLevel + 1)),
title: domToReact(summary.children as DOMNode[], {
transform: (reactNode, domNode) => {
// @ts-ignore
if (domNode.children) {
Expand All @@ -32,79 +37,90 @@ const mapItem = (titleLevel, domNode, summary) => ({
trim: true,
}),
});
const mapToAccordion = (titleLevel, items) => (
const mapToAccordion = (titleLevel: number, items) => (
<StyledAccordion
titleLevel={titleLevel}
data-testid="contrib-accordion"
items={items}
/>
);

function getFirstElementChild(domNode) {
function getFirstElementChild(domNode: Element) {
let child = domNode.children.shift();
while (child && child.type !== "tag") {
child = domNode.children.shift();
}
return child;
}

function getNextFirstElement(domNode) {
function getNextFirstElement(domNode: Element) {
let next = domNode.next;
while (next && next.type !== "tag") {
next = next.next;
}
return next;
}

const mapTbody = (tbody) => {
const mapTbody = (tbody: Element) => {
const tr = getFirstElementChild(tbody);

return (
<UITable>
<thead>{domToReact(tr.children, { trim: true })}</thead>
<tbody>{domToReact(tbody.children, { trim: true })}</tbody>
{tr && (
<thead>{domToReact(tr.children as DOMNode[], { trim: true })}</thead>
)}
<tbody>{domToReact(tbody.children as DOMNode[], { trim: true })}</tbody>
</UITable>
);
};

function getItem(domNode, titleLevel) {
function getItem(domNode: Element, titleLevel: number) {
const summary = getFirstElementChild(domNode);
if (summary && summary.name === "summary") {
return mapItem(titleLevel, domNode, summary);
}
}

const options = (titleLevel) => ({
const options = (titleLevel: number): HTMLReactParserOptions => ({
replace(domNode) {
if (domNode.name === "h3") {
titleLevel = 4;
}
if (domNode.name === "details") {
const items: any[] = [];
const item = getItem(domNode, titleLevel);
if (item) {
items.push(item);
if (domNode instanceof Element) {
if (domNode.name === "h3") {
titleLevel = 4;
}
let next = getNextFirstElement(domNode);
while (next && next.name === "details") {
const item = getItem(next, titleLevel);
if (domNode.name === "details") {
const items: any[] = [];
const item = getItem(domNode, titleLevel);
if (item) {
items.push(item);
}
next = getNextFirstElement(next);
let next = getNextFirstElement(domNode);
while (next && next.name === "details") {
const item = getItem(next, titleLevel);
if (item) {
items.push(item);
}
next = getNextFirstElement(next);
}
return items.length ? mapToAccordion(titleLevel, items) : <></>;
}
return items.length ? mapToAccordion(titleLevel, items) : <></>;
}
if (domNode.name === "table") {
const tableContent = getFirstElementChild(domNode);
if (tableContent.name === "tbody") {
return mapTbody(tableContent);
} else {
return domNode;
if (domNode.name === "table") {
const tableContent = getFirstElementChild(domNode);
if (tableContent?.name === "tbody") {
return mapTbody(tableContent);
} else {
return domNode;
}
}
if (domNode.name === "div" && domNode.attribs.class === "alert") {
return (
<Alert>
{domToReact(domNode.children as DOMNode[], { trim: true })}
</Alert>
);
}
if (domNode.name === "p" && !domNode.children.length) {
return <></>;
}
}
if (domNode.name === "p" && !domNode.children.length) {
return <></>;
}
},
trim: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/code-du-travail-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ type ElasticSearchContributionBase = ElasticSearchItem & {
linkedContent: ContributionLinkedContent[];
references: ContributionRef[];
idcc: string;
messageBlock: string;
messageBlock?: string;
} & (ElasticSearchContributionFicheSp | ElasticSearchContributionContent);

export type ElasticSearchContributionGeneric = ElasticSearchContributionBase & {
Expand Down

0 comments on commit 916cdbc

Please sign in to comment.