import React, { useEffect, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import {
useGetCommunityByIdQuery,
useUpdateCommunityMutation,
} from "../../Services/api.service";
import { addCommunitySchema, schemaEdit } from "../../Validations/Validations";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import {
Box,
Divider,
FormControl,
FormHelperText,
FormLabel,
Heading,
Image,
Input,
Stack,
Textarea,
Button,
Skeleton,
useToast,
Switch,
Tag,
} from "@chakra-ui/react";
import { TiWarning } from "react-icons/ti";
import { OPACITY_ON_LOAD } from "../../Layout/animations";
import { motion } from "framer-motion";
import Loader01 from "../../Components/Loaders/Loader01";
import FullscreenLoaders from "../../Components/Loaders/FullscreenLoaders";
import fallbackImage from "../../assets/fallBackImage.png";
import Header from "../../Components/Header";
import ToastBox from "../../Components/ToastBox";
const API_URL = import.meta.env.VITE_API_BASE_URL;
const ComunityEditPage = () => {
const { id } = useParams();
const toast = useToast();
const navigate = useNavigate();
const { data, error, isLoading } = useGetCommunityByIdQuery(id);
const [isLoadingEdit, setIsLoadingEdit] = useState(false);
const [selectedImage, setSelectedImage] = useState();
const [updateCommunity] = useUpdateCommunityMutation();
const {
register,
handleSubmit,
reset,
watch,
formState: { errors },
setValue,
} = useForm({
resolver: yupResolver(addCommunitySchema),
defaultValues: {
member_name: "",
designation: "",
description: "",
linkedin: "",
profile_image: null,
},
});
useEffect(() => {
if (data?.data) {
setSelectedImage(
`${API_URL}/${data?.data?.profile_image}`
);
setValue("member_name", data?.data?.member_name);
setValue("designation", data?.data?.designation);
setValue("description", data?.data?.description);
setValue("linkedin", data?.data?.linkedin);
setValue("profile_image", data?.data?.profile_image);
}
}, [data, setValue]);
const onSubmit = async (formData) => {
setIsLoadingEdit(true);
const form = new FormData();
form.append("member_name", formData.member_name);
form.append("designation", formData.designation);
form.append("description", formData.description);
form.append("linkedin", formData.linkedin);
if (formData.profile_image[0]) {
form.append("profile_image", formData.profile_image[0]);
}
if (formData?.profile_image === data?.data?.profile_image) {
form.delete("profile_image");
}
await updateCommunity({ id: id, data: form })
.then((response) => {
// Handle the response here
// // console.log("Mutation response:", response?.data?.statusCode);
// // console.log("Mutation response:", response?.data?.message);
if (response?.data?.statusCode === 200) {
setIsLoadingEdit(false);
toast({
render: () => (
),
});
navigate("/community");
// setDeleteAlert(false);
} else {
setIsLoading01(false);
toast({
render: () => (
),
});
}
})
.catch((error) => {
// // console.error("Error creating community:", error);
setIsLoadingEdit(false);
// setDeleteIsLoading(false);
// setDeleteAlert(false);
});
// Log the FormData entries
// for (const [key, value] of form.entries()) {
// // console.log(`${key}: ${value}`);
// }
reset();
};
const handleImageChange = (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onloadend = () => {
setSelectedImage(reader.result);
};
reader.readAsDataURL(file);
}
};
return isLoading ? (
) : (
Members Info
Select the platform for which you need to create this campaign.
Display profile
Below is the profile that will be displayed on the community page.
);
};
export default ComunityEditPage;