Skip to content

Commit

Permalink
ref(KUI-1207): AllSections component
Browse files Browse the repository at this point in the history
  • Loading branch information
belanglos committed Feb 28, 2024
1 parent e6d9976 commit 38b9d37
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
20 changes: 6 additions & 14 deletions public/js/app/components/ContentFromNewSectionEditor.jsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import React from 'react'
import i18n from '../../../../i18n'
import { useWebContext } from '../context/WebContext'
import { ExtraHeaderHead } from './ContentHead'
import HtmlWrapper from './HtmlWrapper'
import { useWebContext } from '../context/WebContext'

function ContentFromNewSectionEditor(props) {
function ContentFromNewSectionEditor({ htmlContent = '', title = '', isEmptyNew = false, visibleInMemo }) {
const [webContext] = useWebContext()
const { userLanguageIndex = 1 } = webContext
const {
initialValue: contentFromEditor = '',
initialTitle: contentForTitle = '',
isEmptyNew = false,
visibleInMemo,
} = props

const { sourceInfo } = i18n.messages[userLanguageIndex]

return (
<article className="Add--New--Title--And--Info" aria-label={contentForTitle}>
{!isEmptyNew && <ExtraHeaderHead header={contentForTitle} />}
<article aria-label={title}>
{!isEmptyNew && <ExtraHeaderHead header={title} />}

{!isEmptyNew &&
/* is included in memo, preview text without editor */
((visibleInMemo && (
<HtmlWrapper
html={(contentFromEditor !== '' && contentFromEditor) || `<p><i>${sourceInfo.noInfoYet}</i></p>`}
/>
<HtmlWrapper html={(htmlContent !== '' && htmlContent) || `<p><i>${sourceInfo.noInfoYet}</i></p>`} />
)) ||
/* editor has content but is not yet included in pm */
(contentFromEditor !== '' && (
(htmlContent !== '' && (
<span>
<p>
<i>{sourceInfo.noInfoYet}</i>
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/components/Section.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { ContentHead, SubSectionHeaderMessage } from './ContentHead'
import i18n from '../../../../i18n'
import { context } from '../util/fieldsByType'
import { ContentHead, SubSectionHeaderMessage } from './ContentHead'
import HtmlWrapper from './HtmlWrapper'

const Section = ({ contentId, menuId, visibleInMemo, html, memoLangIndex = 0 /* en */ }) => {
Expand Down
51 changes: 47 additions & 4 deletions public/js/app/util/AllSectionsUtils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
const sectionAllowedToBeShownEvenIfNoContent = (isRequired, type) => {
export const headingAllowedToBeShownEvenIfNoContent = (isRequired, type) => {
const mandatoryTypes = ['mandatory', 'mandatoryAndEditable']
return isRequired && mandatoryTypes.includes(type)
}

const sectionHasContent = contentHtml => contentHtml !== undefined && contentHtml !== ''
const headingHasContent = contentHtml => contentHtml !== undefined && contentHtml !== ''

export const sectionShouldBeShown = ({ isRequired, type, contentHtml, visibleInMemo = true }) => {
if (sectionHasContent(contentHtml) || sectionAllowedToBeShownEvenIfNoContent(isRequired, type)) {
export const headingShouldBeShown = ({ isRequired, type, contentHtml, visibleInMemo = true }) => {
if (headingHasContent(contentHtml) || headingAllowedToBeShownEvenIfNoContent(isRequired, type)) {
return visibleInMemo
}

return false
}

const headingHasAtLeastOneVisibleExtraHeader = (extraHeaderTitle, extraHeaderTitles) => {
if (extraHeaderTitle && extraHeaderTitles && Array.isArray(extraHeaderTitles)) {
return extraHeaderTitles.some(({ visibleInMemo }) => visibleInMemo)
}

return false
}

const sectionsToSkip = ['contacts']

export const getAllSectionsAndHeadingsToShow = ({ sections, context, memoData }) => {
const sectionsAndHeadings = []
sections
.filter(({ id }) => !sectionsToSkip.includes(id))
.forEach(({ id, content, extraHeaderTitle }) => {
sectionsAndHeadings[id] = []
const headings = []

content.forEach(contentId => {
const { isRequired, type } = context[contentId]
const contentHtml = memoData[contentId]
const visibleInMemo = memoData.visibleInMemo[contentId]

if (headingShouldBeShown({ isRequired, type, contentHtml, visibleInMemo })) {
headings.push(contentId)
}
})

const hasExtraHeading = headingHasAtLeastOneVisibleExtraHeader(extraHeaderTitle, memoData[extraHeaderTitle])

const hasHeadingOrExtraHeading = headings.length > 0 || hasExtraHeading

sectionsAndHeadings.push({
id,
headings,
hasHeadingOrExtraHeading,
extraHeaderTitle: hasExtraHeading ? extraHeaderTitle : undefined,
})
})

return sectionsAndHeadings
}

0 comments on commit 38b9d37

Please sign in to comment.