feat: when creating workflow from within event-type, preselect it (#14774)

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
This commit is contained in:
Amit Sharma
2024-04-29 14:23:28 +00:00
committed by GitHub
co-authored by Udit Takkar
parent 93bb37702c
commit 8b28a6f5df
2 changed files with 16 additions and 3 deletions
@@ -233,7 +233,7 @@ function EventWorkflowsTab(props: Props) {
const createMutation = trpc.viewer.workflows.create.useMutation({
onSuccess: async ({ workflow }) => {
await router.replace(`/workflows/${workflow.id}`);
await router.replace(`/workflows/${workflow.id}?eventTypeId=${eventType.id}`);
},
onError: (err) => {
if (err instanceof HttpError) {
@@ -1,6 +1,6 @@
import { useRouter } from "next/navigation";
import { useRouter, useSearchParams } from "next/navigation";
import type { Dispatch, SetStateAction } from "react";
import { useMemo, useState } from "react";
import { useMemo, useState, useEffect } from "react";
import type { UseFormReturn } from "react-hook-form";
import { Controller } from "react-hook-form";
@@ -44,6 +44,9 @@ export default function WorkflowDetailsPage(props: Props) {
const { data, isPending } = trpc.viewer.eventTypes.getByViewer.useQuery();
const searchParams = useSearchParams();
const eventTypeId = searchParams.get("eventTypeId");
const eventTypeOptions = useMemo(
() =>
data?.eventTypeGroups.reduce((options, group) => {
@@ -83,6 +86,16 @@ export default function WorkflowDetailsPage(props: Props) {
});
}
useEffect(() => {
const matchingOption = allEventTypeOptions.find((option) => option.value === eventTypeId);
if (matchingOption && !selectedEventTypes.find((option) => option.value === eventTypeId)) {
const newOptions = [...selectedEventTypes, matchingOption];
setSelectedEventTypes(newOptions);
form.setValue("activeOn", newOptions);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [eventTypeId, allEventTypeOptions]);
const addAction = (
action: WorkflowActions,
sendTo?: string,