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-03-17 05:10:38 +00:00
|
|
|
import { CustomSelect, Input, PrimaryButton, SecondaryButton } 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
|
|
|
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({
|
|
|
|
type: "success",
|
2023-04-17 19:45:39 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "State created successfully.",
|
2023-01-29 06:55:20 +00:00
|
|
|
});
|
2022-12-14 17:16:41 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-04-17 19:45:39 +00:00
|
|
|
if (err.status === 400)
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message:
|
|
|
|
"Another state exists with the same name. Please try again with another name.",
|
|
|
|
});
|
|
|
|
else
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "State could not be created. Please try again.",
|
2022-12-14 17:16:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} 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,
|
|
|
|
})
|
2023-04-17 19:45:39 +00:00
|
|
|
.then(() => {
|
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({
|
|
|
|
type: "success",
|
2023-04-17 19:45:39 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "State updated successfully.",
|
2023-01-29 06:55:20 +00:00
|
|
|
});
|
2022-12-14 17:16:41 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-04-17 19:45:39 +00:00
|
|
|
if (err.status === 400)
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message:
|
|
|
|
"Another state exists with the same name. Please try again with another name.",
|
|
|
|
});
|
|
|
|
else
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "State could not be updated. Please try again.",
|
2022-12-14 17:16:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-04-17 19:45:39 +00:00
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Popover className="relative flex h-full w-full items-center justify-center">
|
2022-12-14 17:16:41 +00:00
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<Popover.Button
|
2023-04-17 19:45:39 +00:00
|
|
|
className={`group inline-flex items-center text-base font-medium focus:outline-none ${
|
2022-12-14 17:16:41 +00:00
|
|
|
open ? "text-gray-900" : "text-gray-500"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{watch("color") && watch("color") !== "" && (
|
|
|
|
<span
|
2023-04-17 19:45:39 +00:00
|
|
|
className="h-5 w-5 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"
|
|
|
|
/>
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
2023-01-31 12:40:50 +00:00
|
|
|
{isSubmitting ? (data ? "Updating..." : "Creating...") : data ? "Update" : "Create"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-01-26 18:12:20 +00:00
|
|
|
</form>
|
2022-12-14 17:16:41 +00:00
|
|
|
);
|
|
|
|
};
|