feat: weights per attribute UI (#18109)
* wip attribut weights * edit sheet and display in table * add shrink so badges dont split * push changes to support weights on csv export * fix typing issue * type error * fall back to 100 weight --------- Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
b6d6b7cdb4
commit
ee3c08918a
18 files changed
+190
-67
No files matched your search
@@ -45,6 +45,7 @@ const attributeSchema = z.object({
|
||||
z.object({
|
||||
label: z.string().optional(),
|
||||
value: stringOrNumber.optional(),
|
||||
weight: z.number().optional(),
|
||||
createdByDSyncId: z.string().nullable().optional(),
|
||||
})
|
||||
)
|
||||
@@ -192,7 +193,7 @@ export function EditForm({
|
||||
</div>
|
||||
</SheetHeader>
|
||||
<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>
|
||||
<label className="text-emphasis mb-1 text-base font-bold">{t("profile")}</label>
|
||||
<TextField label={t("username")} {...form.register("username")} />
|
||||
<TextField label={t("name")} {...form.register("name")} />
|
||||
<TextField label={t("email")} {...form.register("email")} />
|
||||
@@ -247,11 +248,11 @@ function AttributesList(props: { selectedUserId: number }) {
|
||||
trpc.viewer.attributes.getByUserId.useQuery({
|
||||
userId: props.selectedUserId,
|
||||
});
|
||||
const { data: attributes, isPending: attributesPending } = trpc.viewer.attributes.list.useQuery();
|
||||
const { data: attributes } = trpc.viewer.attributes.list.useQuery();
|
||||
const enabledAttributes = attributes?.filter((attr) => attr.enabled);
|
||||
|
||||
const { t } = useLocale();
|
||||
const { control, watch, getFieldState, setValue } = useFormContext();
|
||||
const { control, getFieldState } = useFormContext();
|
||||
|
||||
const getOptionsByAttributeId = (attributeId: string) => {
|
||||
const attribute = attributes?.find((attr) => attr.id === attributeId);
|
||||
@@ -278,7 +279,8 @@ function AttributesList(props: { selectedUserId: number }) {
|
||||
options: attr.options.map((option) => ({
|
||||
label: option.value,
|
||||
value: option.id,
|
||||
createdByDSyncId: option.createdByDSyncId,
|
||||
createdByDSyncId: option.createdByDSyncId ?? null,
|
||||
weight: option.weight ?? 100,
|
||||
})),
|
||||
};
|
||||
} else if (attr.type === "SINGLE_SELECT") {
|
||||
@@ -289,10 +291,11 @@ function AttributesList(props: { selectedUserId: number }) {
|
||||
label: attr.options[0]?.value,
|
||||
value: attr.options[0]?.id,
|
||||
createdByDSyncId: attr.options[0]?.createdByDSyncId ?? null,
|
||||
weight: attr.options[0]?.weight ?? 100,
|
||||
},
|
||||
],
|
||||
};
|
||||
} else {
|
||||
} else if (attr.type === "TEXT") {
|
||||
acc[key] = {
|
||||
id: attr.id,
|
||||
value: attr.options[0]?.value || "",
|
||||
@@ -308,7 +311,7 @@ function AttributesList(props: { selectedUserId: number }) {
|
||||
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>
|
||||
<label className="text-emphasis mb-1 mt-6 text-base font-bold">{t("attributes")}</label>
|
||||
{attributeFieldState.error && (
|
||||
<p className="text-error mb-2 block text-sm font-medium leading-none">
|
||||
{JSON.stringify(attributeFieldState.error)}
|
||||
@@ -323,7 +326,7 @@ function AttributesList(props: { selectedUserId: number }) {
|
||||
render={({ field: { value, ...field } }) => {
|
||||
const fieldValue = value as Attribute | undefined | null;
|
||||
return (
|
||||
<div className="flex w-full items-center justify-center gap-2" key={attr.id}>
|
||||
<div className="flex w-full items-center justify-center" key={attr.id}>
|
||||
{["TEXT", "NUMBER"].includes(attr.type) && (
|
||||
<InputField
|
||||
{...field}
|
||||
@@ -341,36 +344,85 @@ function AttributesList(props: { selectedUserId: number }) {
|
||||
/>
|
||||
)}
|
||||
{["SINGLE_SELECT", "MULTI_SELECT"].includes(attr.type) && (
|
||||
<SelectField
|
||||
isDisabled={attr.isLocked}
|
||||
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" ? fieldValue?.options : fieldValue?.options?.[0]}
|
||||
onChange={(value) => {
|
||||
if (!value) return;
|
||||
const valueAsArray = value instanceof Array ? value : [value];
|
||||
<div className="w-full">
|
||||
<SelectField
|
||||
isDisabled={attr.isLocked}
|
||||
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" ? fieldValue?.options : fieldValue?.options?.[0]}
|
||||
onChange={(value) => {
|
||||
if (!value) return;
|
||||
const valueAsArray = value instanceof Array ? value : [value];
|
||||
|
||||
const updatedOptions =
|
||||
attr.type === "MULTI_SELECT"
|
||||
? valueAsArray.map((v) => ({ label: v.label, value: v.value }))
|
||||
: [{ label: valueAsArray[0].label, value: valueAsArray[0].value }];
|
||||
const updatedOptions =
|
||||
attr.type === "MULTI_SELECT"
|
||||
? valueAsArray.map((v) => ({
|
||||
label: v.label,
|
||||
value: v.value,
|
||||
weight: v.weight || 100,
|
||||
}))
|
||||
: [
|
||||
{
|
||||
label: valueAsArray[0].label,
|
||||
value: valueAsArray[0].value,
|
||||
weight: valueAsArray[0].weight || 100,
|
||||
},
|
||||
];
|
||||
|
||||
field.onChange({
|
||||
id: attr.id,
|
||||
// It is also ensured in the backend that the options not owned by cal.com are not removed
|
||||
options: getOptionsEnsuringNotOwnedByCalcomNotRemoved({
|
||||
earlierOptions: fieldValue?.options || [],
|
||||
updatedOptions,
|
||||
}),
|
||||
});
|
||||
}}
|
||||
/>
|
||||
field.onChange({
|
||||
id: attr.id,
|
||||
options: getOptionsEnsuringNotOwnedByCalcomNotRemoved({
|
||||
earlierOptions: fieldValue?.options || [],
|
||||
updatedOptions,
|
||||
}),
|
||||
});
|
||||
}}
|
||||
/>
|
||||
{attr.isWeightsEnabled && fieldValue?.options && (
|
||||
<div className="mt-3 space-y-2">
|
||||
<Label>Weights</Label>
|
||||
<div className="">
|
||||
{fieldValue.options.map((option, idx) => {
|
||||
return (
|
||||
<>
|
||||
<div key={option.value} className="flex items-center justify-between">
|
||||
<Label
|
||||
htmlFor={`attributes.${index}.otions.${idx}.weight`}
|
||||
className="text-subtle">
|
||||
{option.label}
|
||||
</Label>
|
||||
<InputField
|
||||
noLabel
|
||||
name={`attributes.${index}.options.${idx}.weight`}
|
||||
type="number"
|
||||
step={10}
|
||||
value={option.weight || 100}
|
||||
onChange={(e) => {
|
||||
const newWeight = parseFloat(e.target.value) || 1;
|
||||
const newOptions = fieldValue?.options?.map((opt, i) =>
|
||||
i === idx ? { ...opt, weight: newWeight } : opt
|
||||
);
|
||||
field.onChange({
|
||||
id: attr.id,
|
||||
options: newOptions,
|
||||
});
|
||||
}}
|
||||
addOnSuffix={<span className="text-subtle text-sm">%</span>}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -62,7 +62,7 @@ export function EditUserSheet({
|
||||
setEditMode(false);
|
||||
dispatch({ type: "CLOSE_MODAL" });
|
||||
}}>
|
||||
<SheetContent className="bg-muted">
|
||||
<SheetContent className="bg-default">
|
||||
{!isPending && loadedUser ? (
|
||||
<>
|
||||
{!editMode ? (
|
||||
@@ -71,12 +71,7 @@ export function EditUserSheet({
|
||||
<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]">
|
||||
<Avatar
|
||||
asChild
|
||||
size="lg"
|
||||
alt={`${loadedUser?.name} avatar`}
|
||||
imageSrc={loadedUser.avatarUrl}
|
||||
/>
|
||||
<Avatar asChild size="lg" alt={`${loadedUser?.name} avatar`} imageSrc={avatarURL} />
|
||||
</div>
|
||||
<h2 className="text-emphasis font-sans text-2xl font-semibold">
|
||||
{loadedUser?.name || "Nameless User"}
|
||||
|
||||
Reference in new issue
Block a user