2023-03-31 13:01:01 +00:00
|
|
|
import React, { forwardRef, useEffect } from "react";
|
2023-02-10 12:32:18 +00:00
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import { mutate } from "swr";
|
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { Controller, SubmitHandler, useForm } from "react-hook-form";
|
2023-06-06 16:06:00 +00:00
|
|
|
// hooks
|
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
2023-02-10 12:32:18 +00:00
|
|
|
// react-color
|
|
|
|
import { TwitterPicker } from "react-color";
|
|
|
|
// headless ui
|
|
|
|
import { Popover, Transition } from "@headlessui/react";
|
|
|
|
// services
|
|
|
|
import issuesService from "services/issues.service";
|
|
|
|
// ui
|
2023-03-17 05:10:38 +00:00
|
|
|
import { Input, PrimaryButton, SecondaryButton } from "components/ui";
|
2023-05-19 13:42:24 +00:00
|
|
|
// icons
|
|
|
|
import { ChevronDownIcon } from "@heroicons/react/24/outline";
|
2023-02-10 12:32:18 +00:00
|
|
|
// types
|
|
|
|
import { IIssueLabels } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
labelForm: boolean;
|
|
|
|
setLabelForm: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
isUpdating: boolean;
|
|
|
|
labelToUpdate: IIssueLabels | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IIssueLabels> = {
|
|
|
|
name: "",
|
2023-07-10 07:17:00 +00:00
|
|
|
color: "rgb(var(--color-text-200))",
|
2023-02-10 12:32:18 +00:00
|
|
|
};
|
|
|
|
|
2023-03-31 13:01:01 +00:00
|
|
|
type Ref = HTMLDivElement;
|
|
|
|
|
|
|
|
export const CreateUpdateLabelInline = forwardRef<Ref, Props>(function CreateUpdateLabelInline(
|
|
|
|
{ labelForm, setLabelForm, isUpdating, labelToUpdate },
|
|
|
|
ref
|
|
|
|
) {
|
2023-02-10 12:32:18 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
2023-06-06 16:06:00 +00:00
|
|
|
const { user } = useUserAuth();
|
|
|
|
|
2023-02-10 12:32:18 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
register,
|
|
|
|
reset,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
watch,
|
|
|
|
setValue,
|
|
|
|
} = useForm<IIssueLabels>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
|
|
|
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
|
|
|
|
|
|
|
await issuesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.createIssueLabel(workspaceSlug as string, projectId as string, formData, user)
|
2023-02-10 12:32:18 +00:00
|
|
|
.then((res) => {
|
|
|
|
mutate<IIssueLabels[]>(
|
|
|
|
PROJECT_ISSUE_LABELS(projectId as string),
|
|
|
|
(prevData) => [res, ...(prevData ?? [])],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
reset(defaultValues);
|
|
|
|
setLabelForm(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLabelUpdate: SubmitHandler<IIssueLabels> = async (formData) => {
|
|
|
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
|
|
|
|
|
|
|
await issuesService
|
|
|
|
.patchIssueLabel(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
labelToUpdate?.id ?? "",
|
2023-06-06 16:06:00 +00:00
|
|
|
formData,
|
|
|
|
user
|
2023-02-10 12:32:18 +00:00
|
|
|
)
|
2023-04-17 19:45:26 +00:00
|
|
|
.then(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
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;
|
|
|
|
|
2023-02-17 11:36:30 +00:00
|
|
|
setValue(
|
|
|
|
"color",
|
|
|
|
labelToUpdate.color && labelToUpdate.color !== "" ? labelToUpdate.color : "#000"
|
|
|
|
);
|
2023-02-10 12:32:18 +00:00
|
|
|
setValue("name", labelToUpdate.name);
|
|
|
|
}, [labelToUpdate, setValue]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2023-07-10 07:17:00 +00:00
|
|
|
className={`flex scroll-m-8 items-center gap-2 rounded-[10px] border border-custom-border-100 bg-custom-background-100 p-5 ${
|
2023-02-10 12:32:18 +00:00
|
|
|
labelForm ? "" : "hidden"
|
|
|
|
}`}
|
2023-03-31 13:01:01 +00:00
|
|
|
ref={ref}
|
2023-02-10 12:32:18 +00:00
|
|
|
>
|
2023-05-19 13:42:24 +00:00
|
|
|
<div className="flex-shrink-0">
|
2023-04-22 18:16:19 +00:00
|
|
|
<Popover className="relative z-10 flex h-full w-full items-center justify-center">
|
2023-02-10 12:32:18 +00:00
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<Popover.Button
|
2023-04-22 18:16:19 +00:00
|
|
|
className={`group inline-flex items-center text-base font-medium focus:outline-none ${
|
2023-07-10 07:17:00 +00:00
|
|
|
open ? "text-custom-text-100" : "text-custom-text-200"
|
2023-02-10 12:32:18 +00:00
|
|
|
}`}
|
|
|
|
>
|
2023-04-22 18:16:19 +00:00
|
|
|
<span
|
|
|
|
className="h-5 w-5 rounded"
|
|
|
|
style={{
|
|
|
|
backgroundColor: watch("color"),
|
|
|
|
}}
|
|
|
|
/>
|
2023-05-19 13:42:24 +00:00
|
|
|
<ChevronDownIcon
|
2023-07-10 07:17:00 +00:00
|
|
|
className={`ml-2 h-5 w-5 group-hover:text-custom-text-200 ${
|
2023-05-19 13:42:24 +00:00
|
|
|
open ? "text-gray-600" : "text-gray-400"
|
|
|
|
}`}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2023-02-10 12:32:18 +00:00
|
|
|
</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>
|
2023-05-19 13:42:24 +00:00
|
|
|
<div className="flex flex-1 flex-col justify-center">
|
2023-02-10 12:32:18 +00:00
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
id="labelName"
|
|
|
|
name="name"
|
|
|
|
register={register}
|
|
|
|
placeholder="Label title"
|
|
|
|
validations={{
|
|
|
|
required: "Label title is required",
|
|
|
|
}}
|
|
|
|
error={errors.name}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton
|
2023-02-10 12:32:18 +00:00
|
|
|
onClick={() => {
|
|
|
|
reset();
|
|
|
|
setLabelForm(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Cancel
|
2023-03-17 05:10:38 +00:00
|
|
|
</SecondaryButton>
|
2023-02-10 12:32:18 +00:00
|
|
|
{isUpdating ? (
|
2023-03-17 05:10:38 +00:00
|
|
|
<PrimaryButton onClick={handleSubmit(handleLabelUpdate)} loading={isSubmitting}>
|
2023-02-10 12:32:18 +00:00
|
|
|
{isSubmitting ? "Updating" : "Update"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-02-10 12:32:18 +00:00
|
|
|
) : (
|
2023-03-17 05:10:38 +00:00
|
|
|
<PrimaryButton onClick={handleSubmit(handleLabelCreate)} loading={isSubmitting}>
|
2023-02-10 12:32:18 +00:00
|
|
|
{isSubmitting ? "Adding" : "Add"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-02-10 12:32:18 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
2023-03-31 13:01:01 +00:00
|
|
|
});
|