From 3069bce498c3458ec166a68602e57dc1f1980f7c Mon Sep 17 00:00:00 2001 From: Athul <89829560+athul-rs@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:07:22 +0530 Subject: [PATCH] Fix/prompt studio bugs (#55) * UI Bug fixes * Fix/prompt-studio UN-714 --------- Co-authored-by: Hari John Kuriakose --- .../custom-synonyms/CustomSynonyms.jsx | 74 +++++++++++-------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/frontend/src/components/custom-tools/custom-synonyms/CustomSynonyms.jsx b/frontend/src/components/custom-tools/custom-synonyms/CustomSynonyms.jsx index f964fdd52..a8efb0bb8 100644 --- a/frontend/src/components/custom-tools/custom-synonyms/CustomSynonyms.jsx +++ b/frontend/src/components/custom-tools/custom-synonyms/CustomSynonyms.jsx @@ -130,44 +130,58 @@ function CustomSynonyms({ setOpen }) { setSynonyms(updatedSynonyms); }; + function isEmpty(obj) { + if (typeof obj !== "object" || obj === null) { + return true; + } + return Object.values(obj).every( + (arr) => Array.isArray(arr) && arr.length === 0 + ); + } + const handleSave = () => { const promptGrammar = {}; [...synonyms].forEach((item) => { promptGrammar[item?.word] = item?.synonyms || []; }); + if (promptGrammar && !isEmpty(promptGrammar)) { + const body = { + prompt_grammer: promptGrammar, + }; + const requestOptions = { + method: "PATCH", + url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/${details?.tool_id}/`, + headers: { + "X-CSRFToken": sessionDetails?.csrfToken, + "Content-Type": "application/json", + }, + data: body, + }; - const body = { - prompt_grammer: promptGrammar, - }; - - const requestOptions = { - method: "PATCH", - url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/${details?.tool_id}/`, - headers: { - "X-CSRFToken": sessionDetails?.csrfToken, - "Content-Type": "application/json", - }, - data: body, - }; - - setIsLoading(true); - axiosPrivate(requestOptions) - .then((res) => { - const grammar = res?.data?.prompt_grammer; - const updatedDetails = { ...details }; - updatedDetails["prompt_grammer"] = grammar; - updateCustomTool(updatedDetails); - setAlertDetails({ - type: "success", - content: "Saved synonyms successfully", + setIsLoading(true); + axiosPrivate(requestOptions) + .then((res) => { + const grammar = res?.data?.prompt_grammer; + const updatedDetails = { ...details }; + updatedDetails["prompt_grammer"] = grammar; + updateCustomTool(updatedDetails); + setAlertDetails({ + type: "success", + content: "Saved synonyms successfully", + }); + }) + .catch((err) => { + setAlertDetails(handleException(err, "Failed to update")); + }) + .finally(() => { + setIsLoading(false); }); - }) - .catch((err) => { - setAlertDetails(handleException(err, "Failed to update")); - }) - .finally(() => { - setIsLoading(false); + } else { + setAlertDetails({ + type: "warning", + content: "Please add synonyms to save", }); + } }; return (