Skip to content

Commit

Permalink
feat: when creating workflow from within event-type, preselect it (#1…
Browse files Browse the repository at this point in the history
…4774)

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
  • Loading branch information
Amit91848 and Udit-takkar committed Apr 29, 2024
1 parent 93bb377 commit 8b28a6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Expand Up @@ -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) {
Expand Down
17 changes: 15 additions & 2 deletions packages/features/ee/workflows/components/WorkflowDetailsPage.tsx
@@ -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";

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8b28a6f

Please sign in to comment.