Skip to content

Commit

Permalink
Merge pull request #1458 from flanksource/1400-notifications-template…
Browse files Browse the repository at this point in the history
…-left-empty-saved

1400-notifications-template-left-empty-saved
  • Loading branch information
moshloop authored Oct 24, 2023
2 parents 984bdfc + e9de6a8 commit 1eef6e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/components/Forms/Formik/FormikCodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useField, useFormikContext } from "formik";
import { isEmpty } from "lodash";
import dynamic from "next/dynamic";
import { useEffect, useMemo, useState } from "react";
import useDebounce from "../../../hooks/useDebounce";
import YAML from "yaml";
import useDebounce from "../../../hooks/useDebounce";

const CodeEditor = dynamic(
() => import("../../CodeEditor").then((m) => m.CodeEditor),
Expand Down Expand Up @@ -55,21 +55,23 @@ export function FormikCodeEditor({
const debouncedValues = useDebounce(codeEditorValue, 300);

useEffect(() => {
if (debouncedValues) {
try {
if (format === "yaml" || format === "json") {
try {
if (format === "yaml" || format === "json") {
if (debouncedValues) {
setFieldValue(
fieldName,
format === "yaml"
? YAML.parse(debouncedValues)
: JSON.parse(debouncedValues)
);
} else {
setFieldValue(fieldName, debouncedValues);
setFieldValue(fieldName, undefined);
}
} catch (e) {
// do nothing, we don't want to set the values if the user is typing
} else {
setFieldValue(fieldName, debouncedValues);
}
} catch (e) {
// do nothing, we don't want to set the values if the user is typing
}
}, [debouncedValues, fieldName, format, setFieldValue]);

Expand Down Expand Up @@ -106,8 +108,9 @@ export function FormikCodeEditor({
if (v) {
setCodeEditorValue(v);
} else {
// if the value is empty, we want to reset the form
setFieldValue(fieldName, {});
setCodeEditorValue(undefined);
// if the value is empty, we want to set to undefined
setFieldValue(fieldName, undefined);
}
}}
value={value}
Expand Down
1 change: 0 additions & 1 deletion src/components/Forms/Formik/FormikSelectDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export default function FormikSelectDropdown({
options={options}
value={value}
onChange={(value: any) => {
console.log({ value: value });
field.onChange({
target: {
name: field.name,
Expand Down

0 comments on commit 1eef6e5

Please sign in to comment.