Skip to content

Commit

Permalink
Display handlebars compile error & prevent saving
Browse files Browse the repository at this point in the history
Signed-off-by: Zvonimir Fras <[email protected]>
  • Loading branch information
zvonimirfras committed Oct 19, 2023
1 parent 9d74f6c commit db48c45
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/components/custom-components-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext, useState } from 'react';
import { Modal } from '@carbon/react';
import Editor from '@monaco-editor/react';
import Handlebars from 'handlebars';
import { GlobalStateContext, ModalContext } from '../context';

export const CustomComponentsModal = () => {
Expand All @@ -14,7 +15,16 @@ export const CustomComponentsModal = () => {
try {
if (modelString) {
// TODO set exact modelCollection based on name instead
setCustomComponentsCollections([JSON.parse(modelString)]);
const parsedModel = JSON.parse(modelString);
parsedModel.components.forEach((component: any, index: number) => {
// try parsing template to check for compile errors
try {
(Handlebars.compile(component.htmlPreview))((component.defaultInputs));
} catch (e) {
throw new Error(`Component ${index} [${component?.type}] htmlPreview` + e);
}
});
setCustomComponentsCollections([parsedModel]);
}

setJsonParseError('');
Expand Down

0 comments on commit db48c45

Please sign in to comment.