Skip to content

Commit

Permalink
Merge pull request #1407 from AmruthPillai/fix/id-null-when-section-d…
Browse files Browse the repository at this point in the history
…uplicated-cloned

Fix issue when a section is duplicated/cloned, ID is null
  • Loading branch information
AmruthPillai authored Jul 27, 2023
2 parents b53d885 + f9a1109 commit 7465a7e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions client/templates/sectionMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const sectionMap = (Section: React.FC<SectionProps>): Record<string, JSX.Element
references: <Section key="references" path="sections.references" titlePath="name" subtitlePath="relationship" />,
});

export const getSectionById = (id: string, Section: React.FC<SectionProps>): JSX.Element => {
// Check if section id is a custom section (an uuid)
if (validate(id)) {
return <Section key={id} path={`sections.${id}`} />;
}
export const getSectionById = (id: string, Section: React.FC<SectionProps>): JSX.Element | null => {
if (!id) return null;

// Check if section id is a custom section (is a valid uuid)
if (validate(id)) return <Section key={id} path={`sections.${id}`} />;

// Check if section id is a predefined seciton in config
const predefinedSection = get(sectionMap(Section), id);
Expand All @@ -57,9 +57,11 @@ export const getSectionById = (id: string, Section: React.FC<SectionProps>): JSX
return predefinedSection;
}

// Other ways section should be a cloned section
const section = find(sectionMap(Section), (element, key) => id.includes(key));
return React.cloneElement(section!, { path: `sections.${id}` });
// Otherwise, section must be a cloned section
const section = find(sectionMap(Section), (_element, key) => id.includes(key));
if (section) return React.cloneElement(section, { path: `sections.${id}` });

return null;
};

export default sectionMap;

0 comments on commit 7465a7e

Please sign in to comment.