From 4aa040d96edc2e24c48c86fcc70f6f842186df87 Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Fri, 2 Feb 2024 17:25:37 -0500 Subject: [PATCH] feat: routing form fields as variables in route (#13519) * add UI for setting up custom event typ url * extract variables when processing route * remove console.log * fix some UI issues * code clean up * add translation * fix type error * Update packages/app-store/routing-forms/pages/route-builder/[...appPages].tsx Co-authored-by: Hariom Balhara * make variable check case insensitive * add function * code clean up * code clean up --------- Co-authored-by: CarinaWolli Co-authored-by: Hariom Balhara --- apps/web/public/static/locales/en/common.json | 1 + .../routing-forms/lib/substituteVariables.ts | 26 +++++++++ .../pages/route-builder/[...appPages].tsx | 55 +++++++++++++++++-- .../pages/routing-link/[...appPages].tsx | 7 ++- 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 packages/app-store/routing-forms/lib/substituteVariables.ts diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index e2edfd60bc..3cd8c121b9 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -2242,5 +2242,6 @@ "redirect_to": "Redirect to", "having_trouble_finding_time": "Having trouble finding a time?", "show_more": "Show more", + "send_booker_to": "Send Booker to", "ADD_NEW_STRINGS_ABOVE_THIS_LINE_TO_PREVENT_MERGE_CONFLICTS": "↑↑↑↑↑↑↑↑↑↑↑↑↑ Add your new strings above here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑" } diff --git a/packages/app-store/routing-forms/lib/substituteVariables.ts b/packages/app-store/routing-forms/lib/substituteVariables.ts new file mode 100644 index 0000000000..b352df7db2 --- /dev/null +++ b/packages/app-store/routing-forms/lib/substituteVariables.ts @@ -0,0 +1,26 @@ +import slugify from "@calcom/lib/slugify"; + +import type { Response, Route, Field } from "../types/types"; +import getFieldIdentifier from "./getFieldIdentifier"; + +export const substituteVariables = ( + routeValue: Route["action"]["value"], + response: Response, + fields: Field[] +) => { + const regex = /\{([^\}]+)\}/g; + const variables: string[] = routeValue.match(regex)?.map((match: string) => match.slice(1, -1)) || []; + + let eventTypeUrl = routeValue; + + variables.forEach((variable) => { + for (const key in response) { + const identifier = getFieldIdentifier(fields.find((field) => field.id === key)); + if (identifier.toLowerCase() === variable.toLowerCase()) { + eventTypeUrl = eventTypeUrl.replace(`{${variable}}`, slugify(response[key].value.toString() || "")); + } + } + }); + + return eventTypeUrl; +}; diff --git a/packages/app-store/routing-forms/pages/route-builder/[...appPages].tsx b/packages/app-store/routing-forms/pages/route-builder/[...appPages].tsx index 0337f53133..a65df14b36 100644 --- a/packages/app-store/routing-forms/pages/route-builder/[...appPages].tsx +++ b/packages/app-store/routing-forms/pages/route-builder/[...appPages].tsx @@ -92,6 +92,8 @@ const Route = ({ appUrl: string; disabled?: boolean; }) => { + const { t } = useLocale(); + const index = routes.indexOf(route); const { data: eventTypesByGroup } = trpc.viewer.eventTypes.getByViewer.useQuery({ @@ -128,6 +130,16 @@ const Route = ({ }); }); + // /team/{TEAM_SLUG}/{EVENT_SLUG} -> /team/{TEAM_SLUG} + const eventTypePrefix = + eventOptions.length !== 0 + ? eventOptions[0].value.substring(0, eventOptions[0].value.lastIndexOf("/") + 1) + : ""; + + const [customEventTypeSlug, setCustomEventTypeSlug] = useState( + !isRouter(route) ? route.action.value.split("/").pop() : "" + ); + const onChange = (route: Route, immutableTree: ImmutableTree, config: QueryBuilderUpdatedConfig) => { const jsonTree = QbUtils.getTree(immutableTree); setRoute(route.id, { @@ -199,7 +211,7 @@ const Route = ({
- Send Booker to + {t("send_booker_to")}