From 4e57807a52ed232309d679d4a9327d8164e48722 Mon Sep 17 00:00:00 2001 From: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:53:46 +0530 Subject: [PATCH] fix: fixes segment not getting saved bug (#2527) --- .../edit/components/SurveyMenuBar.tsx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/SurveyMenuBar.tsx b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/SurveyMenuBar.tsx index 0b8dc48140..04118c00d6 100644 --- a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/SurveyMenuBar.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/SurveyMenuBar.tsx @@ -11,6 +11,7 @@ import toast from "react-hot-toast"; import { createSegmentAction } from "@formbricks/ee/advancedTargeting/lib/actions"; import { TEnvironment } from "@formbricks/types/environment"; import { TProduct } from "@formbricks/types/product"; +import { TSegment } from "@formbricks/types/segment"; import { TSurvey, TSurveyEditorTabs } from "@formbricks/types/surveys"; import { AlertDialog } from "@formbricks/ui/AlertDialog"; import { Button } from "@formbricks/ui/Button"; @@ -116,9 +117,10 @@ export const SurveyMenuBar = ({ } }; - const handleSegmentWithIdTemp = async () => { + const handleTemporarySegment = async () => { if (localSurvey.segment && localSurvey.type === "app" && localSurvey.segment?.id === "temp") { const { filters } = localSurvey.segment; + // create a new private segment const newSegment = await createSegmentAction({ environmentId: localSurvey.environmentId, @@ -132,6 +134,15 @@ export const SurveyMenuBar = ({ } }; + const handleSegmentUpdate = async (): Promise => { + if (localSurvey.segment && localSurvey.segment.id === "temp") { + const segment = await handleTemporarySegment(); + return segment ?? null; + } + + return localSurvey.segment; + }; + const handleSurveySave = async (shouldNavigateBack = false) => { setIsSurveySaving(true); try { @@ -152,7 +163,8 @@ export const SurveyMenuBar = ({ const { isDraft, ...rest } = question; return rest; }); - const segment = (await handleSegmentWithIdTemp()) ?? null; + + const segment = await handleSegmentUpdate(); await updateSurveyAction({ ...localSurvey, segment }); setIsSurveySaving(false); setLocalSurvey(localSurvey); @@ -184,7 +196,8 @@ export const SurveyMenuBar = ({ return; } const status = localSurvey.runOnDate ? "scheduled" : "inProgress"; - const segment = (await handleSegmentWithIdTemp()) ?? null; + const segment = await handleSegmentUpdate(); + await updateSurveyAction({ ...localSurvey, status,