forked from github/plane
feat: randomize color on label create (#1839)
fix: create label state being persisted after edit label
This commit is contained in:
parent
cd5e5b96da
commit
7becec4ee9
@ -20,6 +20,7 @@ import { ChevronDownIcon } from "@heroicons/react/24/outline";
|
|||||||
import type { ICurrentUserResponse, IIssueLabels, IState } from "types";
|
import type { ICurrentUserResponse, IIssueLabels, IState } from "types";
|
||||||
// constants
|
// constants
|
||||||
import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
||||||
|
import { LABEL_COLOR_OPTIONS, getRandomLabelColor } from "constants/label";
|
||||||
|
|
||||||
// types
|
// types
|
||||||
type Props = {
|
type Props = {
|
||||||
@ -52,10 +53,15 @@ export const CreateLabelModal: React.FC<Props> = ({
|
|||||||
watch,
|
watch,
|
||||||
control,
|
control,
|
||||||
reset,
|
reset,
|
||||||
|
setValue,
|
||||||
} = useForm<IIssueLabels>({
|
} = useForm<IIssueLabels>({
|
||||||
defaultValues,
|
defaultValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isOpen) setValue("color", getRandomLabelColor());
|
||||||
|
}, [setValue, isOpen]);
|
||||||
|
|
||||||
const onClose = () => {
|
const onClose = () => {
|
||||||
handleClose();
|
handleClose();
|
||||||
reset(defaultValues);
|
reset(defaultValues);
|
||||||
@ -156,6 +162,7 @@ export const CreateLabelModal: React.FC<Props> = ({
|
|||||||
render={({ field: { value, onChange } }) => (
|
render={({ field: { value, onChange } }) => (
|
||||||
<TwitterPicker
|
<TwitterPicker
|
||||||
color={value}
|
color={value}
|
||||||
|
colors={LABEL_COLOR_OPTIONS}
|
||||||
onChange={(value) => {
|
onChange={(value) => {
|
||||||
onChange(value.hex);
|
onChange(value.hex);
|
||||||
close();
|
close();
|
||||||
|
@ -22,12 +22,14 @@ import { ChevronDownIcon } from "@heroicons/react/24/outline";
|
|||||||
import { IIssueLabels } from "types";
|
import { IIssueLabels } from "types";
|
||||||
// fetch-keys
|
// fetch-keys
|
||||||
import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
||||||
|
import { getRandomLabelColor, LABEL_COLOR_OPTIONS } from "constants/label";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
labelForm: boolean;
|
labelForm: boolean;
|
||||||
setLabelForm: React.Dispatch<React.SetStateAction<boolean>>;
|
setLabelForm: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
isUpdating: boolean;
|
isUpdating: boolean;
|
||||||
labelToUpdate: IIssueLabels | null;
|
labelToUpdate: IIssueLabels | null;
|
||||||
|
onClose?: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultValues: Partial<IIssueLabels> = {
|
const defaultValues: Partial<IIssueLabels> = {
|
||||||
@ -35,167 +37,180 @@ const defaultValues: Partial<IIssueLabels> = {
|
|||||||
color: "rgb(var(--color-text-200))",
|
color: "rgb(var(--color-text-200))",
|
||||||
};
|
};
|
||||||
|
|
||||||
type Ref = HTMLDivElement;
|
export const CreateUpdateLabelInline = forwardRef<HTMLDivElement, Props>(
|
||||||
|
function CreateUpdateLabelInline(props, ref) {
|
||||||
|
const { labelForm, setLabelForm, isUpdating, labelToUpdate, onClose } = props;
|
||||||
|
|
||||||
export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpdateLabelInline(
|
const router = useRouter();
|
||||||
{ labelForm, setLabelForm, isUpdating, labelToUpdate },
|
const { workspaceSlug, projectId } = router.query;
|
||||||
ref
|
|
||||||
) {
|
|
||||||
const router = useRouter();
|
|
||||||
const { workspaceSlug, projectId } = router.query;
|
|
||||||
|
|
||||||
const { user } = useUserAuth();
|
const { user } = useUserAuth();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
control,
|
control,
|
||||||
register,
|
register,
|
||||||
reset,
|
reset,
|
||||||
formState: { errors, isSubmitting },
|
formState: { errors, isSubmitting },
|
||||||
watch,
|
watch,
|
||||||
setValue,
|
setValue,
|
||||||
} = useForm<IIssueLabels>({
|
} = useForm<IIssueLabels>({
|
||||||
defaultValues,
|
defaultValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
|
const handleClose = () => {
|
||||||
if (!workspaceSlug || !projectId || isSubmitting) return;
|
setLabelForm(false);
|
||||||
|
reset(defaultValues);
|
||||||
|
if (onClose) onClose();
|
||||||
|
};
|
||||||
|
|
||||||
await issuesService
|
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
|
||||||
.createIssueLabel(workspaceSlug as string, projectId as string, formData, user)
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
||||||
.then((res) => {
|
|
||||||
mutate<IIssueLabels[]>(
|
await issuesService
|
||||||
PROJECT_ISSUE_LABELS(projectId as string),
|
.createIssueLabel(workspaceSlug as string, projectId as string, formData, user)
|
||||||
(prevData) => [res, ...(prevData ?? [])],
|
.then((res) => {
|
||||||
false
|
mutate<IIssueLabels[]>(
|
||||||
|
PROJECT_ISSUE_LABELS(projectId as string),
|
||||||
|
(prevData) => [res, ...(prevData ?? [])],
|
||||||
|
false
|
||||||
|
);
|
||||||
|
handleClose();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLabelUpdate: SubmitHandler<IIssueLabels> = async (formData) => {
|
||||||
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
||||||
|
|
||||||
|
await issuesService
|
||||||
|
.patchIssueLabel(
|
||||||
|
workspaceSlug as string,
|
||||||
|
projectId as string,
|
||||||
|
labelToUpdate?.id ?? "",
|
||||||
|
formData,
|
||||||
|
user
|
||||||
|
)
|
||||||
|
.then(() => {
|
||||||
|
reset(defaultValues);
|
||||||
|
mutate<IIssueLabels[]>(
|
||||||
|
PROJECT_ISSUE_LABELS(projectId as string),
|
||||||
|
(prevData) =>
|
||||||
|
prevData?.map((p) => (p.id === labelToUpdate?.id ? { ...p, ...formData } : p)),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
handleClose();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!labelForm && isUpdating) return;
|
||||||
|
|
||||||
|
reset();
|
||||||
|
}, [labelForm, isUpdating, reset]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!labelToUpdate) return;
|
||||||
|
|
||||||
|
setValue(
|
||||||
|
"color",
|
||||||
|
labelToUpdate.color && labelToUpdate.color !== "" ? labelToUpdate.color : "#000"
|
||||||
|
);
|
||||||
|
setValue("name", labelToUpdate.name);
|
||||||
|
}, [labelToUpdate, setValue]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (labelToUpdate) {
|
||||||
|
setValue(
|
||||||
|
"color",
|
||||||
|
labelToUpdate.color && labelToUpdate.color !== "" ? labelToUpdate.color : "#000"
|
||||||
);
|
);
|
||||||
reset(defaultValues);
|
return;
|
||||||
setLabelForm(false);
|
}
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleLabelUpdate: SubmitHandler<IIssueLabels> = async (formData) => {
|
setValue("color", getRandomLabelColor());
|
||||||
if (!workspaceSlug || !projectId || isSubmitting) return;
|
}, [labelToUpdate, setValue]);
|
||||||
|
|
||||||
await issuesService
|
return (
|
||||||
.patchIssueLabel(
|
<div
|
||||||
workspaceSlug as string,
|
className={`flex scroll-m-8 items-center gap-2 rounded-[10px] border border-custom-border-200 bg-custom-background-100 p-5 ${
|
||||||
projectId as string,
|
labelForm ? "" : "hidden"
|
||||||
labelToUpdate?.id ?? "",
|
}`}
|
||||||
formData,
|
ref={ref}
|
||||||
user
|
|
||||||
)
|
|
||||||
.then(() => {
|
|
||||||
reset(defaultValues);
|
|
||||||
mutate<IIssueLabels[]>(
|
|
||||||
PROJECT_ISSUE_LABELS(projectId as string),
|
|
||||||
(prevData) =>
|
|
||||||
prevData?.map((p) => (p.id === labelToUpdate?.id ? { ...p, ...formData } : p)),
|
|
||||||
false
|
|
||||||
);
|
|
||||||
setLabelForm(false);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!labelForm && isUpdating) return;
|
|
||||||
|
|
||||||
reset();
|
|
||||||
}, [labelForm, isUpdating, reset]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!labelToUpdate) return;
|
|
||||||
|
|
||||||
setValue(
|
|
||||||
"color",
|
|
||||||
labelToUpdate.color && labelToUpdate.color !== "" ? labelToUpdate.color : "#000"
|
|
||||||
);
|
|
||||||
setValue("name", labelToUpdate.name);
|
|
||||||
}, [labelToUpdate, setValue]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`flex scroll-m-8 items-center gap-2 rounded-[10px] border border-custom-border-200 bg-custom-background-100 p-5 ${
|
|
||||||
labelForm ? "" : "hidden"
|
|
||||||
}`}
|
|
||||||
ref={ref}
|
|
||||||
>
|
|
||||||
<div className="flex-shrink-0">
|
|
||||||
<Popover className="relative z-10 flex h-full w-full items-center justify-center">
|
|
||||||
{({ open }) => (
|
|
||||||
<>
|
|
||||||
<Popover.Button
|
|
||||||
className={`group inline-flex items-center text-base font-medium focus:outline-none ${
|
|
||||||
open ? "text-custom-text-100" : "text-custom-text-200"
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<span
|
|
||||||
className="h-5 w-5 rounded"
|
|
||||||
style={{
|
|
||||||
backgroundColor: watch("color"),
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<ChevronDownIcon
|
|
||||||
className={`ml-2 h-5 w-5 group-hover:text-custom-text-200 ${
|
|
||||||
open ? "text-gray-600" : "text-gray-400"
|
|
||||||
}`}
|
|
||||||
aria-hidden="true"
|
|
||||||
/>
|
|
||||||
</Popover.Button>
|
|
||||||
|
|
||||||
<Transition
|
|
||||||
as={React.Fragment}
|
|
||||||
enter="transition ease-out duration-200"
|
|
||||||
enterFrom="opacity-0 translate-y-1"
|
|
||||||
enterTo="opacity-100 translate-y-0"
|
|
||||||
leave="transition ease-in duration-150"
|
|
||||||
leaveFrom="opacity-100 translate-y-0"
|
|
||||||
leaveTo="opacity-0 translate-y-1"
|
|
||||||
>
|
|
||||||
<Popover.Panel className="absolute top-full left-0 z-20 mt-3 w-screen max-w-xs px-2 sm:px-0">
|
|
||||||
<Controller
|
|
||||||
name="color"
|
|
||||||
control={control}
|
|
||||||
render={({ field: { value, onChange } }) => (
|
|
||||||
<TwitterPicker color={value} onChange={(value) => onChange(value.hex)} />
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</Popover.Panel>
|
|
||||||
</Transition>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Popover>
|
|
||||||
</div>
|
|
||||||
<div className="flex flex-1 flex-col justify-center">
|
|
||||||
<Input
|
|
||||||
type="text"
|
|
||||||
id="labelName"
|
|
||||||
name="name"
|
|
||||||
register={register}
|
|
||||||
placeholder="Label title"
|
|
||||||
validations={{
|
|
||||||
required: "Label title is required",
|
|
||||||
}}
|
|
||||||
error={errors.name}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<SecondaryButton
|
|
||||||
onClick={() => {
|
|
||||||
reset();
|
|
||||||
setLabelForm(false);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
Cancel
|
<div className="flex-shrink-0">
|
||||||
</SecondaryButton>
|
<Popover className="relative z-10 flex h-full w-full items-center justify-center">
|
||||||
{isUpdating ? (
|
{({ open }) => (
|
||||||
<PrimaryButton onClick={handleSubmit(handleLabelUpdate)} loading={isSubmitting}>
|
<>
|
||||||
{isSubmitting ? "Updating" : "Update"}
|
<Popover.Button
|
||||||
</PrimaryButton>
|
className={`group inline-flex items-center text-base font-medium focus:outline-none ${
|
||||||
) : (
|
open ? "text-custom-text-100" : "text-custom-text-200"
|
||||||
<PrimaryButton onClick={handleSubmit(handleLabelCreate)} loading={isSubmitting}>
|
}`}
|
||||||
{isSubmitting ? "Adding" : "Add"}
|
>
|
||||||
</PrimaryButton>
|
<span
|
||||||
)}
|
className="h-5 w-5 rounded"
|
||||||
</div>
|
style={{
|
||||||
);
|
backgroundColor: watch("color"),
|
||||||
});
|
}}
|
||||||
|
/>
|
||||||
|
<ChevronDownIcon
|
||||||
|
className={`ml-2 h-5 w-5 group-hover:text-custom-text-200 ${
|
||||||
|
open ? "text-gray-600" : "text-gray-400"
|
||||||
|
}`}
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
</Popover.Button>
|
||||||
|
|
||||||
|
<Transition
|
||||||
|
as={React.Fragment}
|
||||||
|
enter="transition ease-out duration-200"
|
||||||
|
enterFrom="opacity-0 translate-y-1"
|
||||||
|
enterTo="opacity-100 translate-y-0"
|
||||||
|
leave="transition ease-in duration-150"
|
||||||
|
leaveFrom="opacity-100 translate-y-0"
|
||||||
|
leaveTo="opacity-0 translate-y-1"
|
||||||
|
>
|
||||||
|
<Popover.Panel className="absolute top-full left-0 z-20 mt-3 w-screen max-w-xs px-2 sm:px-0">
|
||||||
|
<Controller
|
||||||
|
name="color"
|
||||||
|
control={control}
|
||||||
|
render={({ field: { value, onChange } }) => (
|
||||||
|
<TwitterPicker
|
||||||
|
colors={LABEL_COLOR_OPTIONS}
|
||||||
|
color={value}
|
||||||
|
onChange={(value) => onChange(value.hex)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</Popover.Panel>
|
||||||
|
</Transition>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Popover>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-1 flex-col justify-center">
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
id="labelName"
|
||||||
|
name="name"
|
||||||
|
register={register}
|
||||||
|
placeholder="Label title"
|
||||||
|
validations={{
|
||||||
|
required: "Label title is required",
|
||||||
|
}}
|
||||||
|
error={errors.name}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<SecondaryButton onClick={() => handleClose()}>Cancel</SecondaryButton>
|
||||||
|
{isUpdating ? (
|
||||||
|
<PrimaryButton onClick={handleSubmit(handleLabelUpdate)} loading={isSubmitting}>
|
||||||
|
{isSubmitting ? "Updating" : "Update"}
|
||||||
|
</PrimaryButton>
|
||||||
|
) : (
|
||||||
|
<PrimaryButton onClick={handleSubmit(handleLabelCreate)} loading={isSubmitting}>
|
||||||
|
{isSubmitting ? "Adding" : "Add"}
|
||||||
|
</PrimaryButton>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
17
apps/app/constants/label.ts
Normal file
17
apps/app/constants/label.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
export const LABEL_COLOR_OPTIONS = [
|
||||||
|
"#FF6900",
|
||||||
|
"#FCB900",
|
||||||
|
"#7BDCB5",
|
||||||
|
"#00D084",
|
||||||
|
"#8ED1FC",
|
||||||
|
"#0693E3",
|
||||||
|
"#ABB8C3",
|
||||||
|
"#EB144C",
|
||||||
|
"#F78DA7",
|
||||||
|
"#9900EF",
|
||||||
|
];
|
||||||
|
|
||||||
|
export const getRandomLabelColor = () => {
|
||||||
|
const randomIndex = Math.floor(Math.random() * LABEL_COLOR_OPTIONS.length);
|
||||||
|
return LABEL_COLOR_OPTIONS[randomIndex];
|
||||||
|
};
|
@ -133,6 +133,11 @@ const LabelsSettings: NextPage = () => {
|
|||||||
setLabelForm={setLabelForm}
|
setLabelForm={setLabelForm}
|
||||||
isUpdating={isUpdating}
|
isUpdating={isUpdating}
|
||||||
labelToUpdate={labelToUpdate}
|
labelToUpdate={labelToUpdate}
|
||||||
|
onClose={() => {
|
||||||
|
setLabelForm(false);
|
||||||
|
setIsUpdating(false);
|
||||||
|
setLabelToUpdate(null);
|
||||||
|
}}
|
||||||
ref={scrollToRef}
|
ref={scrollToRef}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
Loading…
Reference in New Issue
Block a user