feat: randomize color on label create (#1839)

fix: create label state being persisted after edit label
This commit is contained in:
Dakshesh Jain 2023-08-11 17:42:47 +05:30 committed by GitHub
parent cd5e5b96da
commit 7becec4ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 199 additions and 155 deletions

View File

@ -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();

View File

@ -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,12 +37,10 @@ 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(
{ labelForm, setLabelForm, isUpdating, labelToUpdate },
ref
) {
const router = useRouter(); const router = useRouter();
const { workspaceSlug, projectId } = router.query; const { workspaceSlug, projectId } = router.query;
@ -58,6 +58,12 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
defaultValues, defaultValues,
}); });
const handleClose = () => {
setLabelForm(false);
reset(defaultValues);
if (onClose) onClose();
};
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => { const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
if (!workspaceSlug || !projectId || isSubmitting) return; if (!workspaceSlug || !projectId || isSubmitting) return;
@ -69,8 +75,7 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
(prevData) => [res, ...(prevData ?? [])], (prevData) => [res, ...(prevData ?? [])],
false false
); );
reset(defaultValues); handleClose();
setLabelForm(false);
}); });
}; };
@ -93,7 +98,7 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
prevData?.map((p) => (p.id === labelToUpdate?.id ? { ...p, ...formData } : p)), prevData?.map((p) => (p.id === labelToUpdate?.id ? { ...p, ...formData } : p)),
false false
); );
setLabelForm(false); handleClose();
}); });
}; };
@ -113,6 +118,18 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
setValue("name", labelToUpdate.name); setValue("name", labelToUpdate.name);
}, [labelToUpdate, setValue]); }, [labelToUpdate, setValue]);
useEffect(() => {
if (labelToUpdate) {
setValue(
"color",
labelToUpdate.color && labelToUpdate.color !== "" ? labelToUpdate.color : "#000"
);
return;
}
setValue("color", getRandomLabelColor());
}, [labelToUpdate, setValue]);
return ( return (
<div <div
className={`flex scroll-m-8 items-center gap-2 rounded-[10px] border border-custom-border-200 bg-custom-background-100 p-5 ${ className={`flex scroll-m-8 items-center gap-2 rounded-[10px] border border-custom-border-200 bg-custom-background-100 p-5 ${
@ -157,7 +174,11 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
name="color" name="color"
control={control} control={control}
render={({ field: { value, onChange } }) => ( render={({ field: { value, onChange } }) => (
<TwitterPicker color={value} onChange={(value) => onChange(value.hex)} /> <TwitterPicker
colors={LABEL_COLOR_OPTIONS}
color={value}
onChange={(value) => onChange(value.hex)}
/>
)} )}
/> />
</Popover.Panel> </Popover.Panel>
@ -179,14 +200,7 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
error={errors.name} error={errors.name}
/> />
</div> </div>
<SecondaryButton <SecondaryButton onClick={() => handleClose()}>Cancel</SecondaryButton>
onClick={() => {
reset();
setLabelForm(false);
}}
>
Cancel
</SecondaryButton>
{isUpdating ? ( {isUpdating ? (
<PrimaryButton onClick={handleSubmit(handleLabelUpdate)} loading={isSubmitting}> <PrimaryButton onClick={handleSubmit(handleLabelUpdate)} loading={isSubmitting}>
{isSubmitting ? "Updating" : "Update"} {isSubmitting ? "Updating" : "Update"}
@ -198,4 +212,5 @@ export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpd
)} )}
</div> </div>
); );
}); }
);

View 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];
};

View File

@ -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}
/> />
)} )}