2024-09-20 18:04:22 +05:30
|
|
|
import React, { useContext, useEffect, useState } from "react";
|
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
useToast,
|
|
|
|
|
} from "@chakra-ui/react";
|
|
|
|
|
import { useForm} from "react-hook-form";
|
|
|
|
|
import { yupResolver } from "@hookform/resolvers/yup";
|
|
|
|
|
import * as yup from "yup";
|
|
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
|
import { OPACITY_ON_LOAD } from "../../Layout/animations";
|
|
|
|
|
import FormInputMain from "../../Components/FormInputMain";
|
|
|
|
|
import {
|
|
|
|
|
useGetContactQuery,
|
|
|
|
|
useUpdateContactMutation,
|
|
|
|
|
} from "../../Services/contact.service";
|
|
|
|
|
import FullscreenLoaders from "../../Components/Loaders/FullscreenLoaders";
|
|
|
|
|
import ToastBox from "../../Components/ToastBox";
|
|
|
|
|
|
|
|
|
|
export const notification = yup.object().shape({
|
|
|
|
|
investmentNameEnglish: yup
|
|
|
|
|
.string()
|
|
|
|
|
.required("Investment Name is required"),
|
|
|
|
|
ManualDate: yup
|
|
|
|
|
.date()
|
|
|
|
|
.required("Manual Date is required")
|
|
|
|
|
.typeError("Invalid date format"),
|
|
|
|
|
ManualTime: yup
|
|
|
|
|
.string()
|
|
|
|
|
.required("Manual Time is required")
|
|
|
|
|
.matches(
|
|
|
|
|
/^([01]\d|2[0-3]):?([0-5]\d)$/,
|
|
|
|
|
"Invalid time format, must be in HH:mm"
|
|
|
|
|
),
|
|
|
|
|
expectedReturn: yup
|
|
|
|
|
.string()
|
|
|
|
|
.required("Expected Return is required")
|
|
|
|
|
.matches(
|
|
|
|
|
/^[0-9]+(\.[0-9]{1,2})?$/,
|
|
|
|
|
"Expected Return must be a valid number with up to 2 decimal places"
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-24 20:02:01 +05:30
|
|
|
const Notification = () => {
|
2024-09-20 18:04:22 +05:30
|
|
|
const toast = useToast();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const [form, setForm] = useState({});
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
control,
|
|
|
|
|
reset,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
} = useForm({
|
|
|
|
|
resolver: yupResolver(notification),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(errors);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: contact,
|
|
|
|
|
isLoading: contactLoading,
|
|
|
|
|
error,
|
|
|
|
|
} = useGetContactQuery();
|
|
|
|
|
const [updateContact] = useUpdateContactMutation();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (contact) {
|
|
|
|
|
reset({
|
|
|
|
|
phoneNumber: contact?.data[0]?.phoneNumber,
|
|
|
|
|
emailAddress: contact?.data[0]?.emailAddress,
|
|
|
|
|
websiteUrl: contact?.data[0]?.websiteUrl,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [contact, reset]);
|
|
|
|
|
|
|
|
|
|
if (contactLoading) {
|
|
|
|
|
return <FullscreenLoaders />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const formFields = [
|
|
|
|
|
{
|
|
|
|
|
label: "Investment Name",
|
|
|
|
|
placeHolder: " ",
|
|
|
|
|
name: "investmentNameEnglish",
|
|
|
|
|
type: "text",
|
|
|
|
|
isRequired: true,
|
|
|
|
|
section: "Add Details",
|
|
|
|
|
// value: contact?.phoneNumber || "",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Manual Date",
|
|
|
|
|
name: "ManualDate",
|
|
|
|
|
placeHolder: " ",
|
|
|
|
|
type: "date",
|
|
|
|
|
isRequired: true,
|
|
|
|
|
section: "Add Details",
|
|
|
|
|
// value: contact?.emailAddress || "",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Manual Time",
|
|
|
|
|
name: "ManualTime",
|
|
|
|
|
placeHolder: " ",
|
|
|
|
|
type: "time",
|
|
|
|
|
isRequired: true,
|
|
|
|
|
section: "Add Details",
|
|
|
|
|
// value: contact?.websiteUrl || "",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Expected Return",
|
|
|
|
|
name: "expectedReturn",
|
|
|
|
|
placeHolder: " ",
|
|
|
|
|
type: "text",
|
|
|
|
|
isRequired: true,
|
|
|
|
|
section: "Add Details",
|
|
|
|
|
// value: contact?.websiteUrl || "",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const groupedFields = formFields.reduce((groups, field) => {
|
|
|
|
|
const { section } = field;
|
|
|
|
|
if (!groups[section]) {
|
|
|
|
|
groups[section] = [];
|
|
|
|
|
}
|
|
|
|
|
groups[section].push(field);
|
|
|
|
|
return groups;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (data) => {
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const res = await updateContact(data);
|
|
|
|
|
if (res?.data?.statusCode === 200) {
|
|
|
|
|
toast({
|
|
|
|
|
render: () => <ToastBox message={res?.data?.message} />,
|
|
|
|
|
});
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-24 20:02:01 +05:30
|
|
|
return (
|
2024-09-20 18:04:22 +05:30
|
|
|
<Box {...OPACITY_ON_LOAD} overflowY={"scroll"} height={"100vh"} pb={14}>
|
|
|
|
|
<FormInputMain
|
|
|
|
|
groupedFields={groupedFields}
|
|
|
|
|
control={control}
|
|
|
|
|
errors={errors}
|
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
|
btnLoading={isLoading}
|
|
|
|
|
/>
|
2024-06-24 20:02:01 +05:30
|
|
|
</Box>
|
2024-09-20 18:04:22 +05:30
|
|
|
);
|
|
|
|
|
};
|
2024-06-24 20:02:01 +05:30
|
|
|
|
2024-09-20 18:04:22 +05:30
|
|
|
export default Notification;
|