generated from KTH/node-web
-
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.
ref(KUI-1207): AllSections component
- Loading branch information
Showing
3 changed files
with
54 additions
and
19 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
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
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,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 | ||
} |