2023-02-16 22:39:57 +00:00
|
|
|
import type { App_RoutingForms_Form } from "@prisma/client";
|
2022-09-02 19:00:41 +00:00
|
|
|
import type { NextRouter } from "next/router";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { createContext, forwardRef, useContext, useState } from "react";
|
2022-09-02 19:00:41 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2023-01-04 13:30:46 +00:00
|
|
|
import { Controller } from "react-hook-form";
|
2022-09-02 19:00:41 +00:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { classNames } from "@calcom/lib";
|
|
|
|
import { CAL_URL } from "@calcom/lib/constants";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ButtonProps } from "@calcom/ui";
|
2022-09-02 19:00:41 +00:00
|
|
|
import {
|
2022-11-23 02:55:25 +00:00
|
|
|
Button,
|
|
|
|
ConfirmationDialogContent,
|
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
2022-09-02 19:00:41 +00:00
|
|
|
Dropdown,
|
|
|
|
DropdownMenuContent,
|
2022-11-23 02:55:25 +00:00
|
|
|
DropdownMenuItem,
|
2022-09-02 19:00:41 +00:00
|
|
|
DropdownMenuTrigger,
|
2022-11-23 02:55:25 +00:00
|
|
|
Form,
|
2022-09-02 19:00:41 +00:00
|
|
|
showToast,
|
|
|
|
Switch,
|
2022-11-23 02:55:25 +00:00
|
|
|
TextAreaField,
|
|
|
|
TextField,
|
2023-01-04 13:30:46 +00:00
|
|
|
SettingsToggle,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { MoreHorizontal } from "@calcom/ui/components/icon";
|
2022-09-02 19:00:41 +00:00
|
|
|
|
|
|
|
import { EmbedButton, EmbedDialog } from "@components/Embed";
|
|
|
|
|
2022-09-18 10:47:31 +00:00
|
|
|
import getFieldIdentifier from "../lib/getFieldIdentifier";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { SerializableForm } from "../types/types";
|
2022-09-02 19:00:41 +00:00
|
|
|
|
|
|
|
type RoutingForm = SerializableForm<App_RoutingForms_Form>;
|
|
|
|
|
|
|
|
const newFormModalQuerySchema = z.object({
|
2023-06-15 08:58:07 +00:00
|
|
|
action: z.literal("new").or(z.literal("duplicate")),
|
2022-09-02 19:00:41 +00:00
|
|
|
target: z.string().optional(),
|
|
|
|
});
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
const openModal = (router: NextRouter, option: z.infer<typeof newFormModalQuerySchema>) => {
|
2022-09-02 19:00:41 +00:00
|
|
|
const query = {
|
|
|
|
...router.query,
|
|
|
|
dialog: "new-form",
|
|
|
|
...option,
|
|
|
|
};
|
|
|
|
router.push(
|
|
|
|
{
|
|
|
|
pathname: router.pathname,
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
{ shallow: true }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function NewFormDialog({ appUrl }: { appUrl: string }) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const mutation = trpc.viewer.appRoutingForms.formMutation.useMutation({
|
2022-09-02 19:00:41 +00:00
|
|
|
onSuccess: (_data, variables) => {
|
|
|
|
router.push(`${appUrl}/form-edit/${variables.id}`);
|
|
|
|
},
|
2023-06-15 08:58:07 +00:00
|
|
|
onError: (err) => {
|
|
|
|
showToast(err.message || t("something_went_wrong"), "error");
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
2022-10-06 06:49:03 +00:00
|
|
|
onSettled: () => {
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.appRoutingForms.forms.invalidate();
|
2022-10-06 06:49:03 +00:00
|
|
|
},
|
2022-09-02 19:00:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const hookForm = useForm<{
|
|
|
|
name: string;
|
|
|
|
description: string;
|
2023-01-04 13:30:46 +00:00
|
|
|
shouldConnect: boolean;
|
2022-09-02 19:00:41 +00:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const { action, target } = router.query as z.infer<typeof newFormModalQuerySchema>;
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
const formToDuplicate = action === "duplicate" ? target : null;
|
|
|
|
const teamId = action === "new" ? Number(target) : null;
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
const { register } = hookForm;
|
|
|
|
return (
|
|
|
|
<Dialog name="new-form" clearQueryParamsOnClose={["target", "action"]}>
|
|
|
|
<DialogContent className="overflow-y-auto">
|
|
|
|
<div className="mb-4">
|
2023-04-05 18:14:46 +00:00
|
|
|
<h3 className="text-emphasis text-lg font-bold leading-6" id="modal-title">
|
2023-06-15 08:58:07 +00:00
|
|
|
{teamId ? t("add_new_team_form") : t("add_new_form")}
|
2022-09-02 19:00:41 +00:00
|
|
|
</h3>
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-subtle text-sm">{t("form_description")}</p>
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Form
|
|
|
|
form={hookForm}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
const formId = uuidv4();
|
2023-01-04 13:30:46 +00:00
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
mutation.mutate({
|
|
|
|
id: formId,
|
|
|
|
...values,
|
|
|
|
addFallback: true,
|
2023-06-15 08:58:07 +00:00
|
|
|
teamId,
|
|
|
|
duplicateFrom: formToDuplicate,
|
2022-09-02 19:00:41 +00:00
|
|
|
});
|
|
|
|
}}>
|
|
|
|
<div className="mt-3 space-y-4">
|
2023-02-20 10:07:58 +00:00
|
|
|
<TextField label={t("title")} required placeholder={t("a_routing_form")} {...register("name")} />
|
2022-09-02 19:00:41 +00:00
|
|
|
<div className="mb-5">
|
2022-09-08 17:40:09 +00:00
|
|
|
<TextAreaField
|
|
|
|
id="description"
|
|
|
|
label={t("description")}
|
|
|
|
{...register("description")}
|
|
|
|
data-testid="description"
|
2023-02-20 10:07:58 +00:00
|
|
|
placeholder={t("form_description_placeholder")}
|
2022-09-08 17:40:09 +00:00
|
|
|
/>
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
2023-01-04 13:30:46 +00:00
|
|
|
{action === "duplicate" && (
|
|
|
|
<Controller
|
|
|
|
name="shouldConnect"
|
|
|
|
render={({ field: { value, onChange } }) => {
|
|
|
|
return (
|
|
|
|
<SettingsToggle
|
2023-02-20 10:07:58 +00:00
|
|
|
title={t("keep_me_connected_with_form")}
|
|
|
|
description={t("fields_in_form_duplicated")}
|
2023-01-04 13:30:46 +00:00
|
|
|
checked={value}
|
|
|
|
onCheckedChange={(checked) => {
|
|
|
|
onChange(checked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
|
|
|
<div className="mt-8 flex flex-row-reverse gap-x-2">
|
|
|
|
<Button loading={mutation.isLoading} data-testid="add-form" type="submit">
|
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogClose />
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const dropdownCtx = createContext<{ dropdown: boolean }>({ dropdown: false });
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
export const FormActionsDropdown = ({
|
|
|
|
children,
|
|
|
|
disabled,
|
|
|
|
}: {
|
|
|
|
disabled?: boolean;
|
|
|
|
children: React.ReactNode;
|
|
|
|
}) => {
|
2022-09-02 19:00:41 +00:00
|
|
|
return (
|
|
|
|
<dropdownCtx.Provider value={{ dropdown: true }}>
|
|
|
|
<Dropdown>
|
2023-06-15 08:58:07 +00:00
|
|
|
<DropdownMenuTrigger disabled={disabled} data-testid="form-dropdown" asChild>
|
2022-09-02 19:00:41 +00:00
|
|
|
<Button
|
|
|
|
type="button"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2022-09-09 11:45:20 +00:00
|
|
|
color="secondary"
|
2022-10-20 11:35:12 +00:00
|
|
|
className={classNames("radix-state-open:rounded-r-md", disabled && "opacity-30")}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={MoreHorizontal}
|
2022-09-02 19:00:41 +00:00
|
|
|
/>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>{children}</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
</dropdownCtx.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function Dialogs({
|
|
|
|
appUrl,
|
|
|
|
deleteDialogOpen,
|
|
|
|
setDeleteDialogOpen,
|
|
|
|
deleteDialogFormId,
|
|
|
|
}: {
|
|
|
|
appUrl: string;
|
|
|
|
deleteDialogOpen: boolean;
|
|
|
|
setDeleteDialogOpen: (open: boolean) => void;
|
|
|
|
deleteDialogFormId: string | null;
|
|
|
|
}) {
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const router = useRouter();
|
2023-02-20 10:07:58 +00:00
|
|
|
const { t } = useLocale();
|
2022-11-10 23:40:01 +00:00
|
|
|
const deleteMutation = trpc.viewer.appRoutingForms.deleteForm.useMutation({
|
2022-10-06 06:49:03 +00:00
|
|
|
onMutate: async ({ id: formId }) => {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.appRoutingForms.forms.cancel();
|
|
|
|
const previousValue = utils.viewer.appRoutingForms.forms.getData();
|
2022-10-06 06:49:03 +00:00
|
|
|
if (previousValue) {
|
2023-06-15 08:58:07 +00:00
|
|
|
const filtered = previousValue.filtered.filter(({ form: { id } }) => id !== formId);
|
|
|
|
utils.viewer.appRoutingForms.forms.setData(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
...previousValue,
|
|
|
|
filtered,
|
|
|
|
}
|
|
|
|
);
|
2022-10-06 06:49:03 +00:00
|
|
|
}
|
|
|
|
return { previousValue };
|
|
|
|
},
|
2022-09-02 19:00:41 +00:00
|
|
|
onSuccess: () => {
|
2023-02-20 10:07:58 +00:00
|
|
|
showToast(t("form_deleted"), "success");
|
2022-09-02 19:00:41 +00:00
|
|
|
setDeleteDialogOpen(false);
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.appRoutingForms.forms.invalidate();
|
2022-09-02 19:00:41 +00:00
|
|
|
setDeleteDialogOpen(false);
|
|
|
|
},
|
2022-10-06 06:49:03 +00:00
|
|
|
onError: (err, newTodo, context) => {
|
|
|
|
if (context?.previousValue) {
|
2023-06-15 08:58:07 +00:00
|
|
|
utils.viewer.appRoutingForms.forms.setData({}, context.previousValue);
|
2022-10-06 06:49:03 +00:00
|
|
|
}
|
2023-02-20 10:07:58 +00:00
|
|
|
showToast(err.message || t("something_went_wrong"), "error");
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div id="form-dialogs">
|
|
|
|
<EmbedDialog />
|
|
|
|
<Dialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
|
|
|
|
<ConfirmationDialogContent
|
|
|
|
isLoading={deleteMutation.isLoading}
|
|
|
|
variety="danger"
|
2023-02-20 10:07:58 +00:00
|
|
|
title={t("delete_form")}
|
2023-03-08 12:39:58 +00:00
|
|
|
confirmBtnText={t("delete_form_action")}
|
2023-02-20 10:07:58 +00:00
|
|
|
loadingText={t("delete_form_action")}
|
2022-09-02 19:00:41 +00:00
|
|
|
onConfirm={(e) => {
|
|
|
|
if (!deleteDialogFormId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
deleteMutation.mutate({
|
|
|
|
id: deleteDialogFormId,
|
|
|
|
});
|
|
|
|
}}>
|
2023-03-08 12:39:58 +00:00
|
|
|
<ul className="list-disc pl-3">
|
|
|
|
<li> {t("delete_form_confirmation")}</li>
|
|
|
|
<li> {t("delete_form_confirmation_2")}</li>
|
|
|
|
</ul>
|
2022-09-02 19:00:41 +00:00
|
|
|
</ConfirmationDialogContent>
|
|
|
|
</Dialog>
|
|
|
|
<NewFormDialog appUrl={appUrl} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const actionsCtx = createContext({
|
|
|
|
appUrl: "",
|
|
|
|
_delete: {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|
|
|
|
onAction: (_arg: { routingForm: RoutingForm | null }) => {},
|
|
|
|
isLoading: false,
|
|
|
|
},
|
|
|
|
toggle: {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|
|
|
|
onAction: (_arg: { routingForm: RoutingForm | null; checked: boolean }) => {},
|
|
|
|
isLoading: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export function FormActionsProvider({ appUrl, children }: { appUrl: string; children: React.ReactNode }) {
|
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
|
|
|
const [deleteDialogFormId, setDeleteDialogFormId] = useState<string | null>(null);
|
2023-02-20 10:07:58 +00:00
|
|
|
const { t } = useLocale();
|
2022-12-07 20:55:41 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-09-02 19:00:41 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const toggleMutation = trpc.viewer.appRoutingForms.formMutation.useMutation({
|
2022-12-07 20:55:41 +00:00
|
|
|
onMutate: async ({ id: formId, disabled }) => {
|
|
|
|
await utils.viewer.appRoutingForms.forms.cancel();
|
|
|
|
const previousValue = utils.viewer.appRoutingForms.forms.getData();
|
|
|
|
if (previousValue) {
|
2023-06-15 08:58:07 +00:00
|
|
|
const formIndex = previousValue.filtered.findIndex(({ form: { id } }) => id === formId);
|
|
|
|
const previousListOfForms = [...previousValue.filtered];
|
2022-12-07 20:55:41 +00:00
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
if (formIndex !== -1 && previousListOfForms[formIndex] && disabled !== undefined) {
|
|
|
|
previousListOfForms[formIndex].form.disabled = disabled;
|
2022-12-07 20:55:41 +00:00
|
|
|
}
|
2023-06-15 08:58:07 +00:00
|
|
|
utils.viewer.appRoutingForms.forms.setData(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
filtered: previousListOfForms,
|
|
|
|
totalCount: previousValue.totalCount,
|
|
|
|
}
|
|
|
|
);
|
2022-12-07 20:55:41 +00:00
|
|
|
}
|
|
|
|
return { previousValue };
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
2023-05-17 08:47:48 +00:00
|
|
|
onSuccess: () => {
|
|
|
|
showToast(t("form_updated_successfully"), "success");
|
|
|
|
},
|
2023-04-17 12:04:25 +00:00
|
|
|
onSettled: (routingForm) => {
|
2022-12-07 20:55:41 +00:00
|
|
|
utils.viewer.appRoutingForms.forms.invalidate();
|
2023-04-17 12:04:25 +00:00
|
|
|
if (routingForm) {
|
|
|
|
utils.viewer.appRoutingForms.formQuery.invalidate({
|
|
|
|
id: routingForm.id,
|
|
|
|
});
|
|
|
|
}
|
2022-12-07 20:55:41 +00:00
|
|
|
},
|
|
|
|
onError: (err, value, context) => {
|
|
|
|
if (context?.previousValue) {
|
2023-06-15 08:58:07 +00:00
|
|
|
utils.viewer.appRoutingForms.forms.setData({}, context.previousValue);
|
2022-12-07 20:55:41 +00:00
|
|
|
}
|
2023-06-15 08:58:07 +00:00
|
|
|
showToast(err.message || t("something_went_wrong"), "error");
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<actionsCtx.Provider
|
|
|
|
value={{
|
|
|
|
appUrl,
|
|
|
|
_delete: {
|
|
|
|
onAction: ({ routingForm }) => {
|
|
|
|
if (!routingForm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setDeleteDialogOpen(true);
|
|
|
|
setDeleteDialogFormId(routingForm.id);
|
|
|
|
},
|
|
|
|
isLoading: false,
|
|
|
|
},
|
|
|
|
toggle: {
|
|
|
|
onAction: ({ routingForm, checked }) => {
|
|
|
|
if (!routingForm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
toggleMutation.mutate({
|
|
|
|
...routingForm,
|
|
|
|
disabled: !checked,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
isLoading: toggleMutation.isLoading,
|
|
|
|
},
|
|
|
|
}}>
|
|
|
|
{children}
|
|
|
|
</actionsCtx.Provider>
|
|
|
|
<Dialogs
|
|
|
|
appUrl={appUrl}
|
|
|
|
deleteDialogFormId={deleteDialogFormId}
|
|
|
|
deleteDialogOpen={deleteDialogOpen}
|
|
|
|
setDeleteDialogOpen={setDeleteDialogOpen}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type FormActionType =
|
|
|
|
| "preview"
|
|
|
|
| "edit"
|
|
|
|
| "copyLink"
|
|
|
|
| "toggle"
|
|
|
|
| "_delete"
|
|
|
|
| "embed"
|
|
|
|
| "duplicate"
|
|
|
|
| "download"
|
2022-09-18 10:47:31 +00:00
|
|
|
| "copyRedirectUrl"
|
2022-09-02 19:00:41 +00:00
|
|
|
| "create";
|
|
|
|
|
|
|
|
type FormActionProps<T> = {
|
|
|
|
routingForm: RoutingForm | null;
|
|
|
|
as?: T;
|
|
|
|
label?: string;
|
|
|
|
//TODO: Provide types here
|
|
|
|
action: FormActionType;
|
|
|
|
children?: React.ReactNode;
|
2023-06-15 08:58:07 +00:00
|
|
|
render?: (props: {
|
|
|
|
routingForm: RoutingForm | null;
|
|
|
|
className?: string;
|
|
|
|
label?: string;
|
|
|
|
disabled?: boolean | null | undefined;
|
|
|
|
}) => JSX.Element;
|
2023-04-19 20:17:54 +00:00
|
|
|
extraClassNames?: string;
|
2022-09-02 19:00:41 +00:00
|
|
|
} & ButtonProps;
|
|
|
|
|
|
|
|
export const FormAction = forwardRef(function FormAction<T extends typeof Button>(
|
|
|
|
props: FormActionProps<T>,
|
|
|
|
forwardedRef: React.ForwardedRef<HTMLAnchorElement | HTMLButtonElement>
|
|
|
|
) {
|
2023-04-19 20:17:54 +00:00
|
|
|
const {
|
|
|
|
action: actionName,
|
|
|
|
routingForm,
|
|
|
|
children,
|
|
|
|
as: asFromElement,
|
|
|
|
extraClassNames,
|
|
|
|
...additionalProps
|
|
|
|
} = props;
|
2022-09-02 19:00:41 +00:00
|
|
|
const { appUrl, _delete, toggle } = useContext(actionsCtx);
|
|
|
|
const dropdownCtxValue = useContext(dropdownCtx);
|
|
|
|
const dropdown = dropdownCtxValue?.dropdown;
|
|
|
|
const embedLink = `forms/${routingForm?.id}`;
|
|
|
|
const formLink = `${CAL_URL}/${embedLink}`;
|
2022-09-18 10:47:31 +00:00
|
|
|
let redirectUrl = `${CAL_URL}/router?form=${routingForm?.id}`;
|
|
|
|
|
|
|
|
routingForm?.fields?.forEach((field) => {
|
|
|
|
redirectUrl += `&${getFieldIdentifier(field)}={Recalled_Response_For_This_Field}`;
|
|
|
|
});
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
|
|
|
const actionData: Record<
|
|
|
|
FormActionType,
|
|
|
|
ButtonProps & { as?: React.ElementType; render?: FormActionProps<unknown>["render"] }
|
|
|
|
> = {
|
|
|
|
preview: {
|
|
|
|
href: formLink,
|
|
|
|
},
|
|
|
|
copyLink: {
|
|
|
|
onClick: () => {
|
|
|
|
showToast(t("link_copied"), "success");
|
|
|
|
navigator.clipboard.writeText(formLink);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
duplicate: {
|
|
|
|
onClick: () => openModal(router, { action: "duplicate", target: routingForm?.id }),
|
|
|
|
},
|
|
|
|
embed: {
|
|
|
|
as: EmbedButton,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
//@ts-ignore
|
|
|
|
embedUrl: embedLink,
|
|
|
|
},
|
|
|
|
edit: {
|
|
|
|
href: `${appUrl}/form-edit/${routingForm?.id}`,
|
|
|
|
},
|
|
|
|
download: {
|
2022-09-22 17:23:43 +00:00
|
|
|
href: `/api/integrations/routing-forms/responses/${routingForm?.id}`,
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
|
|
|
_delete: {
|
|
|
|
onClick: () => _delete.onAction({ routingForm }),
|
|
|
|
loading: _delete.isLoading,
|
|
|
|
},
|
|
|
|
create: {
|
2023-06-15 08:58:07 +00:00
|
|
|
onClick: () => createAction({ router, teamId: null }),
|
2022-09-02 19:00:41 +00:00
|
|
|
},
|
2022-09-18 10:47:31 +00:00
|
|
|
copyRedirectUrl: {
|
|
|
|
onClick: () => {
|
|
|
|
navigator.clipboard.writeText(redirectUrl);
|
2023-02-20 10:07:58 +00:00
|
|
|
showToast(t("typeform_redirect_url_copied"), "success");
|
2022-09-18 10:47:31 +00:00
|
|
|
},
|
|
|
|
},
|
2022-09-02 19:00:41 +00:00
|
|
|
toggle: {
|
2023-06-15 08:58:07 +00:00
|
|
|
render: ({ routingForm, label = "", disabled, ...restProps }) => {
|
2022-09-02 19:00:41 +00:00
|
|
|
if (!routingForm) {
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
return (
|
2023-04-19 20:17:54 +00:00
|
|
|
<div
|
|
|
|
{...restProps}
|
|
|
|
className={classNames(
|
|
|
|
"sm:hover:bg-subtle self-center rounded-md p-2 hover:bg-gray-200",
|
|
|
|
extraClassNames
|
|
|
|
)}>
|
2022-09-02 19:00:41 +00:00
|
|
|
<Switch
|
2023-06-15 08:58:07 +00:00
|
|
|
disabled={!!disabled}
|
2022-09-02 19:00:41 +00:00
|
|
|
checked={!routingForm.disabled}
|
|
|
|
label={label}
|
|
|
|
onCheckedChange={(checked) => toggle.onAction({ routingForm, checked })}
|
2023-04-19 20:17:54 +00:00
|
|
|
labelOnLeading
|
2022-09-02 19:00:41 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
loading: toggle.isLoading,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const { as: asFromAction, ...action } = actionData[actionName];
|
|
|
|
const as = asFromElement || asFromAction;
|
|
|
|
const actionProps = {
|
|
|
|
...action,
|
|
|
|
...(additionalProps as ButtonProps),
|
|
|
|
} as ButtonProps & { render?: FormActionProps<unknown>["render"] };
|
|
|
|
|
|
|
|
if (actionProps.render) {
|
|
|
|
return actionProps.render({
|
|
|
|
routingForm,
|
|
|
|
...additionalProps,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const Component = as || Button;
|
|
|
|
if (!dropdown) {
|
|
|
|
return (
|
2023-05-17 08:47:48 +00:00
|
|
|
<Component data-testid={`form-action-${actionName}`} ref={forwardedRef} {...actionProps}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{children}
|
|
|
|
</Component>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<DropdownMenuItem>
|
2023-01-17 17:12:22 +00:00
|
|
|
<Component
|
|
|
|
ref={forwardedRef}
|
|
|
|
{...actionProps}
|
|
|
|
className={classNames(
|
|
|
|
props.className,
|
|
|
|
"w-full transition-none",
|
|
|
|
props.color === "destructive" && "border-0"
|
|
|
|
)}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{children}
|
|
|
|
</Component>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
);
|
|
|
|
});
|
2023-06-15 08:58:07 +00:00
|
|
|
|
|
|
|
export const createAction = ({ router, teamId }: { router: NextRouter; teamId: number | null }) => {
|
|
|
|
openModal(router, { action: "new", target: teamId ? String(teamId) : "" });
|
|
|
|
};
|