2022-12-14 17:16:41 +00:00
|
|
|
import React, { useEffect } from "react";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-02-10 12:32:18 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2022-12-14 17:16:41 +00:00
|
|
|
import { mutate } from "swr";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-01-29 06:55:20 +00:00
|
|
|
// react-hook-form
|
2022-12-14 17:16:41 +00:00
|
|
|
import { useForm, Controller } from "react-hook-form";
|
2023-01-29 06:55:20 +00:00
|
|
|
// react-color
|
2022-12-14 17:16:41 +00:00
|
|
|
import { TwitterPicker } from "react-color";
|
2023-01-29 06:55:20 +00:00
|
|
|
// headless ui
|
2022-12-14 17:16:41 +00:00
|
|
|
import { Popover, Transition } from "@headlessui/react";
|
|
|
|
// services
|
2023-01-26 18:12:20 +00:00
|
|
|
import stateService from "services/state.service";
|
2023-01-29 06:55:20 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2022-12-14 17:16:41 +00:00
|
|
|
// ui
|
2023-02-08 04:43:07 +00:00
|
|
|
import { Button, CustomSelect, Input } from "components/ui";
|
2022-12-14 17:16:41 +00:00
|
|
|
// types
|
2023-02-10 12:32:18 +00:00
|
|
|
import type { IState } from "types";
|
2023-01-29 06:55:20 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { STATE_LIST } from "constants/fetch-keys";
|
|
|
|
// constants
|
2023-02-08 04:43:07 +00:00
|
|
|
import { GROUP_CHOICES } from "constants/project";
|
2022-12-14 17:16:41 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
data: IState | null;
|
|
|
|
onClose: () => void;
|
|
|
|
selectedGroup: StateGroup | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type StateGroup = "backlog" | "unstarted" | "started" | "completed" | "cancelled" | null;
|
|
|
|
|
2022-12-16 14:56:25 +00:00
|
|
|
const defaultValues: Partial<IState> = {
|
|
|
|
name: "",
|
|
|
|
color: "#000000",
|
|
|
|
group: "backlog",
|
|
|
|
};
|
|
|
|
|
2023-02-10 12:32:18 +00:00
|
|
|
export const CreateUpdateStateInline: React.FC<Props> = ({ data, onClose, selectedGroup }) => {
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
2023-01-29 06:55:20 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2022-12-14 17:16:41 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
2022-12-16 14:56:25 +00:00
|
|
|
formState: { errors, isSubmitting },
|
2022-12-14 17:16:41 +00:00
|
|
|
setError,
|
|
|
|
watch,
|
|
|
|
reset,
|
|
|
|
control,
|
|
|
|
} = useForm<IState>({
|
2022-12-16 14:56:25 +00:00
|
|
|
defaultValues,
|
2022-12-14 17:16:41 +00:00
|
|
|
});
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
useEffect(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
if (!data) return;
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
reset(data);
|
|
|
|
}, [data, reset]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
if (data) return;
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
group: selectedGroup ?? "backlog",
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
}, [selectedGroup, data, reset]);
|
|
|
|
|
2022-12-14 17:16:41 +00:00
|
|
|
const handleClose = () => {
|
|
|
|
onClose();
|
|
|
|
reset({ name: "", color: "#000000", group: "backlog" });
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSubmit = async (formData: IState) => {
|
2022-12-16 14:56:25 +00:00
|
|
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
2023-02-10 12:32:18 +00:00
|
|
|
|
2022-12-14 17:16:41 +00:00
|
|
|
const payload: IState = {
|
|
|
|
...formData,
|
|
|
|
};
|
|
|
|
if (!data) {
|
|
|
|
await stateService
|
2023-02-10 12:32:18 +00:00
|
|
|
.createState(workspaceSlug as string, projectId as string, { ...payload })
|
2022-12-14 17:16:41 +00:00
|
|
|
.then((res) => {
|
2023-02-10 12:32:18 +00:00
|
|
|
mutate(STATE_LIST(projectId as string));
|
2022-12-14 17:16:41 +00:00
|
|
|
handleClose();
|
2023-01-29 06:55:20 +00:00
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
title: "Success",
|
|
|
|
type: "success",
|
|
|
|
message: "State created successfully",
|
|
|
|
});
|
2022-12-14 17:16:41 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
Object.keys(err).map((key) => {
|
|
|
|
setError(key as keyof IState, {
|
|
|
|
message: err[key].join(", "),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await stateService
|
2023-02-10 12:32:18 +00:00
|
|
|
.updateState(workspaceSlug as string, projectId as string, data.id, {
|
2022-12-14 17:16:41 +00:00
|
|
|
...payload,
|
|
|
|
})
|
|
|
|
.then((res) => {
|
2023-02-10 12:32:18 +00:00
|
|
|
mutate(STATE_LIST(projectId as string));
|
2022-12-14 17:16:41 +00:00
|
|
|
handleClose();
|
2023-01-29 06:55:20 +00:00
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
title: "Success",
|
|
|
|
type: "success",
|
|
|
|
message: "State updated successfully",
|
|
|
|
});
|
2022-12-14 17:16:41 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
Object.keys(err).map((key) => {
|
|
|
|
setError(key as keyof IState, {
|
|
|
|
message: err[key].join(", "),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-03-03 07:59:36 +00:00
|
|
|
<form
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
className="flex items-center gap-x-2 rounded-[10px] bg-white p-5"
|
|
|
|
>
|
2023-01-26 18:12:20 +00:00
|
|
|
<div className="h-8 w-8 flex-shrink-0">
|
|
|
|
<Popover className="relative flex h-full w-full items-center justify-center rounded-xl bg-gray-200">
|
2022-12-14 17:16:41 +00:00
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<Popover.Button
|
2023-01-26 18:12:20 +00:00
|
|
|
className={`group inline-flex items-center text-base font-medium focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 ${
|
2022-12-14 17:16:41 +00:00
|
|
|
open ? "text-gray-900" : "text-gray-500"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{watch("color") && watch("color") !== "" && (
|
|
|
|
<span
|
2023-01-26 18:12:20 +00:00
|
|
|
className="h-4 w-4 rounded"
|
2022-12-14 17:16:41 +00:00
|
|
|
style={{
|
2023-02-17 11:36:30 +00:00
|
|
|
backgroundColor: watch("color") ?? "black",
|
2022-12-14 17:16:41 +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"
|
|
|
|
>
|
2023-01-26 18:12:20 +00:00
|
|
|
<Popover.Panel className="absolute top-full left-0 z-20 mt-3 w-screen max-w-xs px-2 sm:px-0">
|
2022-12-14 17:16:41 +00:00
|
|
|
<Controller
|
|
|
|
name="color"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<TwitterPicker color={value} onChange={(value) => onChange(value.hex)} />
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</Popover.Panel>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
</div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
register={register}
|
2023-01-26 18:12:20 +00:00
|
|
|
autoFocus
|
2023-03-03 07:59:36 +00:00
|
|
|
placeholder="Name"
|
2022-12-14 17:16:41 +00:00
|
|
|
validations={{
|
2022-12-16 14:56:25 +00:00
|
|
|
required: true,
|
2022-12-14 17:16:41 +00:00
|
|
|
}}
|
|
|
|
error={errors.name}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
2022-12-16 14:56:25 +00:00
|
|
|
{data && (
|
2023-01-31 12:40:50 +00:00
|
|
|
<Controller
|
2022-12-16 14:56:25 +00:00
|
|
|
name="group"
|
2023-01-31 12:40:50 +00:00
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomSelect
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
label={
|
|
|
|
Object.keys(GROUP_CHOICES).find((k) => k === value.toString())
|
|
|
|
? GROUP_CHOICES[value.toString() as keyof typeof GROUP_CHOICES]
|
|
|
|
: "Select group"
|
|
|
|
}
|
|
|
|
input
|
|
|
|
>
|
|
|
|
{Object.keys(GROUP_CHOICES).map((key) => (
|
|
|
|
<CustomSelect.Option key={key} value={key}>
|
|
|
|
{GROUP_CHOICES[key as keyof typeof GROUP_CHOICES]}
|
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
|
|
|
)}
|
2022-12-16 14:56:25 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-12-14 17:16:41 +00:00
|
|
|
<Input
|
|
|
|
id="description"
|
|
|
|
name="description"
|
|
|
|
register={register}
|
2023-03-03 07:59:36 +00:00
|
|
|
placeholder="Description"
|
2022-12-14 17:16:41 +00:00
|
|
|
error={errors.description}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
<Button theme="secondary" onClick={handleClose}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
2023-01-26 18:12:20 +00:00
|
|
|
<Button theme="primary" disabled={isSubmitting} type="submit">
|
2023-01-31 12:40:50 +00:00
|
|
|
{isSubmitting ? (data ? "Updating..." : "Creating...") : data ? "Update" : "Create"}
|
2022-12-14 17:16:41 +00:00
|
|
|
</Button>
|
2023-01-26 18:12:20 +00:00
|
|
|
</form>
|
2022-12-14 17:16:41 +00:00
|
|
|
);
|
|
|
|
};
|