import React, { useEffect, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import {
useGetCommunityBannerByIdQuery,
useGetCommunityByIdQuery,
useUpdateCommunityBannerMutation,
useUpdateCommunityMutation,
} from "../../Services/api.service";
import { editCommunityBannerSchema, 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,
Text,
} 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/ultp-fallback-img.webp";
import Header from "../../Components/Header";
import CommunityBannerCard from "../../Pages/Community/CommunityBannerCard";
import BannerMainCard from "./BannerMainCard";
import ToastBox from "../ToastBox";
import { IMAGE_URI } from "../../Constants/Paginations";
const BannerEdit = ({isLoading, data, updateBanner, navigateTo, refetch, center}) => {
const { id } = useParams();
const toast = useToast();
const navigate = useNavigate();
const [isLoadingEdit, setIsLoadingEdit] = useState(false);
const [selectedImage, setSelectedImage] = useState();
const [largeImageData, setLargeImageData] = useState(null);
const [title, setTitle] = useState("");
const handleTitleChange = (e) => {
setTitle(e.target.value);
};
const {
register,
handleSubmit,
reset,
formState: { errors },
setValue,
watch
} = useForm({
resolver: yupResolver(editCommunityBannerSchema),
defaultValues: {
heading: data?.data?.Heading,
sub_heading: data?.data?.sub_heading,
CTO_button_title: data?.data?.CTO_button_title,
CTO_button_link: data?.data?.CTO_button_link,
},
});
// Watch form values to update preview
const formData = watch();
useEffect(() => {
if (data?.data) {
setSelectedImage(
`${IMAGE_URI}/${data?.data?.banner_image}`
);
setValue("heading", data?.data?.Heading);
setValue("sub_heading", data?.data?.sub_heading);
setValue("CTO_button_title", data?.data?.CTO_button_title);
setValue("CTO_button_link", data?.data?.CTO_button_link);
setValue("banner_image", data?.data?.banner_image);
watch()
}
}, [data, setValue]);
// useEffect(() => {
// const subscription = watch((value) => {setFormData(value)});
// return () => subscription.unsubscribe();
// }, [watch]);
const onSubmit = async (formData) => {
setIsLoadingEdit(true);
const form = new FormData();
form.append("heading", formData.heading);
form.append("sub_heading", formData.sub_heading);
form.append("CTO_button_title", formData.CTO_button_title);
form.append("CTO_button_link", formData.CTO_button_link);
if (formData.banner_image[0]) {
form.append("banner_image", formData.banner_image[0]);
}
if (formData?.banner_image === data?.data?.banner_image) {
form.delete("banner_image");
}
const mutationResult = await updateBanner({ id: id, data: form })
.then((response) => {
if (response?.data?.statusCode === 200) {
setIsLoadingEdit(false);
toast({
render: () => (
),
});
refetch()
navigate(navigateTo);
// setDeleteAlert(false);
}
})
.catch((error) => {
// // console.error("Error creating community:", error);
setIsLoadingEdit(false);
// setDeleteIsLoading(false);
// setDeleteAlert(false);
});
reset();
};
const handleImageChange = (e) => {
const file = e.target.files[0];
setLargeImageData(file);
if (file) {
const reader = new FileReader();
reader.onloadend = () => {
setSelectedImage(reader.result);
};
reader.readAsDataURL(file);
}
};
return isLoading ? (
) : (
Display Info
Select the platform for which you need to create this campaign.
Display banner
Below is the profile that will be displayed on the community page.
{/* */}
{selectedImage === fallbackImage || largeImageData === null ? (
""
) : (
{largeImageData?.name}
{(largeImageData?.size / (1024 * 1024)).toFixed(2)} mb
)}
);
};
export default BannerEdit;