From 95fe4a3831226cbb7dba33128b7765d71398c62a Mon Sep 17 00:00:00 2001 From: Kunal Vishwakarma <116634168+kunalv17@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:09:24 +0530 Subject: [PATCH] feat: added estimates (#721) * feat: added estimates * chore: added estimate to issue sidebar --- .../create-update-estimate-modal.tsx | 205 +++++++++ .../estimates/estimate-points-modal.tsx | 432 ++++++++++++++++++ apps/app/components/estimates/index.tsx | 3 + .../components/estimates/single-estimate.tsx | 150 ++++++ apps/app/components/issues/form.tsx | 13 +- .../app/components/issues/select/estimate.tsx | 68 +++ apps/app/components/issues/select/index.ts | 3 +- .../issues/sidebar-select/estimate.tsx | 35 ++ .../components/issues/sidebar-select/index.ts | 1 + apps/app/components/issues/sidebar.tsx | 12 + apps/app/constants/fetch-keys.ts | 6 + apps/app/layouts/settings-navbar.tsx | 4 + .../[projectId]/settings/estimates.tsx | 178 ++++++++ apps/app/services/estimates.service.ts | 152 ++++++ apps/app/types/estimate.d.ts | 25 + apps/app/types/index.d.ts | 1 + apps/app/types/issues.d.ts | 1 + apps/app/types/projects.d.ts | 1 + 18 files changed, 1288 insertions(+), 2 deletions(-) create mode 100644 apps/app/components/estimates/create-update-estimate-modal.tsx create mode 100644 apps/app/components/estimates/estimate-points-modal.tsx create mode 100644 apps/app/components/estimates/index.tsx create mode 100644 apps/app/components/estimates/single-estimate.tsx create mode 100644 apps/app/components/issues/select/estimate.tsx create mode 100644 apps/app/components/issues/sidebar-select/estimate.tsx create mode 100644 apps/app/pages/[workspaceSlug]/projects/[projectId]/settings/estimates.tsx create mode 100644 apps/app/services/estimates.service.ts create mode 100644 apps/app/types/estimate.d.ts diff --git a/apps/app/components/estimates/create-update-estimate-modal.tsx b/apps/app/components/estimates/create-update-estimate-modal.tsx new file mode 100644 index 000000000..da127a455 --- /dev/null +++ b/apps/app/components/estimates/create-update-estimate-modal.tsx @@ -0,0 +1,205 @@ +import React, { useEffect } from "react"; + +import { useRouter } from "next/router"; + +import { mutate } from "swr"; + +// react-hook-form +import { useForm } from "react-hook-form"; +// services +import estimatesService from "services/estimates.service"; +// ui +import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui"; +import { Dialog, Transition } from "@headlessui/react"; + +// hooks +import useToast from "hooks/use-toast"; + +// types +import { IEstimate } from "types"; +// fetch-keys +import { ESTIMATES_LIST } from "constants/fetch-keys"; + +type Props = { + handleClose: () => void; + data?: IEstimate; + isOpen: boolean; + isCreate: boolean; +}; + +const defaultValues: Partial = { + name: "", + description: "", +}; + +export const CreateUpdateEstimateModal: React.FC = ({ handleClose, data, isOpen, isCreate }) => { + const { + register, + formState: { errors, isSubmitting }, + handleSubmit, + reset, + } = useForm({ + defaultValues, + }); + + const router = useRouter(); + const { workspaceSlug, projectId } = router.query; + + const { setToastAlert } = useToast(); + + const createEstimate = async (formData: IEstimate) => { + if (!workspaceSlug || !projectId) return; + + const payload = { + name: formData.name, + description: formData.description, + }; + + await estimatesService + .createEstimate(workspaceSlug as string, projectId as string, payload) + .then((res) => { + mutate( + ESTIMATES_LIST(projectId as string), + (prevData) => [res, ...(prevData ?? [])], + false + ); + handleClose(); + }) + .catch(() => { + setToastAlert({ + type: "error", + title: "Error!", + message: "Error: Estimate could not be created", + }); + }); + }; + + const updateEstimate = async (formData: IEstimate) => { + if (!workspaceSlug || !projectId || !data) return; + + const payload = { + name: formData.name, + description: formData.description, + }; + + mutate( + ESTIMATES_LIST(projectId as string), + (prevData) => + prevData?.map((p) => { + if (p.id === data.id) return { ...p, ...payload }; + + return p; + }), + false + ); + + await estimatesService + .patchEstimate(workspaceSlug as string, projectId as string, data?.id as string, payload) + .then(() => { + handleClose(); + }) + .catch(() => { + setToastAlert({ + type: "error", + title: "Error!", + message: "Error: Estimate could not be updated", + }); + }); + handleClose(); + }; + + useEffect(() => { + if (!data && isCreate) return; + reset({ + ...defaultValues, + ...data, + }); + }, [data, reset, isCreate]); + + return ( + <> + + + +
+ + +
+
+ + +
+
+
Create Estimate
+
+ +
+
+