import { Box, Button, Drawer, DrawerBody, DrawerCloseButton, DrawerContent, DrawerFooter, DrawerHeader, DrawerOverlay, FormControl, FormErrorMessage, FormHelperText, FormLabel, HStack, Input, Select, Stack, Text, Textarea, VStack, useToast, } from "@chakra-ui/react"; import * as yup from "yup"; import React, { useState, useEffect, useContext } from "react"; import { useForm, Controller } from "react-hook-form"; import { yupResolver } from "@hookform/resolvers/yup"; import { v4 as uuidv4 } from "uuid"; import { useParams } from "react-router-dom"; import CustomAlertDialog from "../../../../Components/CustomAlertDialog"; import ToastBox from "../../../../Components/ToastBox"; import GlobalStateContext from "../../../../Contexts/GlobalStateContext"; import CurrencyInput from "../../../../Components/CurrencyInput"; import { useAddNavDetailsMutation } from "../../../../Services/io.service"; import { formatDatee } from "../../../../Components/FormField"; const ioNav = yup.object().shape({ transactionDate: yup.string().required("Date is required"), transactionAmount: yup.number().required("New NAV is required"), comments: yup.string().notRequired() .max(200, "Approve Comment cannot be more than 200 characters"), }); const AddNavDetails = ({ isOpen, onClose, firstField, actionId, setActionId, data }) => { const params = useParams() const id = params?.id const [file, setFile] = useState(""); const [fileName, setFileName] = useState(""); const [isLoading, setIsLoading] = useState(false) const [alert, setAlert] = useState(false); const toast = useToast(); // ======================[ Cotext Api ] const { IODetails } = useContext(GlobalStateContext); const found = data?.find((item) => item?.id === actionId); const [addNavDetails] = useAddNavDetailsMutation() // const { // data // } = useGetArtifactsQuery(id) const { control, handleSubmit, watch, reset, formState: { errors }, } = useForm({ resolver: yupResolver(ioNav), }); const onSubmit = async (data) => { setIsLoading(true) try { const res = await addNavDetails({ data, id }) if (res?.data?.statusCode === 201) { setIsLoading(false); toast({ render: () => , }); handleClose() }else if(res?.error?.status === 400){ toast({ render: () => , }); handleClose() } } catch (error) { console.log(error); } }; const handleConfirm = () => { handleSubmit(onSubmit)(); }; const handleSave = () => { handleSubmit(onSubmit)(); }; const handleClose = () => { setIsLoading(false); setAlert(false) onClose() reset({ transactionDate:"", transactionAmount:"", comments:"" }) } const today = formatDatee(new Date(), 'yyyy-MM-dd'); function calculatePercentage(newNav, currNav) { const per = (newNav - currNav) / currNav * 100 return per.toFixed(2) } console.log(calculatePercentage(1092500, 976070)); return ( <> IO Nav Details Date Selection ( )} /> {errors.transactionDate?.message} New NAV ( )} /> {errors.transactionAmount?.message} Current nav {parseFloat(IODetails?.ioNAV || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, })} Live return % {calculatePercentage(watch()?.transactionAmount||IODetails?.ioNAV,IODetails?.ioNAV)} Comment (