diff --git a/changes/issue-3141-sql-errors-show-in-realtime b/changes/issue-3141-sql-errors-show-in-realtime new file mode 100644 index 0000000000..d5f0f3d727 --- /dev/null +++ b/changes/issue-3141-sql-errors-show-in-realtime @@ -0,0 +1 @@ +* Query and Policy SQL errors showing on Save now show in realtime \ No newline at end of file diff --git a/frontend/pages/policies/PolicyPage/components/PolicyForm/PolicyForm.tsx b/frontend/pages/policies/PolicyPage/components/PolicyForm/PolicyForm.tsx index 0abf5438d7..40a646cc3a 100644 --- a/frontend/pages/policies/PolicyPage/components/PolicyForm/PolicyForm.tsx +++ b/frontend/pages/policies/PolicyPage/components/PolicyForm/PolicyForm.tsx @@ -107,6 +107,15 @@ const PolicyForm = ({ useEffect(() => { debounceCompatiblePlatforms(lastEditedQueryBody); + + let valid = true; + const { valid: isValidated, errors: newErrors } = validateQuerySQL( + lastEditedQueryBody + ); + valid = isValidated; + setErrors({ + ...newErrors, + }); }, [lastEditedQueryBody]); const hasTeamMaintainerPermissions = isEditMode @@ -136,7 +145,11 @@ const PolicyForm = ({ }); }; - const handleSavePolicy = (forceNew = false) => ( + const onChangePolicy = (sqlString: string) => { + setLastEditedQueryBody(sqlString); + }; + + const promptSavePolicy = (forceNew = false) => ( evt: React.MouseEvent ) => { evt.preventDefault(); @@ -149,15 +162,9 @@ const PolicyForm = ({ } let valid = true; - const { valid: isValidated, errors: newErrors } = validateQuerySQL( - lastEditedQueryBody - ); + const { valid: isValidated } = validateQuerySQL(lastEditedQueryBody); valid = isValidated; - setErrors({ - ...errors, - ...newErrors, - }); if (valid) { if (!isEditMode || forceNew) { @@ -168,15 +175,11 @@ const PolicyForm = ({ description: lastEditedQueryDescription, query: lastEditedQueryBody, }); - - setErrors({}); } setIsEditingName(false); setIsEditingDescription(false); } - - return null; }; const renderAuthor = (): JSX.Element | null => { @@ -224,8 +227,6 @@ const PolicyForm = ({ } else if (compatiblePlatforms[0] === "None") { return "No platforms (check your query for invalid tables or tables that are supported on different platforms)"; } - - return null; }; const displayFormattedPlatforms = compatiblePlatforms.map((string) => { @@ -424,10 +425,8 @@ const PolicyForm = ({ name="query editor" onLoad={onLoad} wrapperClassName={`${baseClass}__text-editor-wrapper`} - onChange={(sqlString: string) => { - setLastEditedQueryBody(sqlString); - }} - handleSubmit={handleSavePolicy} + onChange={onChangePolicy} + handleSubmit={promptSavePolicy} /> {renderPlatformCompatibility()} {renderLiveQueryWarning()} @@ -449,7 +448,7 @@ const PolicyForm = ({