2023-02-16 22:39:57 +00:00
|
|
|
import type { App_RoutingForms_Form } from "@prisma/client";
|
2023-01-04 13:30:46 +00:00
|
|
|
import Link from "next/link";
|
2022-11-10 12:58:07 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { UseFormReturn } from "react-hook-form";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-09-02 19:00:41 +00:00
|
|
|
|
2023-01-10 15:39:29 +00:00
|
|
|
import { ShellMain } from "@calcom/features/shell/Shell";
|
2022-09-18 10:47:31 +00:00
|
|
|
import useApp from "@calcom/lib/hooks/useApp";
|
2022-09-02 19:00:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type {
|
2022-11-11 09:57:44 +00:00
|
|
|
AppGetServerSidePropsContext,
|
|
|
|
AppPrisma,
|
|
|
|
AppSsrInit,
|
2022-11-23 02:55:25 +00:00
|
|
|
AppUser,
|
2022-11-11 09:57:44 +00:00
|
|
|
} from "@calcom/types/AppGetServerSideProps";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
2023-01-05 12:04:28 +00:00
|
|
|
Alert,
|
2023-01-04 13:30:46 +00:00
|
|
|
Badge,
|
2023-01-06 12:13:56 +00:00
|
|
|
Button,
|
2022-11-23 02:55:25 +00:00
|
|
|
ButtonGroup,
|
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
DialogHeader,
|
|
|
|
DropdownMenuSeparator,
|
|
|
|
Form,
|
|
|
|
Meta,
|
|
|
|
SettingsToggle,
|
|
|
|
showToast,
|
|
|
|
TextAreaField,
|
|
|
|
TextField,
|
|
|
|
Tooltip,
|
|
|
|
VerticalDivider,
|
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { ExternalLink, Link as LinkIcon, Download, Code, Trash } from "@calcom/ui/components/icon";
|
2022-09-02 19:00:41 +00:00
|
|
|
|
2023-01-07 15:50:28 +00:00
|
|
|
import { RoutingPages } from "../lib/RoutingPages";
|
2022-11-11 09:57:44 +00:00
|
|
|
import { getSerializableForm } from "../lib/getSerializableForm";
|
2022-11-10 12:58:07 +00:00
|
|
|
import { processRoute } from "../lib/processRoute";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Response, Route, SerializableForm } from "../types/types";
|
2022-09-02 19:00:41 +00:00
|
|
|
import { FormAction, FormActionsDropdown, FormActionsProvider } from "./FormActions";
|
2022-11-10 12:58:07 +00:00
|
|
|
import FormInputFields from "./FormInputFields";
|
2022-09-02 19:00:41 +00:00
|
|
|
import RoutingNavBar from "./RoutingNavBar";
|
|
|
|
|
|
|
|
type RoutingForm = SerializableForm<App_RoutingForms_Form>;
|
2023-01-04 13:30:46 +00:00
|
|
|
|
|
|
|
export type RoutingFormWithResponseCount = RoutingForm & {
|
2022-09-02 19:00:41 +00:00
|
|
|
_count: {
|
|
|
|
responses: number;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const Actions = ({
|
|
|
|
form,
|
|
|
|
mutation,
|
|
|
|
}: {
|
|
|
|
form: RoutingFormWithResponseCount;
|
|
|
|
mutation: {
|
|
|
|
isLoading: boolean;
|
|
|
|
};
|
|
|
|
}) => {
|
|
|
|
const { t } = useLocale();
|
2022-09-18 10:47:31 +00:00
|
|
|
const { data: typeformApp } = useApp("typeform");
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
return (
|
|
|
|
<div className="flex items-center">
|
2023-04-19 20:17:54 +00:00
|
|
|
<div className="hidden items-center sm:inline-flex">
|
|
|
|
<FormAction className="self-center" data-testid="toggle-form" action="toggle" routingForm={form} />
|
|
|
|
<VerticalDivider />
|
|
|
|
</div>
|
2022-09-07 11:04:12 +00:00
|
|
|
<ButtonGroup combined containerProps={{ className: "hidden md:inline-flex items-center" }}>
|
2022-09-02 19:00:41 +00:00
|
|
|
<Tooltip content={t("preview")}>
|
|
|
|
<FormAction
|
|
|
|
routingForm={form}
|
|
|
|
color="secondary"
|
|
|
|
target="_blank"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2022-09-02 19:00:41 +00:00
|
|
|
type="button"
|
|
|
|
rel="noreferrer"
|
|
|
|
action="preview"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={ExternalLink}
|
2022-09-02 19:00:41 +00:00
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
<FormAction
|
|
|
|
routingForm={form}
|
|
|
|
action="copyLink"
|
|
|
|
color="secondary"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2022-09-02 19:00:41 +00:00
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={LinkIcon}
|
2022-09-18 10:47:31 +00:00
|
|
|
tooltip={t("copy_link_to_form")}
|
2022-09-02 19:00:41 +00:00
|
|
|
/>
|
2022-09-18 10:47:31 +00:00
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
<Tooltip content="Download Responses">
|
|
|
|
<FormAction
|
|
|
|
data-testid="download-responses"
|
|
|
|
routingForm={form}
|
|
|
|
action="download"
|
|
|
|
color="secondary"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2022-09-02 19:00:41 +00:00
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Download}
|
2022-09-02 19:00:41 +00:00
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
<FormAction
|
|
|
|
routingForm={form}
|
|
|
|
action="embed"
|
|
|
|
color="secondary"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Code}
|
2022-09-02 19:00:41 +00:00
|
|
|
tooltip={t("embed")}
|
|
|
|
/>
|
2023-01-15 11:40:40 +00:00
|
|
|
<DropdownMenuSeparator />
|
2022-09-02 19:00:41 +00:00
|
|
|
<FormAction
|
|
|
|
routingForm={form}
|
|
|
|
action="_delete"
|
|
|
|
// className="mr-3"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}
|
2022-09-02 19:00:41 +00:00
|
|
|
color="secondary"
|
|
|
|
type="button"
|
|
|
|
tooltip={t("delete")}
|
|
|
|
/>
|
2022-10-14 16:24:43 +00:00
|
|
|
{typeformApp?.isInstalled ? (
|
2022-09-18 10:47:31 +00:00
|
|
|
<FormActionsDropdown form={form}>
|
|
|
|
<FormAction
|
2022-12-15 05:45:09 +00:00
|
|
|
data-testid="copy-redirect-url"
|
2022-09-18 10:47:31 +00:00
|
|
|
routingForm={form}
|
|
|
|
action="copyRedirectUrl"
|
|
|
|
color="minimal"
|
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={LinkIcon}>
|
2022-09-18 10:47:31 +00:00
|
|
|
{t("Copy Typeform Redirect Url")}
|
|
|
|
</FormAction>
|
|
|
|
</FormActionsDropdown>
|
|
|
|
) : null}
|
2022-09-02 19:00:41 +00:00
|
|
|
</ButtonGroup>
|
2022-09-18 10:47:31 +00:00
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
<div className="flex md:hidden">
|
|
|
|
<FormActionsDropdown form={form}>
|
|
|
|
<FormAction
|
|
|
|
routingForm={form}
|
|
|
|
color="minimal"
|
|
|
|
target="_blank"
|
|
|
|
type="button"
|
|
|
|
rel="noreferrer"
|
|
|
|
action="preview"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={ExternalLink}>
|
2022-09-09 11:45:20 +00:00
|
|
|
{t("preview")}
|
2022-09-02 19:00:41 +00:00
|
|
|
</FormAction>
|
|
|
|
<FormAction
|
|
|
|
action="copyLink"
|
|
|
|
className="w-full"
|
|
|
|
routingForm={form}
|
|
|
|
color="minimal"
|
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={LinkIcon}>
|
2022-09-18 10:47:31 +00:00
|
|
|
{t("copy_link_to_form")}
|
2022-09-02 19:00:41 +00:00
|
|
|
</FormAction>
|
|
|
|
<FormAction
|
|
|
|
action="download"
|
|
|
|
routingForm={form}
|
|
|
|
className="w-full"
|
|
|
|
color="minimal"
|
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Download}>
|
2022-09-09 11:45:20 +00:00
|
|
|
{t("download_responses")}
|
2022-09-02 19:00:41 +00:00
|
|
|
</FormAction>
|
|
|
|
<FormAction
|
|
|
|
action="embed"
|
|
|
|
routingForm={form}
|
|
|
|
color="minimal"
|
|
|
|
type="button"
|
|
|
|
className="w-full"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Code}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{t("embed")}
|
|
|
|
</FormAction>
|
2022-09-18 10:47:31 +00:00
|
|
|
{typeformApp ? (
|
|
|
|
<FormAction
|
2022-12-15 05:45:09 +00:00
|
|
|
data-testid="copy-redirect-url"
|
2022-09-18 10:47:31 +00:00
|
|
|
routingForm={form}
|
|
|
|
action="copyRedirectUrl"
|
|
|
|
color="minimal"
|
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={LinkIcon}>
|
2022-09-18 10:47:31 +00:00
|
|
|
{t("Copy Typeform Redirect Url")}
|
|
|
|
</FormAction>
|
|
|
|
) : null}
|
2023-04-19 20:17:54 +00:00
|
|
|
<DropdownMenuSeparator className="hidden sm:block" />
|
2022-09-02 19:00:41 +00:00
|
|
|
<FormAction
|
|
|
|
action="_delete"
|
|
|
|
routingForm={form}
|
|
|
|
className="w-full"
|
|
|
|
type="button"
|
|
|
|
color="destructive"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{t("delete")}
|
|
|
|
</FormAction>
|
2023-04-19 20:17:54 +00:00
|
|
|
<div className="block sm:hidden">
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<FormAction
|
|
|
|
data-testid="toggle-form"
|
|
|
|
action="toggle"
|
|
|
|
routingForm={form}
|
|
|
|
label="Disable Form"
|
|
|
|
extraClassNames="hover:bg-subtle cursor-pointer rounded-[5px] pr-4"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-09-02 19:00:41 +00:00
|
|
|
</FormActionsDropdown>
|
|
|
|
</div>
|
2022-09-07 11:04:12 +00:00
|
|
|
<VerticalDivider />
|
2022-09-02 19:00:41 +00:00
|
|
|
<Button data-testid="update-form" loading={mutation.isLoading} type="submit" color="primary">
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-11 15:52:18 +00:00
|
|
|
type SingleFormComponentProps = {
|
2022-09-02 19:00:41 +00:00
|
|
|
form: RoutingFormWithResponseCount;
|
|
|
|
appUrl: string;
|
|
|
|
Page: React.FC<{
|
|
|
|
form: RoutingFormWithResponseCount;
|
|
|
|
appUrl: string;
|
|
|
|
hookForm: UseFormReturn<RoutingFormWithResponseCount>;
|
|
|
|
}>;
|
2022-10-11 15:52:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function SingleForm({ form, appUrl, Page }: SingleFormComponentProps) {
|
|
|
|
const utils = trpc.useContext();
|
2022-11-03 14:40:03 +00:00
|
|
|
const { t } = useLocale();
|
2022-10-11 15:52:18 +00:00
|
|
|
|
2022-11-10 12:58:07 +00:00
|
|
|
const [isTestPreviewOpen, setIsTestPreviewOpen] = useState(false);
|
|
|
|
const [response, setResponse] = useState<Response>({});
|
|
|
|
const [decidedAction, setDecidedAction] = useState<Route["action"] | null>(null);
|
|
|
|
|
|
|
|
function testRouting() {
|
|
|
|
const action = processRoute({ form, response });
|
|
|
|
setDecidedAction(action);
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
const hookForm = useForm({
|
|
|
|
defaultValues: form,
|
|
|
|
});
|
|
|
|
|
2022-10-17 11:21:49 +00:00
|
|
|
useEffect(() => {
|
|
|
|
hookForm.reset(form);
|
|
|
|
}, [form, hookForm]);
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const mutation = trpc.viewer.appRoutingForms.formMutation.useMutation({
|
2022-09-02 19:00:41 +00:00
|
|
|
onSuccess() {
|
2023-05-17 08:47:48 +00:00
|
|
|
showToast(t("form_updated_successfully"), "success");
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
2023-01-04 13:30:46 +00:00
|
|
|
onError(e) {
|
|
|
|
if (e.message) {
|
|
|
|
showToast(e.message, "error");
|
|
|
|
return;
|
|
|
|
}
|
2022-09-02 19:00:41 +00:00
|
|
|
showToast(`Something went wrong`, "error");
|
|
|
|
},
|
|
|
|
onSettled() {
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.appRoutingForms.formQuery.invalidate({ id: form.id });
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
|
|
|
});
|
2023-01-04 13:30:46 +00:00
|
|
|
const connectedForms = form.connectedForms;
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
return (
|
2022-11-10 12:58:07 +00:00
|
|
|
<>
|
|
|
|
<Form
|
|
|
|
form={hookForm}
|
|
|
|
handleSubmit={(data) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
//@ts-ignore
|
|
|
|
mutation.mutate({
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}}>
|
|
|
|
<FormActionsProvider appUrl={appUrl}>
|
|
|
|
<Meta title={form.name} description={form.description || ""} />
|
|
|
|
<ShellMain
|
|
|
|
heading={form.name}
|
|
|
|
subtitle={form.description || ""}
|
|
|
|
backPath={`/${appUrl}/forms`}
|
|
|
|
CTA={<Actions form={form} mutation={mutation} />}>
|
2023-04-19 20:17:54 +00:00
|
|
|
<div className="-mx-4 mt-4 px-4 sm:px-6 md:-mx-8 md:mt-0 md:px-8">
|
|
|
|
<div className="flex flex-col items-center items-baseline md:flex-row md:items-start">
|
2022-11-10 12:58:07 +00:00
|
|
|
<div className="lg:min-w-72 lg:max-w-72 mb-6 md:mr-6">
|
|
|
|
<TextField
|
|
|
|
type="text"
|
|
|
|
containerClassName="mb-6"
|
2023-02-20 10:07:58 +00:00
|
|
|
placeholder={t("title")}
|
2022-11-10 12:58:07 +00:00
|
|
|
{...hookForm.register("name")}
|
2022-11-03 14:40:03 +00:00
|
|
|
/>
|
2022-11-10 12:58:07 +00:00
|
|
|
<TextAreaField
|
|
|
|
rows={3}
|
|
|
|
id="description"
|
|
|
|
data-testid="description"
|
2023-02-20 10:07:58 +00:00
|
|
|
placeholder={t("form_description_placeholder")}
|
2022-11-10 12:58:07 +00:00
|
|
|
{...hookForm.register("description")}
|
|
|
|
defaultValue={form.description || ""}
|
2022-09-02 19:00:41 +00:00
|
|
|
/>
|
2022-11-10 12:58:07 +00:00
|
|
|
|
|
|
|
<div className="mt-6">
|
|
|
|
<Controller
|
|
|
|
name="settings.emailOwnerOnSubmission"
|
|
|
|
control={hookForm.control}
|
|
|
|
render={({ field: { value, onChange } }) => {
|
|
|
|
return (
|
|
|
|
<SettingsToggle
|
|
|
|
title={t("routing_forms_send_email_owner")}
|
|
|
|
description={t("routing_forms_send_email_owner_description")}
|
|
|
|
checked={value}
|
|
|
|
onCheckedChange={(val) => onChange(val)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-01-04 13:30:46 +00:00
|
|
|
|
|
|
|
{form.routers.length ? (
|
|
|
|
<div className="mt-6">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-emphasis mb-2 block text-sm font-semibold leading-none ">
|
|
|
|
Routers
|
|
|
|
</div>
|
|
|
|
<p className="text-default -mt-1 text-xs leading-normal">
|
2023-02-20 10:07:58 +00:00
|
|
|
{t("modifications_in_fields_warning")}
|
2023-01-04 13:30:46 +00:00
|
|
|
</p>
|
|
|
|
<div className="flex">
|
|
|
|
{form.routers.map((router) => {
|
|
|
|
return (
|
|
|
|
<div key={router.id} className="mr-2">
|
|
|
|
<Link href={`/${appUrl}/route-builder/${router.id}`}>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Badge variant="gray">{router.name}</Badge>
|
2023-01-04 13:30:46 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{connectedForms?.length ? (
|
|
|
|
<div className="mt-6">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-emphasis mb-2 block text-sm font-semibold leading-none ">
|
2023-02-20 10:07:58 +00:00
|
|
|
{t("connected_forms")}
|
2023-01-04 13:30:46 +00:00
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-default -mt-1 text-xs leading-normal">
|
2023-02-20 10:07:58 +00:00
|
|
|
{t("form_modifications_warning")}
|
2023-01-04 13:30:46 +00:00
|
|
|
</p>
|
|
|
|
<div className="flex">
|
|
|
|
{connectedForms.map((router) => {
|
|
|
|
return (
|
|
|
|
<div key={router.id} className="mr-2">
|
|
|
|
<Link href={`/${appUrl}/route-builder/${router.id}`}>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Badge variant="default">{router.name}</Badge>
|
2023-01-04 13:30:46 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
2022-11-10 12:58:07 +00:00
|
|
|
<div className="mt-6">
|
2022-11-14 08:50:09 +00:00
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
data-testid="test-preview"
|
|
|
|
onClick={() => setIsTestPreviewOpen(true)}>
|
2022-11-10 12:58:07 +00:00
|
|
|
{t("test_preview")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{!form._count?.responses && (
|
2023-01-05 12:04:28 +00:00
|
|
|
<>
|
|
|
|
<Alert
|
|
|
|
className="mt-6"
|
|
|
|
severity="neutral"
|
2023-02-20 10:07:58 +00:00
|
|
|
title={t("no_responses_yet")}
|
|
|
|
message={t("responses_collection_waiting_description")}
|
2023-01-05 12:04:28 +00:00
|
|
|
/>
|
|
|
|
</>
|
2022-11-10 12:58:07 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="border-subtle w-full rounded-md border p-8">
|
2022-11-10 12:58:07 +00:00
|
|
|
<RoutingNavBar appUrl={appUrl} form={form} />
|
|
|
|
<Page hookForm={hookForm} form={form} appUrl={appUrl} />
|
|
|
|
</div>
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-10 12:58:07 +00:00
|
|
|
</ShellMain>
|
|
|
|
</FormActionsProvider>
|
|
|
|
</Form>
|
|
|
|
<Dialog open={isTestPreviewOpen} onOpenChange={setIsTestPreviewOpen}>
|
2023-01-25 09:14:20 +00:00
|
|
|
<DialogContent enableOverflow>
|
2022-11-10 12:58:07 +00:00
|
|
|
<DialogHeader title={t("test_routing_form")} subtitle={t("test_preview_description")} />
|
|
|
|
<div>
|
|
|
|
<form
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
testRouting();
|
|
|
|
}}>
|
|
|
|
<div className="px-1">
|
|
|
|
{form && <FormInputFields form={form} response={response} setResponse={setResponse} />}
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
{decidedAction && (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-subtle text-default mt-5 rounded-md p-3">
|
2022-11-10 12:58:07 +00:00
|
|
|
<div className="font-bold ">{t("route_to")}:</div>
|
|
|
|
<div className="mt-2">
|
|
|
|
{RoutingPages.map((page) => {
|
2023-01-06 12:13:56 +00:00
|
|
|
if (page.value !== decidedAction.type) return null;
|
|
|
|
return (
|
|
|
|
<div key={page.value} data-testid="test-routing-result-type">
|
|
|
|
{page.label}
|
|
|
|
</div>
|
|
|
|
);
|
2022-11-10 12:58:07 +00:00
|
|
|
})}
|
|
|
|
:{" "}
|
|
|
|
{decidedAction.type === "customPageMessage" ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-default" data-testid="test-routing-result">
|
2022-11-14 08:50:09 +00:00
|
|
|
{decidedAction.value}
|
|
|
|
</span>
|
2022-11-10 12:58:07 +00:00
|
|
|
) : decidedAction.type === "externalRedirectUrl" ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-default underline">
|
2022-11-10 12:58:07 +00:00
|
|
|
<a
|
|
|
|
target="_blank"
|
2022-11-14 08:50:09 +00:00
|
|
|
data-testid="test-routing-result"
|
2022-11-10 12:58:07 +00:00
|
|
|
href={
|
|
|
|
decidedAction.value.includes("https://") ||
|
|
|
|
decidedAction.value.includes("http://")
|
|
|
|
? decidedAction.value
|
|
|
|
: `http://${decidedAction.value}`
|
|
|
|
}
|
|
|
|
rel="noreferrer">
|
|
|
|
{decidedAction.value}
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
) : (
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-default underline">
|
2022-11-14 08:50:09 +00:00
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
href={`/${decidedAction.value}`}
|
|
|
|
rel="noreferrer"
|
|
|
|
data-testid="test-routing-result">
|
2022-11-10 12:58:07 +00:00
|
|
|
{decidedAction.value}
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<DialogFooter>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogClose
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
setIsTestPreviewOpen(false);
|
|
|
|
setDecidedAction(null);
|
|
|
|
setResponse({});
|
|
|
|
}}>
|
|
|
|
{t("close")}
|
2022-11-10 12:58:07 +00:00
|
|
|
</DialogClose>
|
2022-11-14 08:50:09 +00:00
|
|
|
<Button type="submit" data-testid="test-routing">
|
|
|
|
{t("test_routing")}
|
|
|
|
</Button>
|
2022-11-10 12:58:07 +00:00
|
|
|
</DialogFooter>
|
|
|
|
</form>
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
2022-11-10 12:58:07 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
</>
|
2022-09-02 19:00:41 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-11 15:52:18 +00:00
|
|
|
|
|
|
|
export default function SingleFormWrapper({ form: _form, ...props }: SingleFormComponentProps) {
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: form, isLoading } = trpc.viewer.appRoutingForms.formQuery.useQuery(
|
|
|
|
{ id: _form.id },
|
|
|
|
{
|
|
|
|
initialData: _form,
|
|
|
|
trpc: {},
|
|
|
|
}
|
|
|
|
);
|
2022-10-11 15:52:18 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
// It shouldn't be possible because we are passing the data from SSR to it as initialData. So, no need for skeleton here
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!form) {
|
|
|
|
throw new Error(t("something_went_wrong"));
|
|
|
|
}
|
|
|
|
return <SingleForm form={form} {...props} />;
|
|
|
|
}
|
2022-11-11 09:57:44 +00:00
|
|
|
|
|
|
|
export const getServerSidePropsForSingleFormView = async function getServerSidePropsForSingleFormView(
|
|
|
|
context: AppGetServerSidePropsContext,
|
|
|
|
prisma: AppPrisma,
|
|
|
|
user: AppUser,
|
|
|
|
ssrInit: AppSsrInit
|
|
|
|
) {
|
|
|
|
const ssr = await ssrInit(context);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
permanent: false,
|
|
|
|
destination: "/auth/login",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { params } = context;
|
|
|
|
if (!params) {
|
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const formId = params.appPages[0];
|
|
|
|
if (!formId || params.appPages.length > 1) {
|
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
const isFormEditAllowed = (await import("../lib/isFormEditAllowed")).isFormEditAllowed;
|
|
|
|
if (!(await isFormEditAllowed({ userId: user.id, formId }))) {
|
2022-11-11 09:57:44 +00:00
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const form = await prisma.app_RoutingForms_Form.findUnique({
|
|
|
|
where: {
|
|
|
|
id: formId,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
_count: {
|
|
|
|
select: {
|
|
|
|
responses: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!form) {
|
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
trpcState: ssr.dehydrate(),
|
2023-01-04 13:30:46 +00:00
|
|
|
form: await getSerializableForm(form),
|
2022-11-11 09:57:44 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|