import React, { useContext, useEffect, useState } from 'react'; import { Box, Button, FormControl, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Text, useToast, } from "@chakra-ui/react"; import { useForm } from 'react-hook-form'; import * as yup from 'yup'; import { yupResolver } from '@hookform/resolvers/yup'; import GlobalStateContext from '../../../../Contexts/GlobalStateContext'; import { useParams } from 'react-router-dom'; import { useAmountIvestmentMutation } from '../../../../Services/io.service'; import ToastBox from '../../../../Components/ToastBox'; // Validation schema const validationSchema = yup.object().shape({ transactionDate: yup.date().required('Date is required'), Total_Amount: yup.number().required('Amount is required'), amountInvested: yup.number().required('Amount to invest is required'), IoCash: yup.number().positive('IO Cash must be positive').required('IO Cash is required'), }); // Function to format currency const formatCurrency = (value) => { if (isNaN(value)) return ''; const formatted = parseFloat(value).toFixed(2).toString(); const [integer, decimal] = formatted.split('.'); const formattedInteger = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ','); return decimal ? `${formattedInteger}.${decimal}` : formattedInteger; }; const AmountInvested = ({ isOpen, onClose }) => { const params = useParams(); const toast = useToast(); const id = params?.id; const { control, register, handleSubmit, reset, watch, formState: { errors } } = useForm({ resolver: yupResolver(validationSchema), }); const [isLoading, setIsLoading] = useState(false); const { IODetails } = useContext(GlobalStateContext); const [amountInvested] = useAmountIvestmentMutation(); useEffect(() => { if (IODetails?.goalAmount) { const totalAmount = parseFloat(IODetails.goalAmount); const ioCashUpdate = parseFloat(IODetails.goalAmount) reset({ Total_Amount: totalAmount, IoCash: ioCashUpdate, }); } }, [IODetails, reset]); const onSubmit = async (data) => { console.log(data); setIsLoading(true); try { const res = await amountInvested({ data, id }); console.log(res); if (res?.data?.statusCode === 200) { toast({ render: () => , }); setIsLoading(false); onClose(); } else if (res?.error?.status === 400) { toast({ render: () => ( ), }); setIsLoading(false); } } catch (error) { setIsLoading(false); } }; const handleAmountChange = (e) => { const amount = parseFloat(e.target.value) || 0; const totalAmount = parseFloat(IODetails?.goalAmount) || 0; const ioCash = (totalAmount - amount).toFixed(2); reset({ amountInvested: parseFloat(amount), IoCash: parseFloat(ioCash), Total_Amount: IODetails?.goalAmount }); }; return ( Amount Invested
Date {errors.transactionDate && {errors.transactionDate.message}} Amount {errors.Total_Amount && {errors.Total_Amount.message}} Amount to invest {errors.amountInvested && {errors.amountInvested.message}} IO Cash {errors.IoCash && {errors.IoCash.message}}
); }; export default AmountInvested;