2024-08-13 16:56:17 +05:30
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2024-07-08 20:14:34 +05:30
|
|
|
import {
|
2024-07-09 12:25:52 +05:30
|
|
|
Box,
|
2024-07-08 20:14:34 +05:30
|
|
|
Button,
|
2024-07-09 19:05:08 +05:30
|
|
|
FormControl,
|
|
|
|
|
FormLabel,
|
2024-07-09 12:25:52 +05:30
|
|
|
Input,
|
2024-07-08 20:14:34 +05:30
|
|
|
Modal,
|
|
|
|
|
ModalBody,
|
|
|
|
|
ModalCloseButton,
|
|
|
|
|
ModalContent,
|
|
|
|
|
ModalFooter,
|
|
|
|
|
ModalHeader,
|
|
|
|
|
ModalOverlay,
|
2024-07-09 12:25:52 +05:30
|
|
|
Text,
|
2024-08-13 16:56:17 +05:30
|
|
|
useToast,
|
2024-07-08 20:14:34 +05:30
|
|
|
} from "@chakra-ui/react";
|
2024-10-18 12:20:10 +05:30
|
|
|
import { Controller, useForm } from 'react-hook-form';
|
2024-08-13 16:56:17 +05:30
|
|
|
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';
|
2024-10-18 12:20:10 +05:30
|
|
|
import CurrencyInput from '../../../../Components/CurrencyInput';
|
2024-08-13 16:56:17 +05:30
|
|
|
|
|
|
|
|
// Validation schema
|
|
|
|
|
const validationSchema = yup.object().shape({
|
|
|
|
|
transactionDate: yup.date().required('Date is required'),
|
2024-08-14 12:19:27 +05:30
|
|
|
Total_Amount: yup.number().required('Amount is required'),
|
|
|
|
|
amountInvested: yup.number().required('Amount to invest is required'),
|
2024-08-13 16:56:17 +05:30
|
|
|
IoCash: yup.number().positive('IO Cash must be positive').required('IO Cash is required'),
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-14 12:19:27 +05:30
|
|
|
// 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;
|
|
|
|
|
};
|
2024-07-08 20:14:34 +05:30
|
|
|
|
|
|
|
|
const AmountInvested = ({ isOpen, onClose }) => {
|
2024-08-14 12:19:27 +05:30
|
|
|
const params = useParams();
|
|
|
|
|
const toast = useToast();
|
|
|
|
|
const id = params?.id;
|
|
|
|
|
const { control, register, handleSubmit, reset, watch, formState: { errors } } = useForm({
|
2024-08-13 16:56:17 +05:30
|
|
|
resolver: yupResolver(validationSchema),
|
|
|
|
|
});
|
2024-08-14 12:19:27 +05:30
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
2024-08-13 16:56:17 +05:30
|
|
|
const { IODetails } = useContext(GlobalStateContext);
|
2024-08-14 12:19:27 +05:30
|
|
|
const [amountInvested] = useAmountIvestmentMutation();
|
2024-08-22 12:10:07 +05:30
|
|
|
|
2024-08-13 16:56:17 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-08-16 20:24:25 +05:30
|
|
|
if (IODetails?.totalAmtInvestmentInUSD) {
|
|
|
|
|
const totalAmount = parseFloat(IODetails.totalAmtInvestmentInUSD);
|
|
|
|
|
const ioCashUpdate = parseFloat(IODetails.totalAmtInvestmentInUSD)
|
2024-08-14 12:19:27 +05:30
|
|
|
reset({
|
|
|
|
|
Total_Amount: totalAmount,
|
|
|
|
|
IoCash: ioCashUpdate,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [IODetails, reset]);
|
2024-08-13 16:56:17 +05:30
|
|
|
|
|
|
|
|
const onSubmit = async (data) => {
|
|
|
|
|
console.log(data);
|
2024-08-14 12:19:27 +05:30
|
|
|
setIsLoading(true);
|
2024-08-13 16:56:17 +05:30
|
|
|
|
|
|
|
|
try {
|
2024-08-14 12:19:27 +05:30
|
|
|
const res = await amountInvested({ data, id });
|
2024-08-13 16:56:17 +05:30
|
|
|
console.log(res);
|
2024-08-14 12:19:27 +05:30
|
|
|
if (res?.data?.statusCode === 200) {
|
2024-08-13 16:56:17 +05:30
|
|
|
toast({
|
|
|
|
|
render: () => <ToastBox message={res?.data?.message} />,
|
|
|
|
|
});
|
2024-08-14 12:19:27 +05:30
|
|
|
setIsLoading(false);
|
|
|
|
|
onClose();
|
|
|
|
|
} else if (res?.error?.status === 400) {
|
2024-08-13 16:56:17 +05:30
|
|
|
toast({
|
|
|
|
|
render: () => (
|
2024-08-14 12:19:27 +05:30
|
|
|
<ToastBox message={res?.error?.data?.message} status={"error"} />
|
2024-08-13 16:56:17 +05:30
|
|
|
),
|
|
|
|
|
});
|
2024-08-14 12:19:27 +05:30
|
|
|
setIsLoading(false);
|
2024-08-13 16:56:17 +05:30
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2024-08-14 12:19:27 +05:30
|
|
|
setIsLoading(false);
|
2024-08-13 16:56:17 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleAmountChange = (e) => {
|
2024-10-22 20:11:00 +05:30
|
|
|
// e might be an object or just a value, handle both cases
|
|
|
|
|
const amount = typeof e === 'object' && e.target ? parseFloat(e.target.value) || 0 : parseFloat(e) || 0;
|
2024-08-16 20:24:25 +05:30
|
|
|
const totalAmount = parseFloat(IODetails?.totalAmtInvestmentInUSD) || 0;
|
2024-08-14 12:19:27 +05:30
|
|
|
const ioCash = (totalAmount - amount).toFixed(2);
|
2024-10-22 20:11:00 +05:30
|
|
|
|
2024-08-13 16:56:17 +05:30
|
|
|
reset({
|
2024-08-14 12:19:27 +05:30
|
|
|
amountInvested: parseFloat(amount),
|
|
|
|
|
IoCash: parseFloat(ioCash),
|
2024-10-22 20:11:00 +05:30
|
|
|
Total_Amount: IODetails?.totalAmtInvestmentInUSD,
|
2024-08-13 16:56:17 +05:30
|
|
|
});
|
|
|
|
|
};
|
2024-10-22 20:11:00 +05:30
|
|
|
|
2024-08-13 16:56:17 +05:30
|
|
|
|
2024-07-08 20:14:34 +05:30
|
|
|
return (
|
|
|
|
|
<Modal isOpen={isOpen} onClose={onClose}>
|
|
|
|
|
<ModalOverlay />
|
|
|
|
|
<ModalContent>
|
2024-07-09 19:05:08 +05:30
|
|
|
<ModalHeader fontSize={'md'}>Amount Invested</ModalHeader>
|
2024-07-08 20:14:34 +05:30
|
|
|
<ModalCloseButton />
|
|
|
|
|
<ModalBody>
|
2024-08-13 16:56:17 +05:30
|
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
|
<FormControl mb={"15px"} isInvalid={!!errors.transactionDate} isRequired>
|
|
|
|
|
<FormLabel as={"label"} fontSize={"sm"} fontWeight={500}>
|
|
|
|
|
Date
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<Input
|
|
|
|
|
type="date"
|
|
|
|
|
{...register('transactionDate')}
|
|
|
|
|
size="sm"
|
|
|
|
|
rounded={'sm'}
|
|
|
|
|
fontSize={"sm"}
|
|
|
|
|
focusBorderColor="forestGreen.300"
|
|
|
|
|
/>
|
2024-08-23 12:24:34 +05:30
|
|
|
{errors.transactionDate && <Text fontSize={'xs'} fontWeight={600} color="red.500">{errors.transactionDate.message}</Text>}
|
2024-08-13 16:56:17 +05:30
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<FormControl mb={"15px"} isInvalid={!!errors.Total_Amount} isReadOnly>
|
|
|
|
|
<FormLabel as={"label"} fontSize={"sm"} fontWeight={500}>Amount</FormLabel>
|
|
|
|
|
<Input
|
2024-08-14 12:19:27 +05:30
|
|
|
type="text"
|
|
|
|
|
value={formatCurrency(watch('Total_Amount'))}
|
2024-08-13 16:56:17 +05:30
|
|
|
size="sm"
|
|
|
|
|
rounded={'sm'}
|
|
|
|
|
textAlign={'end'}
|
|
|
|
|
focusBorderColor="forestGreen.300"
|
|
|
|
|
fontSize={"sm"}
|
|
|
|
|
readOnly
|
|
|
|
|
/>
|
2024-08-23 12:24:34 +05:30
|
|
|
{errors.Total_Amount && <Text fontSize={'xs'} fontWeight={600} color="red.500">{errors.Total_Amount.message}</Text>}
|
2024-08-13 16:56:17 +05:30
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<FormControl mb={"15px"} isInvalid={!!errors.amountInvested} isRequired>
|
|
|
|
|
<FormLabel as={"label"} fontSize={"sm"} fontWeight={500}>Amount to invest</FormLabel>
|
2024-10-18 12:20:10 +05:30
|
|
|
{/* <Input
|
2024-08-13 16:56:17 +05:30
|
|
|
type="number"
|
|
|
|
|
{...register('amountInvested')}
|
|
|
|
|
size="sm"
|
|
|
|
|
rounded={'sm'}
|
|
|
|
|
textAlign={'end'}
|
|
|
|
|
focusBorderColor="forestGreen.300"
|
|
|
|
|
fontSize={"sm"}
|
|
|
|
|
onChange={handleAmountChange}
|
2024-10-18 12:20:10 +05:30
|
|
|
/> */}
|
|
|
|
|
<Controller
|
2024-10-22 20:11:00 +05:30
|
|
|
name="amountInvested"
|
|
|
|
|
control={control}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<CurrencyInput
|
|
|
|
|
{...field}
|
|
|
|
|
textAlign={'right'}
|
|
|
|
|
fontSize={"sm"}
|
|
|
|
|
type="number"
|
|
|
|
|
size={"sm"}
|
|
|
|
|
onChange={(value) => {
|
|
|
|
|
field.onChange(value); // This will keep the form's internal state updated
|
|
|
|
|
handleAmountChange(value); // This will trigger your custom logic
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2024-08-23 12:24:34 +05:30
|
|
|
{errors.amountInvested && <Text fontSize={'xs'} fontWeight={600} color="red.500">{errors.amountInvested.message}</Text>}
|
2024-08-13 16:56:17 +05:30
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<FormControl mb={"15px"} isInvalid={!!errors.IoCash}>
|
|
|
|
|
<FormLabel as={"label"} fontSize={"sm"} fontWeight={500}>
|
|
|
|
|
IO Cash
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<Input
|
2024-08-14 12:19:27 +05:30
|
|
|
type="text"
|
|
|
|
|
value={formatCurrency(watch('IoCash'))}
|
2024-08-13 16:56:17 +05:30
|
|
|
size="sm"
|
|
|
|
|
rounded={'sm'}
|
|
|
|
|
focusBorderColor="forestGreen.300"
|
|
|
|
|
fontSize={"sm"}
|
|
|
|
|
textAlign={'right'}
|
|
|
|
|
readOnly
|
|
|
|
|
/>
|
2024-08-23 12:24:34 +05:30
|
|
|
{errors.IoCash && <Text fontSize={'xs'} fontWeight={600} color="red.500">{errors.IoCash.message}</Text>}
|
2024-08-13 16:56:17 +05:30
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<ModalFooter>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
bg={"hsla(139, 100%, 14%, 1)"}
|
|
|
|
|
mr={3}
|
|
|
|
|
color={"#fff"}
|
|
|
|
|
_hover={{
|
|
|
|
|
bg: "hsl(139deg 98.99% 26.59%)",
|
|
|
|
|
}}
|
|
|
|
|
size={'sm'}
|
|
|
|
|
rounded={"sm"}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size={'sm'}
|
|
|
|
|
rounded={"sm"} mr={3} onClick={onClose}>
|
|
|
|
|
Close
|
|
|
|
|
</Button>
|
|
|
|
|
</ModalFooter>
|
|
|
|
|
</form>
|
2024-07-08 20:14:34 +05:30
|
|
|
</ModalBody>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default AmountInvested;
|