Skip to content

Commit

Permalink
refactor: shift render svg method out of the render fn
Browse files Browse the repository at this point in the history
  • Loading branch information
phanshiyu committed Apr 22, 2024
1 parent 920dd92 commit 94158e6
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/components/renderer/SvgRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ const fetchSvg = async (svgInDoc: string, abortController: AbortController) => {
return res;
};

const renderSvg = (template: string, document: any) => {
if (template.length === 0) return "";
const compiledTemplate = handlebars.compile(template);
return document.credentialSubject ? compiledTemplate(document.credentialSubject) : compiledTemplate(document);
};

// As specified in - https://w3c-ccg.github.io/vc-render-method/#svgrenderingtemplate2023
export const SVG_RENDERER_TYPE = "SvgRenderingTemplate2023";

Expand Down Expand Up @@ -114,7 +120,7 @@ const SvgRenderer = React.forwardRef<HTMLImageElement, SvgRendererProps>(
const handleValidSvgTemplate = (rawSvgTemplate: string) => {
setToDisplay({
status: "OK",
svgDataUri: `data:image/svg+xml,${encodeURIComponent(renderTemplate(rawSvgTemplate, document))}`,
svgDataUri: `data:image/svg+xml,${encodeURIComponent(renderSvg(rawSvgTemplate, document))}`,
});
};

Expand Down Expand Up @@ -170,12 +176,6 @@ const SvgRenderer = React.forwardRef<HTMLImageElement, SvgRendererProps>(
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [document]);

const renderTemplate = (template: string, document: any) => {
if (template.length === 0) return "";
const compiledTemplate = handlebars.compile(template);
return document.credentialSubject ? compiledTemplate(document.credentialSubject) : compiledTemplate(document);
};

switch (toDisplay.status) {
case "DEFAULT":
return <NoTemplate document={document} handleObfuscation={() => null} />;
Expand Down

0 comments on commit 94158e6

Please sign in to comment.