diff --git a/apps/web/app/environments/[environmentId]/events/EventSettingsTab.tsx b/apps/web/app/environments/[environmentId]/events/EventSettingsTab.tsx index 5f558e9fec..8ca3519930 100644 --- a/apps/web/app/environments/[environmentId]/events/EventSettingsTab.tsx +++ b/apps/web/app/environments/[environmentId]/events/EventSettingsTab.tsx @@ -1,12 +1,17 @@ import LoadingSpinner from "@/components/shared/LoadingSpinner"; -import { Button } from "@formbricks/ui"; +import { Button, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@formbricks/ui"; import { ErrorComponent } from "@formbricks/ui"; import { Input } from "@formbricks/ui"; import { Label } from "@formbricks/ui"; import { RadioGroup, RadioGroupItem } from "@formbricks/ui"; import { useEventClass, useEventClasses } from "@/lib/eventClasses/eventClasses"; import { useEventClassMutation } from "@/lib/eventClasses/mutateEventClasses"; -import { useForm } from "react-hook-form"; +import { Controller, useForm } from "react-hook-form"; +import type { NoCodeConfig, Event } from "@formbricks/types/events"; +import { testURLmatch } from "./testURLmatch"; +import { useState } from "react"; +import { toast } from "react-hot-toast"; +import clsx from "clsx"; interface EventSettingsTabProps { environmentId: string; @@ -17,8 +22,12 @@ interface EventSettingsTabProps { export default function EventSettingsTab({ environmentId, eventClassId, setOpen }: EventSettingsTabProps) { const { eventClass, isLoadingEventClass, isErrorEventClass } = useEventClass(environmentId, eventClassId); - const { register, handleSubmit } = useForm({ - defaultValues: { name: eventClass.name, description: eventClass.description }, + const { register, handleSubmit, control, watch } = useForm({ + defaultValues: { + name: eventClass.name, + description: eventClass.description, + noCodeConfig: eventClass.noCodeConfig, + }, }); const { triggerEventClassMutate, isMutatingEventClass } = useEventClassMutation( environmentId, @@ -28,11 +37,41 @@ export default function EventSettingsTab({ environmentId, eventClassId, setOpen const { mutateEventClasses } = useEventClasses(environmentId); const onSubmit = async (data) => { - await triggerEventClassMutate(data); + const filteredNoCodeConfig = filterNoCodeConfig(data.noCodeConfig as NoCodeConfig); + + const updatedData: Event = { + ...data, + noCodeConfig: filteredNoCodeConfig, + type: "noCode", + } as Event; + + await triggerEventClassMutate(updatedData); mutateEventClasses(); setOpen(false); }; + const filterNoCodeConfig = (noCodeConfig: NoCodeConfig): NoCodeConfig => { + const { type } = noCodeConfig; + return { + type, + [type]: noCodeConfig[type], + }; + }; + + const [testUrl, setTestUrl] = useState(""); + const [isMatch, setIsMatch] = useState(""); + + const handleMatchClick = () => { + const match = testURLmatch( + testUrl, + watch("noCodeConfig.[pageUrl].value"), + watch("noCodeConfig.[pageUrl].rule") + ); + setIsMatch(match); + if (match === "yes") toast.success("Your survey would be shown on this URL."); + if (match === "no") toast.error("Your survey would not be shown."); + }; + if (isLoadingEventClass) return ; if (isErrorEventClass) return ; @@ -61,37 +100,152 @@ export default function EventSettingsTab({ environmentId, eventClassId, setOpen })} /> -
+
{eventClass.type === "code" ? (

This is a code action. Please make changes in your code base.

) : eventClass.type === "noCode" ? ( -
- - -
- - -
-
- - -
-
- - -
-
+
+
+ ( + +
+ + +
+
+ + +
+
+ + +
+
+ )} + /> + {(watch("noCodeConfig.type") === "pageUrl" || !watch("noCodeConfig.type")) && ( + <> +
+
+ + ( + + )} + /> +
+ +
+ +
+
+ +
+ +
+ +
+ { + setTestUrl(e.target.value); + setIsMatch("default"); + }} + className={clsx( + isMatch === "yes" + ? "border-green-500 bg-green-50" + : isMatch === "no" + ? "border-red-200 bg-red-50" + : isMatch === "default" + ? "border-slate-200 bg-white" + : null + )} + placeholder="Paste the URL you want the event to trigger on" + /> + +
+
+
+ + )} + {watch("noCodeConfig.type") === "innerHtml" && ( +
+
+ +
+
+ +
+
+ )} + {watch("noCodeConfig.type") === "cssSelector" && ( +
+
+ +
+
+ +
+
+ )} +
) : eventClass.type === "automatic" ? (

@@ -99,7 +253,7 @@ export default function EventSettingsTab({ environmentId, eventClassId, setOpen

) : null}
-
+