Skip to content

Commit

Permalink
feat: In Embed, Fire 'routed' event when Routing Form routes (#14470)
Browse files Browse the repository at this point in the history
  • Loading branch information
hariombalhara committed Apr 11, 2024
1 parent e2ef8d2 commit 9a66dae
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import getFieldIdentifier from "../../lib/getFieldIdentifier";
import { processRoute } from "../../lib/processRoute";
import { substituteVariables } from "../../lib/substituteVariables";
import transformResponse from "../../lib/transformResponse";
import type { Response, Route } from "../../types/types";
import type { NonRouterRoute, Response } from "../../types/types";
import { getServerSideProps } from "./getServerSideProps";

type Props = inferSSRProps<typeof getServerSideProps>;
Expand All @@ -42,7 +42,7 @@ const useBrandColors = ({
};

function RoutingForm({ form, profile, ...restProps }: Props) {
const [customPageMessage, setCustomPageMessage] = useState<Route["action"]["value"]>("");
const [customPageMessage, setCustomPageMessage] = useState<NonRouterRoute["action"]["value"]>("");
const formFillerIdRef = useRef(uuidv4());
const isEmbed = useIsEmbed(restProps.isEmbed);
useTheme(profile.theme);
Expand All @@ -58,7 +58,7 @@ function RoutingForm({ form, profile, ...restProps }: Props) {
// - like a network error
// - or he abandoned booking flow in between
const formFillerId = formFillerIdRef.current;
const decidedActionWithFormResponseRef = useRef<{ action: Route["action"]; response: Response }>();
const decidedActionWithFormResponseRef = useRef<{ action: NonRouterRoute["action"]; response: Response }>();
const router = useRouter();

const onSubmit = (response: Response) => {
Expand Down Expand Up @@ -98,7 +98,10 @@ function RoutingForm({ form, profile, ...restProps }: Props) {
}
const allURLSearchParams = getUrlSearchParamsToForward(decidedActionWithFormResponse.response, fields);
const decidedAction = decidedActionWithFormResponse.action;

sdkActionManager?.fire("routed", {
actionType: decidedAction.type,
actionValue: decidedAction.value,
});
//TODO: Maybe take action after successful mutation
if (decidedAction.type === "customPageMessage") {
setCustomPageMessage(decidedAction.value);
Expand Down
2 changes: 2 additions & 0 deletions packages/app-store/routing-forms/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type Fields = z.infer<typeof zodFieldsView>;
export type Field = Fields[number];
export type Routes = z.infer<typeof zodRoutesView>;
export type Route = Routes[0];
export type NonRouterRoute = z.infer<typeof zodNonRouterRoute>;

export type SerializableFormTeamMembers = {
id: number;
name: string | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/embed-core/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ <h2>Popup Examples</h2>
Book Free Event [Auto Theme][uiConfig.hideEventTypeDetails=true]
</button>
<button data-cal-namespace="routingFormAuto" data-cal-link="forms/948ae412-d995-4865-875a-48302588de03">
Book through Routing Form [Auto Theme]
Book through Routing Form [Auto Theme] - Test for 'routed' event
</button>
<button
data-cal-namespace="routingFormDark"
Expand Down
9 changes: 9 additions & 0 deletions packages/embeds/embed-core/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ Cal("init", "routingFormAuto", {
origin: "http://localhost:3000",
});

Cal.ns.routingFormAuto("on", {
action: "routed",
callback: (e) => {
const detail = e.detail;
console.log("`routed` event data:", detail.data);
alert(`Routing Done - Check console for 'routed' event data`);
},
});

Cal("init", "routingFormDark", {
debug: true,
origin: "http://localhost:3000",
Expand Down
4 changes: 4 additions & 0 deletions packages/embeds/embed-core/src/sdk-action-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export type EventDataMap = {
};
eventType: unknown;
};
routed: {
actionType: "customPageMessage" | "externalRedirectUrl" | "eventTypeRedirectUrl";
actionValue: string;
};
"*": Record<string, unknown>;
__routeChanged: Record<string, never>;
__windowLoadComplete: Record<string, never>;
Expand Down

0 comments on commit 9a66dae

Please sign in to comment.