Files
tanami-admin-panel/src/Pages/IO_Management/CreateIO/IONAVDetails/AddNavDetails.jsx

267 lines
7.4 KiB
React
Raw Normal View History

2024-11-14 16:04:20 +05:30
import {
Box,
Button,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
FormControl,
FormErrorMessage,
2024-12-02 12:23:27 +05:30
FormHelperText,
2024-11-14 16:04:20 +05:30
FormLabel,
2024-11-18 17:54:23 +05:30
HStack,
2024-11-14 16:04:20 +05:30
Input,
Select,
Stack,
2024-11-18 17:54:23 +05:30
Text,
2024-11-14 16:04:20 +05:30
Textarea,
2024-11-18 17:54:23 +05:30
VStack,
2024-11-14 16:04:20 +05:30
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";
2024-11-18 17:54:23 +05:30
import { useAddNavDetailsMutation } from "../../../../Services/io.service";
import { formatDatee } from "../../../../Components/FormField";
2024-11-14 16:04:20 +05:30
2024-11-18 17:54:23 +05:30
const ioNav = yup.object().shape({
2024-11-14 16:04:20 +05:30
transactionDate: yup.string().required("Date is required"),
2024-11-18 17:54:23 +05:30
transactionAmount: yup.number().required("New NAV is required"),
2024-12-02 12:23:27 +05:30
comments: yup.string().notRequired()
.max(200, "Approve Comment cannot be more than 200 characters"),
2024-11-14 16:04:20 +05:30
});
2024-11-18 17:54:23 +05:30
const AddNavDetails = ({ isOpen, onClose, firstField, actionId, setActionId, data }) => {
const params = useParams()
2024-11-14 16:04:20 +05:30
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);
2024-11-18 17:54:23 +05:30
const [addNavDetails] = useAddNavDetailsMutation()
2024-11-14 16:04:20 +05:30
// const {
// data
2024-11-18 17:54:23 +05:30
// } = useGetArtifactsQuery(id)
2024-11-14 16:04:20 +05:30
const {
control,
handleSubmit,
watch,
reset,
formState: { errors },
} = useForm({
2024-11-18 17:54:23 +05:30
resolver: yupResolver(ioNav),
2024-11-14 16:04:20 +05:30
});
const onSubmit = async (data) => {
setIsLoading(true)
try {
2024-11-18 17:54:23 +05:30
const res = await addNavDetails({ data, id })
if (res?.data?.statusCode === 201) {
2024-11-14 16:04:20 +05:30
setIsLoading(false);
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
handleClose()
}else if(res?.error?.status === 400){
toast({
render: () => <ToastBox message={res?.error?.data?.message } status={"error"} />,
});
2024-11-18 17:54:23 +05:30
handleClose()
2024-11-14 16:04:20 +05:30
}
2024-11-18 17:54:23 +05:30
2024-11-14 16:04:20 +05:30
} catch (error) {
console.log(error);
2024-11-18 17:54:23 +05:30
2024-11-14 16:04:20 +05:30
}
};
const handleConfirm = () => {
handleSubmit(onSubmit)();
};
const handleSave = () => {
handleSubmit(onSubmit)();
};
const handleClose = () => {
2024-11-18 17:54:23 +05:30
setIsLoading(false);
2024-11-14 16:04:20 +05:30
setAlert(false)
onClose()
reset({
2024-11-18 17:54:23 +05:30
transactionDate:"",
transactionAmount:"",
comments:""
2024-11-14 16:04:20 +05:30
})
}
2024-11-18 17:54:23 +05:30
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));
2024-11-14 16:04:20 +05:30
return (
<>
<Drawer
size={"md"}
isOpen={isOpen}
placement="right"
initialFocusRef={firstField}
onClose={handleClose}
>
<DrawerOverlay />
<DrawerContent>
<DrawerCloseButton />
<DrawerHeader fontSize={"sm"}>IO Nav Details</DrawerHeader>
<DrawerBody>
<Stack spacing={4}>
2024-11-18 17:54:23 +05:30
<FormControl isInvalid={errors.transactionDate} isRequired>
2024-11-14 16:04:20 +05:30
<FormLabel fontSize={"sm"}>Date Selection</FormLabel>
<Controller
name="transactionDate"
control={control}
render={({ field }) => (
2024-11-18 17:54:23 +05:30
<Input {...field}
max={today} // Set max attribute to todays date
fontSize={"sm"} type="date" size={"sm"} />
2024-11-14 16:04:20 +05:30
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.transactionDate?.message}
</FormErrorMessage>
</FormControl>
<FormControl isInvalid={errors.transactionAmount} isRequired>
2024-11-18 17:54:23 +05:30
<FormLabel fontSize={"sm"}>New NAV</FormLabel>
2024-11-14 16:04:20 +05:30
<Controller
name="transactionAmount"
control={control}
render={({ field }) => (
<CurrencyInput {...field} textAlign={'right'} fontSize={"sm"} type="number" size={"sm"} />
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.transactionAmount?.message}
</FormErrorMessage>
</FormControl>
2024-11-18 17:54:23 +05:30
<HStack justify={'start'} gap={10} bg={'green.100'} p={3} rounded={'md'} shadow={'md'}>
<VStack align={'start'}>
<Text as={'span'} fontSize={'sm'} fontWeight={500}>Current nav</Text>
<Text as={'span'} fontSize={'sm'}>
{parseFloat(IODetails?.ioNAV || 0).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
</Text>
</VStack>
<VStack align={'start'}>
<Text as={'span'} fontSize={'sm'} fontWeight={500}>Live return %</Text>
<Text as={'span'} fontSize={'sm'}>{calculatePercentage(watch()?.transactionAmount||IODetails?.ioNAV,IODetails?.ioNAV)}</Text>
</VStack>
</HStack>
2024-11-14 16:04:20 +05:30
<FormControl isInvalid={errors.comments}>
2024-12-02 12:23:27 +05:30
<FormLabel fontSize={"sm"}>Comment</FormLabel>
2024-11-14 16:04:20 +05:30
<Controller
name="comments"
control={control}
render={({ field }) => (
2024-12-02 12:23:27 +05:30
<Textarea {...field} maxLength={200} fontSize={"sm"} type="text" size={"sm"} />
2024-11-14 16:04:20 +05:30
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.comments?.message}
</FormErrorMessage>
2024-12-02 12:23:27 +05:30
<FormHelperText fontSize="xs" color="gray.500">
<Box as="span" me={1}>Maximum length should be 200 characters. You have entered </Box>
{watch("comments")?.length || 0} characters.
</FormHelperText>
2024-11-14 16:04:20 +05:30
</FormControl>
</Stack>
</DrawerBody>
<DrawerFooter>
<Button
variant="outline"
colorScheme={"forestGreen"}
rounded={"sm"}
size={"sm"}
mr={3}
onClick={handleClose}
>
Cancel
</Button>
<Button
colorScheme={"forestGreen"}
rounded={"sm"}
size={"sm"}
onClick={() => setAlert(true)}
>
Save
</Button>
</DrawerFooter>
</DrawerContent>
</Drawer>
<CustomAlertDialog
isOpen={alert}
onClose={() => setAlert(false)}
alertHandler={handleSave}
2024-11-18 17:54:23 +05:30
message={"Are you sure you want to add NAV details?"}
2024-11-14 16:04:20 +05:30
isLoading={isLoading}
/>
</>
);
};
2024-11-18 17:54:23 +05:30
export default AddNavDetails;