Files
rubix-admin-panel/src/Components/Banner/AddBanner.jsx

377 lines
12 KiB
React
Raw Normal View History

2024-05-07 12:47:06 +05:30
import {
2024-05-10 00:37:57 +05:30
Box,
FormControl,
FormHelperText,
FormLabel,
Input,
Stack,
Textarea,
Heading,
Button,
useToast,
Divider,
Image,
} from "@chakra-ui/react";
import React, { useState } from "react";
import fallbackImage from "../../assets/ultp-fallback-img.webp";
import { TiWarning } from "react-icons/ti";
2024-05-07 12:47:06 +05:30
2024-05-10 00:37:57 +05:30
import { motion } from "framer-motion";
import { OPACITY_ON_LOAD } from "../../Layout/animations";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm } from "react-hook-form";
import {
addCommunityBannerSchema,
addCommunitySchema,
} from "../../Validations/Validations";
import { useNavigate } from "react-router-dom";
import Loader01 from "../../Components/Loaders/Loader01";
import Header from "../Header";
2024-05-17 15:32:45 +05:30
import ToastBox from "../ToastBox";
2024-05-10 00:37:57 +05:30
const AddBanner = ({ createApi, navigateLink, title }) => {
const toast = useToast();
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(false);
const [selectedImage, setSelectedImage] = useState(fallbackImage);
const [largeImageData, setLargeImageData] = useState(null);
const {
register,
handleSubmit,
reset,
formState: { errors },
} = useForm({
resolver: yupResolver(addCommunityBannerSchema),
});
const onSubmit = async (data) => {
console.log(data);
try {
setIsLoading(true);
const formData = new FormData();
formData.append("heading", data.heading);
formData.append("sub_heading", data.sub_heading);
formData.append("CTO_button_link", data.CTO_button_link);
formData.append("CTO_button_title", data.CTO_button_title);
if (data.banner_image[0]) {
formData.append("banner_image", data.banner_image[0]);
2024-05-07 12:47:06 +05:30
}
2024-05-10 00:37:57 +05:30
// Trigger the mutation
createApi(formData)
.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) {
setIsLoading(false);
toast({
2024-05-17 15:32:45 +05:30
render: () => (
<ToastBox status={"success"} message={response?.data?.message} />
),
2024-05-10 00:37:57 +05:30
});
reset();
navigate(navigateLink);
} else if (response?.data?.statusCode === 500) {
setIsLoading(false);
toast({
2024-05-17 15:32:45 +05:30
render: () => (
<ToastBox status={"success"} message={response?.data?.message} />
),
2024-05-10 00:37:57 +05:30
});
}
})
.catch((error) => {
// Handle errors
console.error("Error creating community:", error);
setIsLoading(false);
// Handle error notification if needed
});
} catch (error) {
// Handle errors
console.error("Error creating community:", error);
setIsLoading(false);
}
};
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 (
<Box
{...OPACITY_ON_LOAD}
w={"100%"}
h={"100vh"}
className="overflow-auto "
display={"flex"}
flexDirection={"column"}
>
<Header title={title} />
<Box display={"flex"}>
2024-05-07 12:47:06 +05:30
<Box className="col-5 d-flex flex-column gap-2 pt-4">
<span className="web-text-large fw-bold rubix-text-dark">
Banner Info
</span>
<span className="web-text-medium text-secondary">
Select the platform for which you need to create this campaign.
</span>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<Divider />
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<span className="web-text-large fw-bold rubix-text-dark">
Banner Image
</span>
<span className="web-text-medium text-secondary mb-4">
Below is the profile that will be displayed on the community page.
</span>
2024-05-10 00:37:57 +05:30
<Box
boxSize="sm"
className="d-flex w-100 justify-content-center flex-column align-items-center gap-3"
>
<>
<Image
shadow={"md"}
rounded={8}
w={500}
h={240}
src={selectedImage}
alt="Selected Image"
/>
{selectedImage === fallbackImage || largeImageData === null ? (
""
) : (
<Box display={"flex"} flexDirection={"column"} w={"100%"}>
<span className="web-text-small">{largeImageData?.name}</span>
<span className="web-text-small text-secondary fst-italic">
{(largeImageData?.size / (1024 * 1024)).toFixed(2)} mb
</span>
</Box>
)}
</>
2024-05-07 12:47:06 +05:30
<Button
onClick={() => setSelectedImage(fallbackImage)}
backgroundColor="red.400"
color={"whitesmoke"}
transition={"0.5s"}
_hover={{
backgroundColor: "red.500",
}}
size="xs"
>
Remove
</Button>
</Box>
</Box>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<form
onSubmit={handleSubmit(onSubmit)}
className="col-7 pt-4 mb-3 overflow-auto p-4"
>
<FormControl isRequired className="mb-3">
<FormLabel className="web-text-large fw-bold rubix-text-dark">
Heading
</FormLabel>
<Input
{...register("heading")}
placeholder="Heading"
className="web-text-medium"
size="sm"
/>
{errors.name && (
<span className="text-danger web-text-small fw-bold ps-2 d-flex align-items-center gap-1 mt-1">
2024-05-10 00:37:57 +05:30
<TiWarning className="fw-bold fs-5 " /> {errors.heading.message}
2024-05-07 12:47:06 +05:30
</span>
)}
</FormControl>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<FormControl isRequired className="mb-3">
<FormLabel className="web-text-large fw-bold rubix-text-dark">
Sub Heading
</FormLabel>
<Input
{...register("sub_heading")}
placeholder="Sub Heading"
className="web-text-medium"
size="sm"
/>
{errors.sub_heading && (
<span className="text-danger web-text-small fw-bold ps-2 d-flex align-items-center gap-1 mt-1">
<TiWarning className="fw-bold fs-5 " />{" "}
{errors.sub_heading.message}
</span>
)}
</FormControl>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<FormControl isRequired className="mb-3">
<FormLabel className="web-text-large fw-bold rubix-text-dark">
Button title
</FormLabel>
<Input
{...register("CTO_button_title")}
placeholder="Button title"
className="web-text-medium"
size="sm"
maxLength={90}
/>
<FormHelperText className="web-text-small">
Maximum characters must be 100 characters.
</FormHelperText>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
{errors.CTO_button_title && (
<span className="text-danger web-text-small fw-bold ps-2 d-flex align-items-center gap-1 mt-1">
<TiWarning className="fw-bold fs-5 " />{" "}
{errors.CTO_button_title.message}
</span>
)}
</FormControl>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<FormControl isRequired className="mb-3">
<FormLabel className="web-text-large fw-bold rubix-text-dark">
2024-05-10 00:37:57 +05:30
Button link
2024-05-07 12:47:06 +05:30
</FormLabel>
<Input
{...register("CTO_button_link")}
placeholder="Button link"
className="web-text-medium"
size="sm"
/>
<FormHelperText className="web-text-small">
Please share proper linked in link here.
</FormHelperText>
{errors.CTO_button_link && (
<span className="text-danger web-text-small fw-bold ps-2 d-flex align-items-center gap-1 mt-1">
2024-05-10 00:37:57 +05:30
<TiWarning className="fw-bold fs-5 " />{" "}
{errors.CTO_button_link.message}
2024-05-07 12:47:06 +05:30
</span>
)}
</FormControl>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<FormControl isRequired className="mb-3">
<FormLabel className="web-text-large fw-bold rubix-text-dark">
Banner image
</FormLabel>
{/* <ImageDropBox /> */}
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<Box
borderColor="gray.300"
borderStyle="dashed"
borderWidth="2px"
rounded="md"
shadow="sm"
role="group"
transition="all 150ms ease-in-out"
_hover={{
shadow: "md",
}}
as={motion.div}
initial="rest"
animate="rest"
whileHover="hover"
height={"105px"}
className="pointer"
>
<Box position="relative" height="100%" width="100%">
<Box
position="absolute"
top="0"
left="0"
height="100%"
width="100%"
display="flex"
flexDirection="column"
>
<Stack
height="100%"
width="100%"
display="flex"
alignItems="center"
justify="center"
>
<span
className="d-flex flex-column align-items-center pointer"
spacing="1"
>
<Heading
fontSize="lg"
color="gray.700"
fontWeight="bold"
cursor={"pointer"}
>
Drop images here
</Heading>
<span
fontWeight="light"
className="web-text-large text-secondary text-center pointer"
>
or click to upload
</span>
</span>
</Stack>
</Box>
<Input
{...register("banner_image")}
type="file"
height="100%"
width="100%"
position="absolute"
top="0"
left="0"
opacity="0"
aria-hidden="true"
accept="image/*"
onChange={handleImageChange}
onDrop={handleImageChange}
// onDragEnter={startAnimation}
// onDragLeave={stopAnimation}
/>
</Box>
</Box>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
{errors.banner_image && (
<span className="text-danger web-text-small fw-bold ps-2 d-flex align-items-center gap-1 mt-1">
<TiWarning className="fw-bold fs-5 " />{" "}
{errors.banner_image.message}
</span>
)}
<FormHelperText className="web-text-small">
Maximum limit of image is 5mb.
</FormHelperText>
</FormControl>
2024-05-10 00:37:57 +05:30
2024-05-07 12:47:06 +05:30
<Box className=" d-flex justify-content-end mb-5">
<Button
isLoading={isLoading}
spinner={<Loader01 />}
2024-05-10 00:37:57 +05:30
color={"whitesmoke"}
2024-05-21 20:21:30 +05:30
backgroundColor={"purple.900"}
2024-05-07 12:47:06 +05:30
_hover={{
backgroundColor: "purple.800",
}}
type="submit"
2024-05-10 00:37:57 +05:30
rounded={"sm"}
2024-05-07 12:47:06 +05:30
size="sm"
>
Create
</Button>
</Box>
</form>
</Box>
2024-05-10 00:37:57 +05:30
</Box>
);
};
export default AddBanner;