feat: Routing Forms/Teams Support (#9417)

This commit is contained in:
Hariom Balhara authored and GitHub committed 2023-06-15 08:58:07 +00:00
1 parent e513180d7e
commit 53224886e3
45 files changed
+1046 -219

No files matched your search

@@ -21,7 +21,7 @@ export interface Option {
slug: string | null;
}
interface CreateBtnProps {
export type CreateBtnProps = {
options: Option[];
createDialog?: () => JSX.Element;
createFunction?: (teamId?: number) => void;
@@ -29,15 +29,28 @@ interface CreateBtnProps {
buttonText?: string;
isLoading?: boolean;
disableMobileButton?: boolean;
}
"data-testid"?: string;
};
/**
* @deprecated use CreateButtonWithTeamsList instead
*/
export function CreateButton(props: CreateBtnProps) {
const { t } = useLocale();
const router = useRouter();
const {
createDialog,
options,
isLoading,
createFunction,
buttonText,
disableMobileButton,
subtitle,
...restProps
} = props;
const CreateDialog = createDialog ? createDialog() : null;
const CreateDialog = props.createDialog ? props.createDialog() : null;
const hasTeams = !!props.options.find((option) => option.teamId);
const hasTeams = !!options.find((option) => option.teamId);
// inject selection data into url for correct router history
const openModal = (option: Option) => {
@@ -66,33 +79,33 @@ export function CreateButton(props: CreateBtnProps) {
<Button
onClick={() =>
!!CreateDialog
? openModal(props.options[0])
: props.createFunction
? props.createFunction(props.options[0].teamId || undefined)
? openModal(options[0])
: createFunction
? createFunction(options[0].teamId || undefined)
: null
}
data-testid="new-event-type"
StartIcon={Plus}
loading={props.isLoading}
variant={props.disableMobileButton ? "button" : "fab"}>
{props.buttonText ? props.buttonText : t("new")}
loading={isLoading}
variant={disableMobileButton ? "button" : "fab"}
{...restProps}>
{buttonText ? buttonText : t("new")}
</Button>
) : (
<Dropdown>
<DropdownMenuTrigger asChild>
<Button
variant={props.disableMobileButton ? "button" : "fab"}
variant={disableMobileButton ? "button" : "fab"}
StartIcon={Plus}
data-testid="new-event-type-dropdown"
loading={props.isLoading}>
{props.buttonText ? props.buttonText : t("new")}
loading={isLoading}
{...restProps}>
{buttonText ? buttonText : t("new")}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent sideOffset={14} align="end">
<DropdownMenuLabel>
<div className="w-48 text-left text-xs">{props.subtitle}</div>
<div className="w-48 text-left text-xs">{subtitle}</div>
</DropdownMenuLabel>
{props.options.map((option, idx) => (
{options.map((option, idx) => (
<DropdownMenuItem key={option.label}>
<DropdownItem
type="button"
@@ -108,8 +121,8 @@ export function CreateButton(props: CreateBtnProps) {
onClick={() =>
!!CreateDialog
? openModal(option)
: props.createFunction
? props.createFunction(option.teamId || undefined)
: createFunction
? createFunction(option.teamId || undefined)
: null
}>
{" "}
@@ -0,0 +1,22 @@
import { trpc } from "@calcom/trpc/react";
import type { CreateBtnProps } from "./CreateButton";
import { CreateButton } from "./CreateButton";
export function CreateButtonWithTeamsList(props: Omit<CreateBtnProps, "options">) {
const query = trpc.viewer.teamsAndUserProfilesQuery.useQuery();
if (!query.data) return null;
const teamsAndUserProfiles = query.data
.filter((profile) => !profile.readOnly)
.map((profile) => {
return {
teamId: profile.teamId,
label: profile.name || profile.slug,
image: profile.image,
slug: profile.slug,
};
});
return <CreateButton {...props} options={teamsAndUserProfiles} />;
}
@@ -1 +1,2 @@
export { CreateButton } from "./CreateButton";
export { CreateButtonWithTeamsList } from "./CreateButtonWithTeamsList";