Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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