2022-12-06 14:38:28 +00:00
|
|
|
// react
|
|
|
|
import React, { useState } from "react";
|
|
|
|
// swr
|
|
|
|
import useSWR from "swr";
|
|
|
|
// react-hook-form
|
|
|
|
import { Controller, SubmitHandler, useForm } from "react-hook-form";
|
|
|
|
// react-color
|
|
|
|
import { TwitterPicker } from "react-color";
|
|
|
|
// services
|
2022-12-12 04:49:52 +00:00
|
|
|
import issuesServices from "lib/services/issues.service";
|
2022-12-06 14:38:28 +00:00
|
|
|
// hooks
|
|
|
|
import useUser from "lib/hooks/useUser";
|
|
|
|
// headless ui
|
2022-12-19 18:22:01 +00:00
|
|
|
import { Popover, Transition } from "@headlessui/react";
|
2022-12-06 14:38:28 +00:00
|
|
|
// ui
|
2022-12-19 18:22:01 +00:00
|
|
|
import { Button, Input, Spinner } from "ui";
|
2022-12-06 14:38:28 +00:00
|
|
|
// icons
|
2022-12-19 18:22:01 +00:00
|
|
|
import { PlusIcon } from "@heroicons/react/24/outline";
|
2022-12-06 14:38:28 +00:00
|
|
|
// types
|
|
|
|
import { IIssueLabels } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys";
|
2022-12-19 15:00:09 +00:00
|
|
|
import SingleLabel from "./single-label";
|
2022-12-06 14:38:28 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<IIssueLabels> = {
|
|
|
|
name: "",
|
|
|
|
colour: "#ff0000",
|
|
|
|
};
|
|
|
|
|
|
|
|
const LabelsSettings: React.FC = () => {
|
|
|
|
const [newLabelForm, setNewLabelForm] = useState(false);
|
|
|
|
const [isUpdating, setIsUpdating] = useState(false);
|
|
|
|
const [labelIdForUpdate, setLabelidForUpdate] = useState<string | null>(null);
|
|
|
|
|
|
|
|
const { activeWorkspace, activeProject } = useUser();
|
|
|
|
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
|
|
|
control,
|
|
|
|
setValue,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
watch,
|
|
|
|
} = useForm<IIssueLabels>({ defaultValues });
|
|
|
|
|
|
|
|
const { data: issueLabels, mutate } = useSWR<IIssueLabels[]>(
|
|
|
|
activeProject && activeWorkspace ? PROJECT_ISSUE_LABELS(activeProject.id) : null,
|
|
|
|
activeProject && activeWorkspace
|
|
|
|
? () => issuesServices.getIssueLabels(activeWorkspace.slug, activeProject.id)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleNewLabel: SubmitHandler<IIssueLabels> = (formData) => {
|
|
|
|
if (!activeWorkspace || !activeProject || isSubmitting) return;
|
|
|
|
issuesServices
|
|
|
|
.createIssueLabel(activeWorkspace.slug, activeProject.id, formData)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
reset(defaultValues);
|
|
|
|
mutate((prevData) => [...(prevData ?? []), res], false);
|
|
|
|
setNewLabelForm(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-19 15:00:09 +00:00
|
|
|
const editLabel = (label: IIssueLabels) => {
|
|
|
|
setNewLabelForm(true);
|
|
|
|
setValue("colour", label.colour);
|
|
|
|
setValue("name", label.name);
|
|
|
|
setIsUpdating(true);
|
|
|
|
setLabelidForUpdate(label.id);
|
|
|
|
};
|
|
|
|
|
2022-12-06 14:38:28 +00:00
|
|
|
const handleLabelUpdate: SubmitHandler<IIssueLabels> = (formData) => {
|
|
|
|
if (!activeWorkspace || !activeProject || isSubmitting) return;
|
|
|
|
issuesServices
|
|
|
|
.patchIssueLabel(activeWorkspace.slug, activeProject.id, labelIdForUpdate ?? "", formData)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
reset(defaultValues);
|
|
|
|
mutate(
|
|
|
|
(prevData) =>
|
|
|
|
prevData?.map((p) => (p.id === labelIdForUpdate ? { ...p, ...formData } : p)),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
setNewLabelForm(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLabelDelete = (labelId: string) => {
|
|
|
|
if (activeWorkspace && activeProject) {
|
|
|
|
mutate((prevData) => prevData?.filter((p) => p.id !== labelId), false);
|
|
|
|
issuesServices
|
|
|
|
.deleteIssueLabel(activeWorkspace.slug, activeProject.id, labelId)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-12-19 18:22:01 +00:00
|
|
|
<section className="space-y-8">
|
|
|
|
<div className="md:w-2/3 flex justify-between items-center gap-2">
|
2022-12-06 14:38:28 +00:00
|
|
|
<div>
|
2022-12-19 18:22:01 +00:00
|
|
|
<h3 className="text-3xl font-bold leading-6 text-gray-900">Labels</h3>
|
|
|
|
<p className="mt-4 text-sm text-gray-500">Manage the labels of this project.</p>
|
2022-12-06 14:38:28 +00:00
|
|
|
</div>
|
2022-12-19 15:00:09 +00:00
|
|
|
<Button
|
|
|
|
theme="secondary"
|
|
|
|
className="flex items-center gap-x-1"
|
|
|
|
onClick={() => setNewLabelForm(true)}
|
|
|
|
>
|
2022-12-06 14:38:28 +00:00
|
|
|
<PlusIcon className="h-4 w-4" />
|
|
|
|
New label
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-5">
|
2022-12-13 15:52:44 +00:00
|
|
|
<div
|
2022-12-19 15:00:09 +00:00
|
|
|
className={`md:w-2/3 border p-3 rounded-md flex items-center gap-2 ${
|
|
|
|
newLabelForm ? "" : "hidden"
|
|
|
|
}`}
|
2022-12-06 14:38:28 +00:00
|
|
|
>
|
2022-12-19 15:00:09 +00:00
|
|
|
<div className="flex-shrink-0 h-8 w-8">
|
|
|
|
<Popover className="relative w-full h-full flex justify-center items-center bg-gray-200 rounded-xl">
|
2022-12-06 14:38:28 +00:00
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<Popover.Button
|
2022-12-19 15:00:09 +00:00
|
|
|
className={`group inline-flex items-center text-base font-medium focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 ${
|
|
|
|
open ? "text-gray-900" : "text-gray-500"
|
|
|
|
}`}
|
2022-12-06 14:38:28 +00:00
|
|
|
>
|
|
|
|
{watch("colour") && watch("colour") !== "" && (
|
|
|
|
<span
|
2022-12-19 15:00:09 +00:00
|
|
|
className="w-4 h-4 rounded"
|
2022-12-06 14:38:28 +00:00
|
|
|
style={{
|
|
|
|
backgroundColor: watch("colour") ?? "green",
|
|
|
|
}}
|
|
|
|
></span>
|
|
|
|
)}
|
|
|
|
</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"
|
|
|
|
>
|
2022-12-19 15:00:09 +00:00
|
|
|
<Popover.Panel className="absolute top-full z-20 left-0 mt-3 px-2 w-screen max-w-xs sm:px-0">
|
2022-12-06 14:38:28 +00:00
|
|
|
<Controller
|
|
|
|
name="colour"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<TwitterPicker
|
|
|
|
color={value}
|
|
|
|
onChange={(value) => onChange(value.hex)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</Popover.Panel>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
</div>
|
2022-12-07 23:27:10 +00:00
|
|
|
<div className="w-full flex flex-col justify-center">
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
id="labelName"
|
|
|
|
name="name"
|
|
|
|
register={register}
|
|
|
|
placeholder="Lable title"
|
|
|
|
validations={{
|
|
|
|
required: "Label title is required",
|
|
|
|
}}
|
|
|
|
error={errors.name}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-06 14:38:28 +00:00
|
|
|
<Button type="button" theme="secondary" onClick={() => setNewLabelForm(false)}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
{isUpdating ? (
|
2022-12-07 23:27:10 +00:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
onClick={handleSubmit(handleLabelUpdate)}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
>
|
|
|
|
{isSubmitting ? "Updating" : "Update"}
|
2022-12-06 14:38:28 +00:00
|
|
|
</Button>
|
|
|
|
) : (
|
2022-12-07 23:27:10 +00:00
|
|
|
<Button type="button" onClick={handleSubmit(handleNewLabel)} disabled={isSubmitting}>
|
|
|
|
{isSubmitting ? "Adding" : "Add"}
|
2022-12-06 14:38:28 +00:00
|
|
|
</Button>
|
|
|
|
)}
|
2022-12-13 15:52:44 +00:00
|
|
|
</div>
|
2022-12-19 15:00:09 +00:00
|
|
|
<>
|
|
|
|
{issueLabels ? (
|
|
|
|
issueLabels.map((label) => (
|
|
|
|
<SingleLabel
|
|
|
|
key={label.id}
|
|
|
|
label={label}
|
|
|
|
issueLabels={issueLabels}
|
|
|
|
editLabel={editLabel}
|
|
|
|
handleLabelDelete={handleLabelDelete}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<div className="flex justify-center py-4">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
2022-12-06 14:38:28 +00:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LabelsSettings;
|