2023-04-06 09:39:24 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import { mutate } from "swr";
|
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { useForm } from "react-hook-form";
|
2023-04-21 19:29:57 +00:00
|
|
|
// headless ui
|
|
|
|
import { Dialog, Transition } from "@headlessui/react";
|
2023-04-06 09:39:24 +00:00
|
|
|
// services
|
|
|
|
import estimatesService from "services/estimates.service";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-04-21 19:29:57 +00:00
|
|
|
// ui
|
|
|
|
import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
|
|
|
// helpers
|
|
|
|
import { checkDuplicates } from "helpers/array.helper";
|
2023-04-06 09:39:24 +00:00
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import { ICurrentUserResponse, IEstimate, IEstimateFormData } from "types";
|
2023-04-06 09:39:24 +00:00
|
|
|
// fetch-keys
|
2023-04-21 19:29:57 +00:00
|
|
|
import { ESTIMATES_LIST, ESTIMATE_DETAILS } from "constants/fetch-keys";
|
2023-04-06 09:39:24 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-04-11 12:24:01 +00:00
|
|
|
isOpen: boolean;
|
2023-04-06 09:39:24 +00:00
|
|
|
handleClose: () => void;
|
|
|
|
data?: IEstimate;
|
2023-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-04-06 09:39:24 +00:00
|
|
|
};
|
|
|
|
|
2023-04-21 19:29:57 +00:00
|
|
|
type FormValues = {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
value1: string;
|
|
|
|
value2: string;
|
|
|
|
value3: string;
|
|
|
|
value4: string;
|
|
|
|
value5: string;
|
|
|
|
value6: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<FormValues> = {
|
2023-04-06 09:39:24 +00:00
|
|
|
name: "",
|
|
|
|
description: "",
|
2023-04-21 19:29:57 +00:00
|
|
|
value1: "",
|
|
|
|
value2: "",
|
|
|
|
value3: "",
|
|
|
|
value4: "",
|
|
|
|
value5: "",
|
|
|
|
value6: "",
|
2023-04-06 09:39:24 +00:00
|
|
|
};
|
|
|
|
|
2023-06-06 16:06:00 +00:00
|
|
|
export const CreateUpdateEstimateModal: React.FC<Props> = ({ handleClose, data, isOpen, user }) => {
|
2023-04-06 09:39:24 +00:00
|
|
|
const {
|
|
|
|
register,
|
2023-04-21 19:29:57 +00:00
|
|
|
formState: { isSubmitting },
|
2023-04-06 09:39:24 +00:00
|
|
|
handleSubmit,
|
|
|
|
reset,
|
2023-04-21 19:29:57 +00:00
|
|
|
} = useForm<FormValues>({
|
2023-04-06 09:39:24 +00:00
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
2023-04-20 06:39:35 +00:00
|
|
|
const onClose = () => {
|
|
|
|
handleClose();
|
|
|
|
reset();
|
|
|
|
};
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2023-04-21 19:29:57 +00:00
|
|
|
const createEstimate = async (payload: IEstimateFormData) => {
|
2023-04-06 09:39:24 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
await estimatesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.createEstimate(workspaceSlug as string, projectId as string, payload, user)
|
2023-04-21 19:29:57 +00:00
|
|
|
.then(() => {
|
|
|
|
mutate(ESTIMATES_LIST(projectId as string));
|
|
|
|
onClose();
|
2023-04-06 09:39:24 +00:00
|
|
|
})
|
2023-04-21 19:29:57 +00:00
|
|
|
.catch((err) => {
|
|
|
|
if (err.status === 400)
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate with that name already exists. Please try again with another name.",
|
|
|
|
});
|
|
|
|
else
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate could not be created. Please try again.",
|
|
|
|
});
|
2023-04-06 09:39:24 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-21 19:29:57 +00:00
|
|
|
const updateEstimate = async (payload: IEstimateFormData) => {
|
2023-04-06 09:39:24 +00:00
|
|
|
if (!workspaceSlug || !projectId || !data) return;
|
|
|
|
|
|
|
|
mutate<IEstimate[]>(
|
2023-04-21 19:29:57 +00:00
|
|
|
ESTIMATES_LIST(projectId.toString()),
|
2023-04-06 09:39:24 +00:00
|
|
|
(prevData) =>
|
|
|
|
prevData?.map((p) => {
|
2023-04-21 19:29:57 +00:00
|
|
|
if (p.id === data.id)
|
|
|
|
return {
|
|
|
|
...p,
|
|
|
|
name: payload.estimate.name,
|
|
|
|
description: payload.estimate.description,
|
|
|
|
points: p.points.map((point, index) => ({
|
|
|
|
...point,
|
|
|
|
value: payload.estimate_points[index].value,
|
|
|
|
})),
|
|
|
|
};
|
2023-04-06 09:39:24 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
await estimatesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.patchEstimate(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
data?.id as string,
|
|
|
|
payload,
|
|
|
|
user
|
|
|
|
)
|
2023-04-21 19:29:57 +00:00
|
|
|
.then(() => {
|
|
|
|
mutate(ESTIMATES_LIST(projectId.toString()));
|
|
|
|
mutate(ESTIMATE_DETAILS(data.id));
|
|
|
|
handleClose();
|
|
|
|
})
|
2023-04-06 09:39:24 +00:00
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
2023-04-21 19:29:57 +00:00
|
|
|
message: "Estimate could not be updated. Please try again.",
|
2023-04-06 09:39:24 +00:00
|
|
|
});
|
|
|
|
});
|
2023-04-20 06:39:35 +00:00
|
|
|
|
|
|
|
onClose();
|
2023-04-06 09:39:24 +00:00
|
|
|
};
|
|
|
|
|
2023-04-21 19:29:57 +00:00
|
|
|
const onSubmit = async (formData: FormValues) => {
|
|
|
|
if (!formData.name || formData.name === "") {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate title cannot be empty.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
formData.value1 === "" ||
|
|
|
|
formData.value2 === "" ||
|
|
|
|
formData.value3 === "" ||
|
|
|
|
formData.value4 === "" ||
|
|
|
|
formData.value5 === "" ||
|
|
|
|
formData.value6 === ""
|
|
|
|
) {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate point cannot be empty.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
checkDuplicates([
|
|
|
|
formData.value1,
|
|
|
|
formData.value2,
|
|
|
|
formData.value3,
|
|
|
|
formData.value4,
|
|
|
|
formData.value5,
|
|
|
|
formData.value6,
|
|
|
|
])
|
|
|
|
) {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate points cannot have duplicate values.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload: IEstimateFormData = {
|
|
|
|
estimate: {
|
|
|
|
name: formData.name,
|
|
|
|
description: formData.description,
|
|
|
|
},
|
2023-04-28 21:57:03 +00:00
|
|
|
estimate_points: [],
|
2023-04-21 19:29:57 +00:00
|
|
|
};
|
|
|
|
|
2023-04-28 21:57:03 +00:00
|
|
|
for (let i = 0; i < 6; i++) {
|
|
|
|
const point = {
|
|
|
|
key: i,
|
|
|
|
value: formData[`value${i + 1}` as keyof FormValues],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
payload.estimate_points.push({
|
|
|
|
id: data.points[i].id,
|
|
|
|
...point,
|
|
|
|
});
|
|
|
|
else payload.estimate_points.push({ ...point });
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:29:57 +00:00
|
|
|
if (data) await updateEstimate(payload);
|
|
|
|
else await createEstimate(payload);
|
|
|
|
};
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
useEffect(() => {
|
2023-04-21 19:29:57 +00:00
|
|
|
if (data)
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
value1: data.points[0]?.value,
|
|
|
|
value2: data.points[1]?.value,
|
|
|
|
value3: data.points[2]?.value,
|
|
|
|
value4: data.points[3]?.value,
|
|
|
|
value5: data.points[4]?.value,
|
|
|
|
value6: data.points[5]?.value,
|
|
|
|
});
|
|
|
|
else reset({ ...defaultValues });
|
2023-04-11 12:24:01 +00:00
|
|
|
}, [data, reset]);
|
2023-04-06 09:39:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Transition.Root show={isOpen} as={React.Fragment}>
|
|
|
|
<Dialog as="div" className="relative z-20" onClose={handleClose}>
|
|
|
|
<Transition.Child
|
|
|
|
as={React.Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="fixed inset-0 bg-custom-backdrop bg-opacity-50 transition-opacity" />
|
2023-04-06 09:39:24 +00:00
|
|
|
</Transition.Child>
|
|
|
|
|
|
|
|
<div className="fixed inset-0 z-20 overflow-y-auto">
|
|
|
|
<div className="flex min-h-full items-center justify-center p-4 text-center sm:p-0">
|
|
|
|
<Transition.Child
|
|
|
|
as={React.Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
|
|
|
enterTo="opacity-100 translate-y-0 sm:scale-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
|
|
|
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<Dialog.Panel className="relative transform rounded-lg border border-custom-border-100 bg-custom-background-100 px-5 py-8 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-2xl sm:p-6">
|
2023-04-21 19:29:57 +00:00
|
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
2023-04-06 09:39:24 +00:00
|
|
|
<div className="space-y-3">
|
2023-04-11 12:24:01 +00:00
|
|
|
<div className="text-lg font-medium leading-6">
|
|
|
|
{data ? "Update" : "Create"} Estimate
|
|
|
|
</div>
|
2023-04-06 09:39:24 +00:00
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
|
|
|
placeholder="Title"
|
|
|
|
autoComplete="off"
|
|
|
|
className="resize-none text-xl"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
|
|
|
placeholder="Description"
|
|
|
|
className="h-32 resize-none text-sm"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-04-21 19:29:57 +00:00
|
|
|
<div className="grid grid-cols-3 gap-3">
|
|
|
|
<div className="flex items-center">
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex h-full items-center rounded-lg bg-custom-background-80">
|
|
|
|
<span className="rounded-lg px-2 text-sm text-custom-text-200">1</span>
|
|
|
|
<span className="rounded-r-lg bg-custom-background-100">
|
2023-04-21 19:29:57 +00:00
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="value1"
|
|
|
|
type="name"
|
|
|
|
className="rounded-l-none"
|
|
|
|
placeholder="Point 1"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex h-full items-center rounded-lg bg-custom-background-80">
|
|
|
|
<span className="rounded-lg px-2 text-sm text-custom-text-200">2</span>
|
|
|
|
<span className="rounded-r-lg bg-custom-background-100">
|
2023-04-21 19:29:57 +00:00
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="value2"
|
|
|
|
type="name"
|
|
|
|
className="rounded-l-none"
|
|
|
|
placeholder="Point 2"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex h-full items-center rounded-lg bg-custom-background-80">
|
|
|
|
<span className="rounded-lg px-2 text-sm text-custom-text-200">3</span>
|
|
|
|
<span className="rounded-r-lg bg-custom-background-100">
|
2023-04-21 19:29:57 +00:00
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="value3"
|
|
|
|
type="name"
|
|
|
|
className="rounded-l-none"
|
|
|
|
placeholder="Point 3"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex h-full items-center rounded-lg bg-custom-background-80">
|
|
|
|
<span className="rounded-lg px-2 text-sm text-custom-text-200">4</span>
|
|
|
|
<span className="rounded-r-lg bg-custom-background-100">
|
2023-04-21 19:29:57 +00:00
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="value4"
|
|
|
|
type="name"
|
|
|
|
className="rounded-l-none"
|
|
|
|
placeholder="Point 4"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex h-full items-center rounded-lg bg-custom-background-80">
|
|
|
|
<span className="rounded-lg px-2 text-sm text-custom-text-200">5</span>
|
|
|
|
<span className="rounded-r-lg bg-custom-background-100">
|
2023-04-21 19:29:57 +00:00
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="value5"
|
|
|
|
type="name"
|
|
|
|
className="rounded-l-none"
|
|
|
|
placeholder="Point 5"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex h-full items-center rounded-lg bg-custom-background-80">
|
|
|
|
<span className="rounded-lg px-2 text-sm text-custom-text-200">6</span>
|
|
|
|
<span className="rounded-r-lg bg-custom-background-100">
|
2023-04-21 19:29:57 +00:00
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="value6"
|
|
|
|
type="name"
|
|
|
|
className="rounded-l-none"
|
|
|
|
placeholder="Point 6"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-04-06 09:39:24 +00:00
|
|
|
</div>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
|
|
|
{data
|
|
|
|
? isSubmitting
|
|
|
|
? "Updating Estimate..."
|
|
|
|
: "Update Estimate"
|
|
|
|
: isSubmitting
|
|
|
|
? "Creating Estimate..."
|
|
|
|
: "Create Estimate"}
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|