contact updted

This commit is contained in:
2024-08-05 20:10:07 +05:30
parent d42cdf0a50
commit 059275b310
2 changed files with 49 additions and 18 deletions

View File

@@ -10,6 +10,7 @@ import {
Textarea,
Button,
Text,
useToast,
} from "@chakra-ui/react";
import { useForm, Controller } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
@@ -19,8 +20,9 @@ import { v4 as uuidv4 } from "uuid";
import GlobalStateContext from "../../Contexts/GlobalStateContext";
import { OPACITY_ON_LOAD } from "../../Layout/animations";
import FormInputMain from "../../Components/FormInputMain";
import { useGetContactQuery } from "../../Services/contact.service";
import { useGetContactQuery, useUpdateContactMutation } from "../../Services/contact.service";
import FullscreenLoaders from "../../Components/Loaders/FullscreenLoaders";
import ToastBox from "../../Components/ToastBox";
export const addSponser = yup.object().shape({
phoneNumber: yup.string().required("Phone Number is required"),
@@ -37,8 +39,10 @@ export function debounce(func, delay) {
}
const Contact = () => {
const toast = useToast()
const navigate = useNavigate();
const [form, setForm] = useState({});
const [ isLoading, setIsLoading ] = useState(false)
// const { sponser, setSponser } = useContext(GlobalStateContext);
const {
@@ -50,22 +54,21 @@ const Contact = () => {
resolver: yupResolver(addSponser),
});
console.log(errors);
const {
data: contact,
isLoading: contactLoading,
error,
} = useGetContactQuery({ page: 1, size: 10 });
} = useGetContactQuery();
const [ updateContact ] = useUpdateContactMutation()
console.log(contact?.data);
useEffect(() => {
if (contact) {
reset({
phoneNumber: contact.phoneNumber,
emailAddress: contact.emailAddress,
websiteUrl: contact.websiteUrl,
phoneNumber: contact?.data[0]?.phoneNumber,
emailAddress: contact?.data[0]?.emailAddress,
websiteUrl: contact?.data[0]?.websiteUrl,
});
}
}, [contact, reset]);
@@ -82,7 +85,7 @@ const Contact = () => {
type: "text",
isRequired: true,
section: "Add Details",
defaultValue: contact?.phoneNumber || "",
// value: contact?.phoneNumber || "",
},
{
label: "E-mail ID",
@@ -91,7 +94,7 @@ const Contact = () => {
type: "text",
isRequired: true,
section: "Add Details",
defaultValue: contact?.emailAddress || "",
// value: contact?.emailAddress || "",
},
{
label: "Website URL",
@@ -100,7 +103,7 @@ const Contact = () => {
type: "text",
isRequired: true,
section: "Add Details",
defaultValue: contact?.websiteUrl || "",
// value: contact?.websiteUrl || "",
},
];
@@ -113,11 +116,26 @@ const Contact = () => {
return groups;
}, {});
const onSubmit = (data) => {
if (!Object.keys(errors).length) {
setForm(data);
setAlert(true);
const onSubmit = async (data) => {
setIsLoading(true)
try {
const res = await updateContact(data)
console.log(res?.data?.statusCode);
if (res?.data?.statusCode === 200) {
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
setIsLoading(false)
}
} catch (error) {
console.log(error);
setIsLoading(false)
}
};
return (
@@ -127,6 +145,7 @@ const Contact = () => {
control={control}
errors={errors}
onSubmit={handleSubmit(onSubmit)}
btnLoading={isLoading}
/>
</Box>
);

View File

@@ -8,17 +8,29 @@ const baseUrl = api?.defaults.baseURL;
export const contact = createApi({
reducerPath: "contact",
baseQuery: fetchBaseQuery({ baseUrl }),
tagTypes: [],
tagTypes: ["getContact"],
endpoints: (builder) => ({
getContact: builder.query({
query: ({ page, size }) =>
`/contactDetails/admin/?page=${page}&size=${size}`,
query: () =>
`/contactDetails/admin`,
providesTags: ["getContact"],
}),
// ========[Update Investment]=======
updateContact: builder.mutation({
query: (data) => ({
url: `/contactDetails/admin/`,
method: "PATCH",
body: data,
}),
invalidatesTags: ["getContact"],
}),
}),
});
// Export hooks for usage in functional components
export const { useGetContactQuery } = contact;
export const { useGetContactQuery, useUpdateContactMutation } = contact;