From 38e2c05ce7dc4238924264600bdb720c19c60be3 Mon Sep 17 00:00:00 2001 From: Bobby Date: Mon, 5 Jul 2021 14:26:47 +1000 Subject: [PATCH] spark editor validation --- .../Table/TableHeader/Sparks/SparkModal.tsx | 84 +++++++++++++++++-- .../components/Table/editors/CodeEditor.tsx | 10 +++ 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/www/src/components/Table/TableHeader/Sparks/SparkModal.tsx b/www/src/components/Table/TableHeader/Sparks/SparkModal.tsx index 4757c093..c8a91f37 100644 --- a/www/src/components/Table/TableHeader/Sparks/SparkModal.tsx +++ b/www/src/components/Table/TableHeader/Sparks/SparkModal.tsx @@ -1,8 +1,10 @@ import React, { useState } from "react"; import _isEqual from "lodash/isEqual"; +import useStateRef from "react-usestateref"; import { ISpark, triggerTypes } from "./utils"; import Modal from "components/Modal"; import CodeEditorHelper from "components/CodeEditorHelper"; +import { useConfirmation } from "components/ConfirmationDialog"; import CodeEditor from "../../editors/CodeEditor"; import { useFiretableContext } from "contexts/FiretableContext"; import BackIcon from "@material-ui/icons/ArrowBack"; @@ -134,8 +136,23 @@ export default function SparkModal({ mode, sparkObject: initialObject, }: ISparkModalProps) { + const { requestConfirmation } = useConfirmation(); const [sparkObject, setSparkObject] = useState(initialObject); const [tabIndex, setTabIndex] = useState(0); + const [validation, setValidation, validationRef] = useStateRef({ + condition: true, + sparkBody: true, + }); + const [ + conditionEditorActive, + setConditionEditorActive, + conditionEditorActiveRef, + ] = useStateRef(false); + const [ + bodyEditorActive, + setBodyEditorActive, + bodyEditorActiveRef, + ] = useStateRef(false); const classes = useStyles(); const { tableState } = useFiretableContext(); const columns = Object.keys(tableState?.columns ?? {}); @@ -145,6 +162,17 @@ export default function SparkModal({ setTabIndex(newValue); }; + const handleAddOrUpdate = () => { + switch (mode) { + case "add": + handleAdd(sparkObject); + return; + case "update": + handleUpdate(sparkObject); + return; + } + }; + return ( { - // setIsSparksValid(isValid); + if (!conditionEditorActiveRef.current) { + return; + } + setValidation({ + ...validationRef.current, + condition: isValid, + }); + console.log(validationRef.current); }} diagnosticsOptions={{ noSemanticValidation: false, noSyntaxValidation: false, noSuggestionDiagnostics: true, }} + onMount={() => { + setConditionEditorActive(true); + }} + onUnmount={() => { + setConditionEditorActive(false); + }} /> { - // setIsSparksValid(isValid); + if (!bodyEditorActiveRef.current) { + return; + } + setValidation({ + ...validationRef.current, + sparkBody: isValid, + }); + console.log(validationRef.current); }} diagnosticsOptions={{ noSemanticValidation: false, noSyntaxValidation: false, noSuggestionDiagnostics: true, }} + onMount={() => { + setBodyEditorActive(true); + }} + onUnmount={() => { + setBodyEditorActive(false); + }} /> { - switch (mode) { - case "add": - handleAdd(sparkObject); - return; - case "update": - handleUpdate(sparkObject); - return; + let warningMessage; + if (!validation.condition && !validation.sparkBody) { + warningMessage = "Condition and spark body are not valid"; + } else if (!validation.condition) { + warningMessage = "Condition is not valid"; + } else if (!validation.sparkBody) { + warningMessage = "Spark body is not valid"; + } + + if (warningMessage) { + requestConfirmation({ + title: "Validation failed", + body: `${warningMessage}, do you want to continue?`, + confirm: "Yes, I know what I am doing", + cancel: "No, I'll fix the errors", + handleConfirm: handleAddOrUpdate, + }); + } else { + handleAddOrUpdate(); } }, }, diff --git a/www/src/components/Table/editors/CodeEditor.tsx b/www/src/components/Table/editors/CodeEditor.tsx index a529e08a..9a1f67b1 100644 --- a/www/src/components/Table/editors/CodeEditor.tsx +++ b/www/src/components/Table/editors/CodeEditor.tsx @@ -3,6 +3,7 @@ import { useTheme, createStyles, makeStyles } from "@material-ui/core/styles"; import Editor, { useMonaco } from "@monaco-editor/react"; import { useFiretableContext } from "contexts/FiretableContext"; import { FieldType } from "constants/fields"; +import { useEffect } from "react"; const useStyles = makeStyles((theme) => createStyles({ @@ -33,6 +34,8 @@ export default function CodeEditor(props: any) { script, onValideStatusUpdate, diagnosticsOptions, + onUnmount, + onMount, } = props; const theme = useTheme(); const monacoInstance = useMonaco(); @@ -43,8 +46,15 @@ export default function CodeEditor(props: any) { const editorRef = useRef(); + useEffect(() => { + return () => { + onUnmount?.(); + }; + }, []); + function handleEditorDidMount(_, editor) { editorRef.current = editor; + onMount?.(); } const themeTransformer = (theme: string) => {