From 49b981b3a65372ac46235f88166a629127a353a8 Mon Sep 17 00:00:00 2001 From: Amit Ranjan Date: Sat, 19 Oct 2024 01:37:31 +0530 Subject: [PATCH] fix: added the trimmed function and utilized it --- .../components/visual-editor/BlockDialog.tsx | 3 ++- frontend/src/utils/object.ts | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/visual-editor/BlockDialog.tsx b/frontend/src/components/visual-editor/BlockDialog.tsx index 5088621e..247e7fc8 100644 --- a/frontend/src/components/visual-editor/BlockDialog.tsx +++ b/frontend/src/components/visual-editor/BlockDialog.tsx @@ -34,6 +34,7 @@ import { useToast } from "@/hooks/useToast"; import { useTranslate } from "@/hooks/useTranslate"; import { EntityType } from "@/services/types"; import { OutgoingMessageFormat } from "@/types/message.types"; +import { trimObjectValues } from "@/utils/object"; import { IBlockAttributes, IBlock } from "../../types/block.types"; @@ -102,7 +103,7 @@ const BlockDialog: FC = ({ }; const onSubmitForm = async (params: IBlockAttributes) => { if (block) { - updateBlock({ id: block.id, params }); + updateBlock({ id: block.id, params: trimObjectValues(params) }); } }; diff --git a/frontend/src/utils/object.ts b/frontend/src/utils/object.ts index 8c9e4c5e..a56fcc48 100644 --- a/frontend/src/utils/object.ts +++ b/frontend/src/utils/object.ts @@ -39,3 +39,27 @@ export function merge< return output as T & U; } + +export function trimObjectValues(obj) { + if (typeof obj === "object" && obj !== null) { + if (Array.isArray(obj)) { + return obj.map(trimObjectValues); + } + + const trimmedObject = {}; + + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + trimmedObject[key] = trimObjectValues(obj[key]); + } + } + + return trimmedObject; + } + + if (typeof obj === "string") { + return obj.trim(); + } + + return obj; +}