diff --git a/apps/app/components/analytics/custom-analytics/create-update-analytics-modal.tsx b/apps/app/components/analytics/custom-analytics/create-update-analytics-modal.tsx new file mode 100644 index 000000000..625a2a4c7 --- /dev/null +++ b/apps/app/components/analytics/custom-analytics/create-update-analytics-modal.tsx @@ -0,0 +1,158 @@ +import React from "react"; + +import { useRouter } from "next/router"; + +// react-hook-form +import { useForm } from "react-hook-form"; +// headless ui +import { Dialog, Transition } from "@headlessui/react"; +// services +import analyticsService from "services/analytics.service"; +// hooks +import useToast from "hooks/use-toast"; +// ui +import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui"; +// types +import { IAnalyticsParams, ISaveAnalyticsFormData } from "types"; + +// types +type Props = { + isOpen: boolean; + handleClose: () => void; + params?: IAnalyticsParams; +}; + +type FormValues = { + name: string; + description: string; +}; + +const defaultValues: FormValues = { + name: "", + description: "", +}; + +export const CreateUpdateAnalyticsModal: React.FC = ({ isOpen, handleClose, params }) => { + const router = useRouter(); + const { workspaceSlug } = router.query; + + const { setToastAlert } = useToast(); + + const { + register, + formState: { errors, isSubmitting }, + handleSubmit, + reset, + } = useForm({ + defaultValues, + }); + + const onClose = () => { + handleClose(); + reset(defaultValues); + }; + + const onSubmit = async (formData: FormValues) => { + if (!workspaceSlug) return; + + const payload: ISaveAnalyticsFormData = { + name: formData.name, + description: formData.description, + query_dict: { + x_axis: "priority", + y_axis: "issue_count", + ...params, + project: params?.project ? [params.project] : [], + }, + }; + + await analyticsService + .saveAnalytics(workspaceSlug.toString(), payload) + .then(() => { + setToastAlert({ + type: "success", + title: "Success!", + message: "Analytics saved successfully.", + }); + onClose(); + }) + .catch(() => + setToastAlert({ + type: "error", + title: "Error!", + message: "Analytics could not be saved. Please try again.", + }) + ); + }; + + return ( + + + +
+ + +
+
+ + +
+
+ + Save Analytics + +
+ +