feat: attributes (#15964)
Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: Omar López <[email protected]>
This commit is contained in:
41 files changed
+1877
-39
No files matched your search
@@ -0,0 +1,152 @@
|
||||
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import React, { useState } from "react";
|
||||
import { Controller, useForm, useFieldArray } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
SelectField,
|
||||
InputField,
|
||||
Label,
|
||||
Input,
|
||||
Dialog,
|
||||
ConfirmationDialogContent,
|
||||
} from "@calcom/ui";
|
||||
|
||||
const AttributeSchema = z.object({
|
||||
attrName: z.string().min(1),
|
||||
type: z.enum(["TEXT", "NUMBER", "SINGLE_SELECT", "MULTI_SELECT"]),
|
||||
options: z.array(
|
||||
z.object({ value: z.string(), id: z.string().optional(), assignedUsers: z.number().optional() })
|
||||
),
|
||||
});
|
||||
|
||||
type FormValues = z.infer<typeof AttributeSchema>;
|
||||
|
||||
const AttributeTypeOptions = [
|
||||
{ value: "TEXT", label: "Text" },
|
||||
{ value: "NUMBER", label: "Number" },
|
||||
{ value: "SINGLE_SELECT", label: "Single Select" },
|
||||
{ value: "MULTI_SELECT", label: "Multi Select" },
|
||||
];
|
||||
|
||||
interface AttributeFormProps {
|
||||
initialValues?: FormValues;
|
||||
onSubmit: (values: FormValues) => void;
|
||||
header: React.ReactNode;
|
||||
}
|
||||
|
||||
export function AttributeForm({ initialValues, onSubmit, header }: AttributeFormProps) {
|
||||
const { t } = useLocale();
|
||||
const [deleteOptionDialog, setDeleteOptionDialog] = useState<{
|
||||
id: number | undefined;
|
||||
open: boolean;
|
||||
}>({
|
||||
id: undefined,
|
||||
open: false,
|
||||
});
|
||||
const [listRef] = useAutoAnimate<HTMLDivElement>({
|
||||
duration: 300,
|
||||
easing: "ease-in-out",
|
||||
});
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(AttributeSchema),
|
||||
defaultValues: initialValues || {
|
||||
attrName: "",
|
||||
options: [{ value: "" }],
|
||||
type: "TEXT",
|
||||
},
|
||||
});
|
||||
|
||||
const { fields, append, remove } = useFieldArray({
|
||||
control: form.control,
|
||||
name: "options",
|
||||
});
|
||||
|
||||
const watchedType = form.watch("type");
|
||||
|
||||
return (
|
||||
<Form
|
||||
form={form}
|
||||
className="flex flex-col space-y-4 lg:space-y-6"
|
||||
handleSubmit={(values) => {
|
||||
onSubmit({
|
||||
...values,
|
||||
options: values.options,
|
||||
});
|
||||
}}>
|
||||
{header}
|
||||
<InputField label={t("name")} required {...form.register("attrName")} />
|
||||
<Controller
|
||||
name="type"
|
||||
render={({ field: { value, onChange } }) => (
|
||||
<SelectField
|
||||
label="Type"
|
||||
options={AttributeTypeOptions}
|
||||
onChange={(option) => {
|
||||
if (!option) return;
|
||||
onChange(option.value);
|
||||
}}
|
||||
value={AttributeTypeOptions.find((attr) => attr.value === value)}
|
||||
required
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
{["SINGLE_SELECT", "MULTI_SELECT"].includes(watchedType) && (
|
||||
<div className="bg-muted border-muted mt-6 rounded-lg border p-6">
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>{t("options")}</Label>
|
||||
<div ref={listRef}>
|
||||
{fields.map((field, index) => (
|
||||
<div className="flex items-center gap-2" key={field.id}>
|
||||
<Input {...form.register(`options.${index}.value`)} className="w-full" />
|
||||
<Input {...form.register(`options.${index}.id`)} className="hidden" />
|
||||
<Button
|
||||
type="button"
|
||||
variant="icon"
|
||||
StartIcon="x"
|
||||
color="minimal"
|
||||
className="mb-2"
|
||||
disabled={index === 0 && fields.length === 1}
|
||||
onClick={() => {
|
||||
if (field.assignedUsers && field.assignedUsers > 0) {
|
||||
setDeleteOptionDialog({ id: index, open: true });
|
||||
} else {
|
||||
remove(index);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<Button type="button" StartIcon="plus" color="secondary" onClick={() => append({ value: "" })}>
|
||||
{t("new_option")}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
<Dialog
|
||||
open={deleteOptionDialog.open}
|
||||
onOpenChange={() => setDeleteOptionDialog({ id: undefined, open: false })}>
|
||||
<ConfirmationDialogContent
|
||||
title={t("delete_attribute")}
|
||||
confirmBtnText={t("delete")}
|
||||
onConfirm={() => {
|
||||
remove(deleteOptionDialog.id as number);
|
||||
setDeleteOptionDialog({ id: undefined, open: false });
|
||||
}}
|
||||
loadingText={t("deleting_attribute")}>
|
||||
<>
|
||||
{t("delete_attribute_description", {
|
||||
numberOfUsers: fields[deleteOptionDialog.id as number]?.assignedUsers || 0,
|
||||
})}
|
||||
</>
|
||||
</ConfirmationDialogContent>
|
||||
</Dialog>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { SkeletonText } from "@calcom/ui";
|
||||
|
||||
export function ListSkeleton() {
|
||||
return (
|
||||
<ul className="focus-within:border-emphasis flex justify-between p-4">
|
||||
{Array.from({ length: 3 }).map((_, idx) => (
|
||||
<li key={idx}>
|
||||
<AttributeSkeleton />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
function AttributeSkeleton() {
|
||||
return (
|
||||
<div>
|
||||
<SkeletonText className="text-sm font-semibold leading-none" />
|
||||
<SkeletonText className="text-default inline-flex items-center gap-1 text-sm font-normal leading-none" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import React from "react";
|
||||
import { useFormContext } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
|
||||
import SettingsLayout from "@calcom/features/settings/layouts/SettingsLayout";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { Meta, Button, useMeta, showToast } from "@calcom/ui";
|
||||
|
||||
import { AttributeForm } from "./AttributesForm";
|
||||
|
||||
const CreateAttributeSchema = z.object({
|
||||
// Calling this name would make sense but conflicts with rhf "watch" "name" field
|
||||
attrName: z.string().min(1),
|
||||
type: z.enum(["TEXT", "NUMBER", "SINGLE_SELECT", "MULTI_SELECT"]),
|
||||
options: z.array(z.object({ value: z.string(), id: z.string() })),
|
||||
});
|
||||
|
||||
type FormValues = z.infer<typeof CreateAttributeSchema>;
|
||||
|
||||
function CreateAttributesPage() {
|
||||
const router = useRouter();
|
||||
const mutation = trpc.viewer.attributes.create.useMutation({
|
||||
onSuccess: () => {
|
||||
showToast("Attribute created successfully", "success");
|
||||
router.push("/settings/organizations/attributes");
|
||||
},
|
||||
onError: (err) => {
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
});
|
||||
const { t } = useLocale();
|
||||
|
||||
return (
|
||||
<>
|
||||
<LicenseRequired>
|
||||
<Meta title="Attribute" description="Create an attribute for your team members" />
|
||||
<AttributeForm
|
||||
header={<CreateAttributeHeader isPending={mutation.isPending} />}
|
||||
onSubmit={(values) => {
|
||||
// Create set of attributes to get unique values
|
||||
const uniqueAttributes = new Set(values.options.map((option) => option.value));
|
||||
mutation.mutate({
|
||||
name: values.attrName,
|
||||
type: values.type,
|
||||
options: Array.from(uniqueAttributes).map((value) => ({ value })),
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</LicenseRequired>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function CreateAttributeHeader(props: { isPending: boolean }) {
|
||||
const { meta } = useMeta();
|
||||
const { t } = useLocale();
|
||||
const formContext = useFormContext<FormValues>();
|
||||
|
||||
const watchedTitle = formContext.watch("attrName");
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="mb-6 mt-6 flex flex-grow items-center justify-between lg:mt-12">
|
||||
<div className="-ml-12 flex items-center gap-4">
|
||||
<Button
|
||||
variant="icon"
|
||||
StartIcon="arrow-left"
|
||||
color="minimal"
|
||||
href="/settings/organizations/attributes">
|
||||
<span className="sr-only">{t("back_to_attributes")}</span>
|
||||
</Button>
|
||||
<div className="font-cal text-cal flex space-x-1 text-xl font-semibold leading-none">
|
||||
<h1 className="text-emphasis">{meta.title}</h1>
|
||||
{watchedTitle && (
|
||||
<>
|
||||
<span className="text-subtle">/</span> <span className="text-emphasis">{watchedTitle}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Button type="submit" data-testid="create-attribute-button" loading={props.isPending}>
|
||||
{t("save")}
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
function getLayout(page: React.ReactElement) {
|
||||
return <SettingsLayout hideHeader>{page}</SettingsLayout>;
|
||||
}
|
||||
|
||||
CreateAttributesPage.getLayout = getLayout;
|
||||
|
||||
export default CreateAttributesPage;
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter, useParams } from "next/navigation";
|
||||
import React from "react";
|
||||
import { useFormContext } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
|
||||
import SettingsLayout from "@calcom/features/settings/layouts/SettingsLayout";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { Meta, Button, useMeta, showToast } from "@calcom/ui";
|
||||
|
||||
import { AttributeForm } from "./AttributesForm";
|
||||
|
||||
const CreateAttributeSchema = z.object({
|
||||
// Calling this name would make sense but conflicts with rhf "watch" "name" field
|
||||
attrName: z.string().min(1),
|
||||
type: z.enum(["TEXT", "NUMBER", "SINGLE_SELECT", "MULTI_SELECT"]),
|
||||
options: z.array(z.object({ value: z.string(), id: z.string() })),
|
||||
});
|
||||
|
||||
type FormValues = z.infer<typeof CreateAttributeSchema>;
|
||||
|
||||
function CreateAttributesPage() {
|
||||
const router = useRouter();
|
||||
const utils = trpc.useUtils();
|
||||
const { t } = useLocale();
|
||||
// Get the attribute id from the url
|
||||
const { id } = useParams<{ id: string }>();
|
||||
// ensure string with zod
|
||||
const attribute = trpc.viewer.attributes.get.useQuery({ id: id as string });
|
||||
|
||||
const mutation = trpc.viewer.attributes.edit.useMutation({
|
||||
onSuccess: () => {
|
||||
showToast(t("attribute_edited_successfully"), "success");
|
||||
utils.viewer.attributes.get.invalidate({
|
||||
id: id as string,
|
||||
});
|
||||
utils.viewer.attributes.list.invalidate();
|
||||
router.push("/settings/organizations/attributes");
|
||||
},
|
||||
onError: (err) => {
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<LicenseRequired>
|
||||
<Meta title="Attribute" description={t("edit_attribute_description")} />
|
||||
{!attribute.isLoading && attribute.data ? (
|
||||
<AttributeForm
|
||||
initialValues={{
|
||||
attrName: attribute.data.name,
|
||||
type: attribute.data.type,
|
||||
options: attribute.data.options,
|
||||
}}
|
||||
header={<EditAttributeHeader isPending={mutation.isPending} />}
|
||||
onSubmit={(values) => {
|
||||
mutation.mutate({
|
||||
attributeId: id as string,
|
||||
name: values.attrName,
|
||||
type: values.type,
|
||||
options: values.options,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<>Loading</>
|
||||
)}
|
||||
</LicenseRequired>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function EditAttributeHeader(props: { isPending: boolean }) {
|
||||
const { meta } = useMeta();
|
||||
const { t } = useLocale();
|
||||
const formContext = useFormContext<FormValues>();
|
||||
|
||||
const watchedTitle = formContext.watch("attrName");
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="mb-6 mt-6 flex flex-grow items-center justify-between lg:mt-12">
|
||||
<div className="-ml-12 flex items-center gap-4">
|
||||
<Button
|
||||
variant="icon"
|
||||
StartIcon="arrow-left"
|
||||
color="minimal"
|
||||
href="/settings/organizations/attributes">
|
||||
<span className="sr-only">{t("back_to_attributes")}</span>
|
||||
</Button>
|
||||
<div className="font-cal text-cal flex space-x-1 text-xl font-semibold leading-none">
|
||||
<h1 className="text-emphasis">{meta.title}</h1>
|
||||
{watchedTitle && (
|
||||
<>
|
||||
<span className="text-subtle">/</span> <span className="text-emphasis">{watchedTitle}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Button type="submit" data-testid="create-attribute-button" loading={props.isPending}>
|
||||
Save
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function getLayout(page: React.ReactElement) {
|
||||
return <SettingsLayout hideHeader>{page}</SettingsLayout>;
|
||||
}
|
||||
|
||||
CreateAttributesPage.getLayout = getLayout;
|
||||
|
||||
export default CreateAttributesPage;
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
|
||||
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
|
||||
import SettingsLayout from "@calcom/features/settings/layouts/SettingsLayout";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import {
|
||||
Meta,
|
||||
Switch,
|
||||
Dropdown,
|
||||
Button,
|
||||
DropdownMenuTrigger,
|
||||
DropdownMenuContent,
|
||||
DropdownItem,
|
||||
DropdownMenuItem,
|
||||
Icon,
|
||||
showToast,
|
||||
useMeta,
|
||||
} from "@calcom/ui";
|
||||
|
||||
import { ListSkeleton } from "./ListSkeleton";
|
||||
|
||||
type AttributeItemProps = RouterOutputs["viewer"]["attributes"]["list"][number];
|
||||
|
||||
const TypeToLabelMap = {
|
||||
TEXT: "Text",
|
||||
NUMBER: "Number",
|
||||
SINGLE_SELECT: "Single-select",
|
||||
MULTI_SELECT: "Multi-select",
|
||||
};
|
||||
|
||||
function AttributeItem({ attribute }: { attribute: AttributeItemProps }) {
|
||||
const utils = trpc.useUtils();
|
||||
const { t } = useLocale();
|
||||
const [isEnabled, setIsEnabled] = useState(attribute.enabled);
|
||||
const mutation = trpc.viewer.attributes.toggleActive.useMutation({
|
||||
onSuccess: () => {
|
||||
showToast(t("attribute_updated_successfully"), "success");
|
||||
},
|
||||
onError: (err) => {
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
});
|
||||
|
||||
const deleteMutation = trpc.viewer.attributes.delete.useMutation({
|
||||
onSuccess: () => {
|
||||
showToast(t("attribute_deleted_successfully"), "success");
|
||||
utils.viewer.attributes.list.invalidate();
|
||||
},
|
||||
onError: (err) => {
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
});
|
||||
|
||||
const handleToggle = (checked: boolean) => {
|
||||
// Optimistically update the local state
|
||||
setIsEnabled(checked);
|
||||
mutation.mutate(
|
||||
{ attributeId: attribute.id },
|
||||
{
|
||||
onError: (err) => {
|
||||
setIsEnabled(!checked);
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
deleteMutation.mutate({ id: attribute.id });
|
||||
};
|
||||
|
||||
return (
|
||||
<ul className="focus-within:border-emphasis flex justify-between p-4" key={attribute.id}>
|
||||
<div>
|
||||
<h3 className="text-sm font-semibold leading-none">{attribute.name}</h3>
|
||||
<p className="text-default inline-flex items-center gap-1 text-sm font-normal leading-none">
|
||||
{TypeToLabelMap[attribute.type]}
|
||||
{attribute.options?.length > 0 && (
|
||||
<>
|
||||
<span className="text-muted">•</span>
|
||||
<span>{t("number_of_options", { count: attribute.options.length })}</span>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-4">
|
||||
<Switch checked={isEnabled} onCheckedChange={handleToggle} />
|
||||
<Dropdown modal={false}>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="icon"
|
||||
color="secondary"
|
||||
StartIcon="ellipsis"
|
||||
className="ltr:radix-state-open:rounded-r-md rtl:radix-state-open:rounded-l-md"
|
||||
/>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent className="min-w-[200px]">
|
||||
<DropdownMenuItem>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
StartIcon="pencil"
|
||||
href={`/settings/organizations/attributes/${attribute.id}/edit`}>
|
||||
{t("edit")}
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
StartIcon="trash-2"
|
||||
color="destructive"
|
||||
disabled={deleteMutation.isPending}
|
||||
onClick={handleDelete}>
|
||||
{t("delete")}
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
</div>
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
function OrganizationAttributesPage() {
|
||||
const { t } = useLocale();
|
||||
const { data, isLoading } = trpc.viewer.attributes.list.useQuery();
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<>
|
||||
<Meta title={t("attributes")} description={t("attribute_meta_description")} />
|
||||
<div className="border-subtle bg-default flex flex-col gap-4 rounded-lg border p-6">
|
||||
<ListSkeleton />
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Meta title={t("attributes")} description={t("attribute_meta_description")} />
|
||||
<ListAttributeHeader />
|
||||
<LicenseRequired>
|
||||
<div className="border-subtle bg-default flex flex-col gap-4 rounded-lg border p-6">
|
||||
{data && data?.length > 0 ? (
|
||||
<>
|
||||
<h2 className="text-emphasis text-base font-semibold leading-none">{t("custom")}</h2>
|
||||
<li className="border-subtle bg-default divide-subtle flex flex-col divide-y rounded-lg border">
|
||||
{data?.map((attribute) => (
|
||||
<AttributeItem attribute={attribute} key={attribute.id} />
|
||||
))}
|
||||
</li>
|
||||
<Button
|
||||
className="w-fit"
|
||||
StartIcon="plus"
|
||||
color="minimal"
|
||||
href="/settings/organizations/attributes/create">
|
||||
{t("add")}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<div className="flex w-full flex-col items-center justify-center p-14">
|
||||
<div className="bg-emphasis text-emphasis flex h-16 w-16 items-center justify-center rounded-full p-2">
|
||||
<Icon name="loader" />
|
||||
</div>
|
||||
<h2 className="font-cal text-emphasis mt-6 text-xl font-semibold leading-none">
|
||||
{t("add_attributes")}
|
||||
</h2>
|
||||
|
||||
<p className="text-emphasis mt-3 text-sm font-normal leading-none">
|
||||
{t("add_attributes_description")}
|
||||
</p>
|
||||
<Button
|
||||
className="mt-8"
|
||||
StartIcon="plus"
|
||||
color="secondary"
|
||||
href="/settings/organizations/attributes/create">
|
||||
{t("new_attribute")}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</LicenseRequired>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function ListAttributeHeader() {
|
||||
const { meta } = useMeta();
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="min-h-6 mb-6 mt-6 flex flex-grow items-center justify-between lg:mt-12">
|
||||
<div className="flex items-center gap-4 ">
|
||||
<div className="flex flex-col space-y-1 ">
|
||||
<h1 className="text-emphasis font-cal text-xl font-semibold leading-none">{meta.title}</h1>
|
||||
<p className="text-subtle text-sm">{meta.description}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function getLayout(page: React.ReactElement) {
|
||||
return <SettingsLayout hideHeader>{page}</SettingsLayout>;
|
||||
}
|
||||
|
||||
OrganizationAttributesPage.getLayout = getLayout;
|
||||
|
||||
export default OrganizationAttributesPage;
|
||||
@@ -13,4 +13,5 @@ export type AppFlags = {
|
||||
"email-verification": boolean;
|
||||
"google-workspace-directory": boolean;
|
||||
"disable-signup": boolean;
|
||||
attributes: boolean;
|
||||
};
|
||||
@@ -166,6 +166,16 @@ const useTabs = () => {
|
||||
const newArray = (tab?.children ?? []).filter(
|
||||
(child) => isOrgAdminOrOwner || !organizationAdminKeys.includes(child.name)
|
||||
);
|
||||
|
||||
// TODO: figure out feature flag as it doesnt cause a re-render of the component when loaded.
|
||||
// You have to refresh the page to see the changes.
|
||||
if (true) {
|
||||
newArray.splice(4, 0, {
|
||||
name: "attributes",
|
||||
href: "/settings/organizations/attributes",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
...tab,
|
||||
children: newArray,
|
||||
@@ -636,8 +646,12 @@ const MobileSettingsContainer = (props: { onSideContainerOpen?: () => void }) =>
|
||||
|
||||
export default function SettingsLayout({
|
||||
children,
|
||||
hideHeader,
|
||||
...rest
|
||||
}: { children: React.ReactNode } & ComponentProps<typeof Shell>) {
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
hideHeader?: boolean;
|
||||
} & ComponentProps<typeof Shell>) {
|
||||
const pathname = usePathname();
|
||||
const state = useState(false);
|
||||
const { t } = useLocale();
|
||||
@@ -681,7 +695,7 @@ export default function SettingsLayout({
|
||||
}>
|
||||
<div className="flex flex-1 [&>*]:flex-1">
|
||||
<div className="mx-auto max-w-full justify-center lg:max-w-3xl">
|
||||
<ShellHeader />
|
||||
{!hideHeader && <ShellHeader />}
|
||||
<ErrorBoundary>
|
||||
<Suspense fallback={<Icon name="loader" />}>{children}</Suspense>
|
||||
</ErrorBoundary>
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useSession } from "next-auth/react";
|
||||
import type { Dispatch } from "react";
|
||||
import { useMemo } from "react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import { useMemo, useState } from "react";
|
||||
import { Controller, useForm, useFormContext } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
import { shallow } from "zustand/shallow";
|
||||
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
@@ -13,19 +12,22 @@ import {
|
||||
Form,
|
||||
TextField,
|
||||
ToggleGroup,
|
||||
InputField,
|
||||
TextAreaField,
|
||||
TimezoneSelect,
|
||||
Label,
|
||||
showToast,
|
||||
Avatar,
|
||||
ImageUploader,
|
||||
SelectField,
|
||||
SheetHeader,
|
||||
SheetBody,
|
||||
SheetFooter,
|
||||
Button,
|
||||
SheetTitle,
|
||||
} from "@calcom/ui";
|
||||
|
||||
import type { Action } from "../UserListTable";
|
||||
import { SheetFooterControls } from "./SheetFooterControls";
|
||||
import { useEditMode } from "./store";
|
||||
|
||||
type MembershipOption = {
|
||||
@@ -33,6 +35,14 @@ type MembershipOption = {
|
||||
label: string;
|
||||
};
|
||||
|
||||
const stringOrNumber = z.string().or(z.number());
|
||||
|
||||
const attributeSchema = z.object({
|
||||
id: z.string(),
|
||||
options: z.array(z.object({ label: z.string().optional(), value: stringOrNumber.optional() })).optional(),
|
||||
value: stringOrNumber.optional(),
|
||||
});
|
||||
|
||||
const editSchema = z.object({
|
||||
name: z.string(),
|
||||
username: z.string(),
|
||||
@@ -43,6 +53,7 @@ const editSchema = z.object({
|
||||
timeZone: z.string(),
|
||||
// schedules: z.array(z.string()),
|
||||
// teams: z.array(z.string()),
|
||||
attributes: z.array(attributeSchema).optional(),
|
||||
});
|
||||
|
||||
type EditSchema = z.infer<typeof editSchema>;
|
||||
@@ -58,7 +69,8 @@ export function EditForm({
|
||||
domainUrl: string;
|
||||
dispatch: Dispatch<Action>;
|
||||
}) {
|
||||
const [setMutationLoading] = useEditMode((state) => [state.setMutationloading], shallow);
|
||||
const setEditMode = useEditMode((state) => state.setEditMode);
|
||||
const [mutationLoading, setMutationLoading] = useState(false);
|
||||
const { t } = useLocale();
|
||||
const session = useSession();
|
||||
const org = session?.data?.user?.org;
|
||||
@@ -124,7 +136,6 @@ export function EditForm({
|
||||
<>
|
||||
<Form
|
||||
form={form}
|
||||
id="edit-user-form"
|
||||
className="flex h-full flex-col"
|
||||
handleSubmit={(values) => {
|
||||
setMutationLoading(true);
|
||||
@@ -137,10 +148,18 @@ export function EditForm({
|
||||
avatar: values.avatar,
|
||||
bio: values.bio,
|
||||
timeZone: values.timeZone,
|
||||
// @ts-expect-error theyre there in local types but for some reason it errors?
|
||||
attributeOptions: values.attributes
|
||||
? // @ts-expect-error same as above
|
||||
{ userId: selectedUser?.id ?? "", attributes: values.attributes }
|
||||
: undefined,
|
||||
});
|
||||
setEditMode(false);
|
||||
}}>
|
||||
<SheetHeader>
|
||||
<div className="flex flex-col gap-2">
|
||||
<SheetTitle>{t("update_profile")}</SheetTitle>
|
||||
|
||||
<div className="mt-6 flex flex-col gap-2">
|
||||
<Controller
|
||||
control={form.control}
|
||||
name="avatar"
|
||||
@@ -161,22 +180,15 @@ export function EditForm({
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
<div className="space-between flex flex-col leading-none">
|
||||
<span className="text-emphasis text-lg font-semibold">
|
||||
{selectedUser?.name ?? "Nameless User"}
|
||||
</span>
|
||||
<p className="subtle text-sm font-normal">
|
||||
{domainUrl}/{selectedUser?.username}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</SheetHeader>
|
||||
<SheetBody className="mt-6 flex h-full flex-col space-y-3">
|
||||
<SheetBody className="mt-4 flex h-full flex-col space-y-3 px-1">
|
||||
<label className="text-emphasis mb-1 text-base font-semibold">{t("profile")}</label>
|
||||
<TextField label={t("username")} {...form.register("username")} />
|
||||
<TextField label={t("name")} {...form.register("name")} />
|
||||
<TextField label={t("email")} {...form.register("email")} />
|
||||
|
||||
<TextAreaField label={t("bio")} {...form.register("bio")} className="min-h-52" />
|
||||
<TextAreaField label={t("bio")} {...form.register("bio")} className="min-h-24" />
|
||||
<div>
|
||||
<Label>{t("role")}</Label>
|
||||
<ToggleGroup
|
||||
@@ -189,15 +201,157 @@ export function EditForm({
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<div className="mb-4">
|
||||
<Label>{t("timezone")}</Label>
|
||||
<TimezoneSelect value={watchTimezone ?? "America/Los_Angeles"} />
|
||||
</div>
|
||||
<AttributesList selectedUserId={selectedUser?.id} />
|
||||
</SheetBody>
|
||||
<SheetFooter>
|
||||
<SheetFooterControls />
|
||||
<Button
|
||||
color="secondary"
|
||||
type="button"
|
||||
className="justify-center md:w-1/5"
|
||||
onClick={() => {
|
||||
setEditMode(false);
|
||||
}}>
|
||||
{t("cancel")}
|
||||
</Button>
|
||||
|
||||
<Button type="submit" className="w-full justify-center">
|
||||
{t("update")}
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</Form>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
type AttributeType = z.infer<typeof attributeSchema>;
|
||||
|
||||
type DefaultValueType = {
|
||||
[key: `attributes.${number}`]: AttributeType;
|
||||
};
|
||||
|
||||
function AttributesList(props: { selectedUserId: number }) {
|
||||
const { data: usersAttributes, isPending: usersAttributesPending } =
|
||||
trpc.viewer.attributes.getByUserId.useQuery({
|
||||
userId: props.selectedUserId,
|
||||
});
|
||||
const { data: attributes, isPending: attributesPending } = trpc.viewer.attributes.list.useQuery();
|
||||
const enabledAttributes = attributes?.filter((attr) => attr.enabled);
|
||||
|
||||
const { t } = useLocale();
|
||||
const { control, watch, getFieldState, setValue } = useFormContext();
|
||||
|
||||
const getOptionsByAttributeId = (attributeId: string) => {
|
||||
const attribute = attributes?.find((attr) => attr.id === attributeId);
|
||||
return attribute
|
||||
? attribute.options.map((option) => ({
|
||||
value: option.id,
|
||||
label: option.value,
|
||||
}))
|
||||
: [];
|
||||
};
|
||||
|
||||
const defaultValues = useMemo<DefaultValueType>(() => {
|
||||
if (!usersAttributes || usersAttributesPending || !enabledAttributes) return {};
|
||||
|
||||
return enabledAttributes.reduce<DefaultValueType>((acc, enabledAttr, index) => {
|
||||
const attr = usersAttributes.find((attr) => attr.id === enabledAttr.id);
|
||||
const key = `attributes.${index}` as const;
|
||||
|
||||
if (!attr) {
|
||||
acc[key] = { id: enabledAttr.id, value: "", options: [] };
|
||||
} else if (attr.type === "MULTI_SELECT") {
|
||||
acc[key] = {
|
||||
id: attr.id,
|
||||
options: attr.options.map((option) => ({ label: option.value, value: option.id })),
|
||||
};
|
||||
} else if (attr.type === "SINGLE_SELECT") {
|
||||
acc[key] = {
|
||||
id: attr.id,
|
||||
options: [{ label: attr.options[0]?.value, value: attr.options[0]?.id }],
|
||||
};
|
||||
} else {
|
||||
acc[key] = {
|
||||
id: attr.id,
|
||||
value: attr.options[0]?.value || "",
|
||||
};
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
}, [usersAttributes, usersAttributesPending, enabledAttributes]);
|
||||
|
||||
if (!enabledAttributes || !usersAttributes) return null;
|
||||
const attributeFieldState = getFieldState("attributes");
|
||||
|
||||
return (
|
||||
<div className="flex flex-col overflow-visible">
|
||||
<div className="flex flex-col gap-3 rounded-lg">
|
||||
<label className="text-emphasis mb-1 mt-6 text-base font-semibold">{t("attributes")}</label>
|
||||
{attributeFieldState.error && (
|
||||
<p className="text-error mb-2 block text-sm font-medium leading-none">
|
||||
{JSON.stringify(attributeFieldState.error)}
|
||||
</p>
|
||||
)}
|
||||
{enabledAttributes.map((attr, index) => (
|
||||
<Controller
|
||||
name={`attributes.${index}`}
|
||||
control={control}
|
||||
key={attr.id}
|
||||
defaultValue={defaultValues[`attributes.${index}`]}
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<div className="flex w-full items-center justify-center gap-2" key={attr.id}>
|
||||
{["TEXT", "NUMBER"].includes(attr.type) && (
|
||||
<InputField
|
||||
{...field}
|
||||
containerClassName="w-full"
|
||||
labelClassName="text-emphasis mb-2 block text-sm font-medium leading-none"
|
||||
label={attr.name}
|
||||
type={attr.type === "TEXT" ? "text" : "number"}
|
||||
value={field.value?.value || ""}
|
||||
onChange={(e) => {
|
||||
field.onChange({
|
||||
id: attr.id,
|
||||
value: e.target.value,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{["SINGLE_SELECT", "MULTI_SELECT"].includes(attr.type) && (
|
||||
<SelectField
|
||||
name={field.name}
|
||||
containerClassName="w-full"
|
||||
isMulti={attr.type === "MULTI_SELECT"}
|
||||
labelProps={{
|
||||
className: "text-emphasis mb-2 block text-sm font-medium leading-none",
|
||||
}}
|
||||
label={attr.name}
|
||||
options={getOptionsByAttributeId(attr.id)}
|
||||
value={attr.type === "MULTI_SELECT" ? field.value?.options : field.value?.options[0]}
|
||||
onChange={(value) => {
|
||||
if (attr.type === "MULTI_SELECT") {
|
||||
field.onChange({
|
||||
id: attr.id,
|
||||
options: value.map((v: any) => ({ label: v.label, value: v.value })),
|
||||
});
|
||||
} else {
|
||||
field.onChange({
|
||||
id: attr.id,
|
||||
options: [{ label: value.label, value: value.value }],
|
||||
});
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -23,9 +23,26 @@ export function EditUserSheet({ state, dispatch }: { state: State; dispatch: Dis
|
||||
const { user: selectedUser } = state.editSheet;
|
||||
const orgBranding = useOrgBranding();
|
||||
const [editMode, setEditMode] = useEditMode((state) => [state.editMode, state.setEditMode], shallow);
|
||||
const { data: loadedUser, isPending } = trpc.viewer.organizations.getUser.useQuery({
|
||||
userId: selectedUser?.id,
|
||||
});
|
||||
const { data: loadedUser, isPending } = trpc.viewer.organizations.getUser.useQuery(
|
||||
{
|
||||
// @ts-expect-error we obly enable the query if the user is selected
|
||||
userId: selectedUser.id,
|
||||
},
|
||||
{
|
||||
enabled: !!selectedUser?.id,
|
||||
}
|
||||
);
|
||||
|
||||
const { data: usersAttributes, isPending: usersAttributesPending } =
|
||||
trpc.viewer.attributes.getByUserId.useQuery(
|
||||
{
|
||||
// @ts-expect-error we obly enable the query if the user is selected
|
||||
userId: selectedUser.id,
|
||||
},
|
||||
{
|
||||
enabled: !!selectedUser?.id,
|
||||
}
|
||||
);
|
||||
|
||||
const avatarURL = `${orgBranding?.fullDomain ?? WEBAPP_URL}/${loadedUser?.username}/avatar.png`;
|
||||
|
||||
@@ -45,7 +62,7 @@ export function EditUserSheet({ state, dispatch }: { state: State; dispatch: Dis
|
||||
<>
|
||||
{!editMode ? (
|
||||
<>
|
||||
<SheetHeader showCloseButton={false}>
|
||||
<SheetHeader showCloseButton={false} className="w-full">
|
||||
<div className="border-sublte bg-default w-full rounded-xl border p-4">
|
||||
<OrganizationBanner />
|
||||
<div className="bg-default ml-3 w-fit translate-y-[-50%] rounded-full p-1 ring-1 ring-[#0000000F]">
|
||||
@@ -64,8 +81,8 @@ export function EditUserSheet({ state, dispatch }: { state: State; dispatch: Dis
|
||||
</p>
|
||||
</div>
|
||||
</SheetHeader>
|
||||
<SheetBody className="flex flex-col space-y-8 p-4">
|
||||
<div className="flex flex-col space-y-4">
|
||||
<SheetBody className="flex flex-col space-y-4 p-4">
|
||||
<div className="mb-4 flex flex-col space-y-4">
|
||||
<h3 className="text-emphasis mb-1 text-base font-semibold">{t("profile")}</h3>
|
||||
<DisplayInfo
|
||||
label="Cal"
|
||||
@@ -89,6 +106,26 @@ export function EditUserSheet({ state, dispatch }: { state: State; dispatch: Dis
|
||||
icon="calendar"
|
||||
/>
|
||||
</div>
|
||||
{usersAttributes && usersAttributes?.length > 0 && (
|
||||
<div className="mt-4 flex flex-col">
|
||||
<h3 className="text-emphasis mb-5 text-base font-semibold">{t("attributes")}</h3>
|
||||
<div className="flex flex-col space-y-4">
|
||||
{usersAttributes.map((attribute, index) => (
|
||||
<>
|
||||
<DisplayInfo
|
||||
key={index}
|
||||
label={attribute.name}
|
||||
value={
|
||||
["TEXT", "NUMBER", "SINGLE_SELECT"].includes(attribute.type)
|
||||
? attribute.options[0].value
|
||||
: attribute.options.map((option) => option.value)
|
||||
}
|
||||
/>
|
||||
</>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</SheetBody>
|
||||
<SheetFooter>
|
||||
<SheetFooterControls />
|
||||
|
||||
@@ -50,7 +50,7 @@ export type State = {
|
||||
deleteMember: Payload;
|
||||
impersonateMember: Payload;
|
||||
inviteMember: Payload;
|
||||
editSheet: Payload;
|
||||
editSheet: Payload & { user?: User };
|
||||
};
|
||||
|
||||
export type Action =
|
||||
|
||||
Reference in new issue
Block a user