import { useEffect, useState } from "react"; import Button from "@material-ui/core/Button"; import { Link } from "react-router-dom"; import ReactQuill from "react-quill"; import "react-quill/dist/quill.snow.css"; import axios from "axios"; import { isAutheticated } from "src/auth"; const TOOLBAR_OPTIONS = [ [{ header: [1, 2, 3, 4, 5, 6, false] }], [{ font: [] }], [{ list: "ordered" }, { list: "bullet" }], ["bold", "italic", "underline", "strike"], [{ color: [] }, { background: [] }], [{ align: [] }], [{ script: "super" }, { script: "sub" }], ["undo", "redo"], ]; import CloudUploadIcon from "@mui/icons-material/CloudUpload"; import DeleteSharpIcon from "@mui/icons-material/DeleteSharp"; import { Box, TextField, Checkbox, FormControlLabel } from "@mui/material"; const Editpanel4 = () => { const token = isAutheticated(); const [loading, setLoading] = useState(false); const [displayPanel, setDisplayPanel] = useState(false); const [content, setContent] = useState(""); const [olderContent, setOlderContent] = useState(""); const [image, setimage] = useState(""); const [title, setTitle] = useState(""); const [error, setError] = useState(""); const [newUpdatedImages, setNewUpdatedImages] = useState(null); const [Img, setImg] = useState(true); const [id, setId] = useState(null); const handleContentChange = (content, delta, source, editor) => { setContent(editor.getHTML()); }; //get Blogdata const getPanel = async () => { try { const res = await axios.get(`/api/panel/panel4/get`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }); if (res?.status === 200) { setTitle(res?.data?.panel4[0]?.title); if(res?.data?.panel4[0]?.image!==undefined && res?.data?.panel4[0]?.image!==null){ setimage(res?.data?.panel4[0]?.image); } setContent(res?.data?.panel4[0]?.content); setOlderContent(res?.data?.panel4[0]?.content); setDisplayPanel(res?.data?.panel4[0]?.displayPanel); setId(res?.data?.panel4[0]?._id); setImg(false); } } catch (err) { console.error(err) swal({ title: "Error", text: "Unable to fetch the panel content", icon: "error", button: "Retry", dangerMode: true, }); } }; useEffect(() => { getPanel(); }, []); const handleFileChange = (e) => { const files = e.target.files; // Reset error state setError(""); // Check if more than one image is selected if (files.length > 1 || newUpdatedImages !== null) { setError("You can only upload one image."); return; } // Check file types and append to selectedFiles const allowedTypes = ["image/jpeg", "image/png", "image/jpg"]; const file = files[0]; // Only one file is selected, so we get the first one if (allowedTypes.includes(file.type)) { setNewUpdatedImages(file); setimage(file); } else { setError("Please upload only PNG, JPEG, or JPG files."); } }; const handelDelete = async (public_id) => { const ary = public_id.split("/"); setNewUpdatedImages(null); const res = await axios.delete( `/api/v1/blog/deleteImage/jatinMor/Blog/${ary[2]}`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, } ); if (res) { setimage(null); setImg(true); } }; const addContent = async () => { const formData = new FormData(); formData.append("title", title); formData.append("content", content); formData.append("displayPanel", displayPanel); formData.append("image", image); const response = await axios.post( "/api/panel/panel4/add", formData, { headers: { Authorization: `Bearer ${token}`, }, } ); if (response.status == 201) { swal({ title: "Congratulations!!", text: "Panel 4 added successfully!", icon: "success", button: "OK", }); } }; const updateContent = () => { if (title === "" || content === "") { swal({ title: "Warning", text: "Fill all mandatory fields", icon: "error", button: "Close", dangerMode: true, }); return; } setLoading(true); const formData = new FormData(); formData.append("title", title); formData.append("content", content); formData.append("displayPanel", displayPanel); if (newUpdatedImages !== null) { formData.append("image", newUpdatedImages); } axios .patch(`/api/panel/panel4/update/${id}`, formData, { headers: { Authorization: `Bearer ${token}`, "Content-Type": "multipart/form-data", "Access-Control-Allow-Origin": "*", }, }) .then((res) => { swal({ title: "Updated", text: " Updated successfully!", icon: "success", button: "ok", }); setLoading(false); }) .catch((err) => { setLoading(false); const message = err.response?.data?.message ? err.response?.data?.message : "Something went wrong!"; swal({ title: "Warning", text: message, icon: "error", button: "Retry", dangerMode: true, }); }); }; const handleSaveClick = async () => { if (!olderContent) { addContent(); } else { updateContent(); } // // Reload terms and conditions // await getPrivacyPolicy(); }; const handleChange = (event) => { setDisplayPanel(event.target.checked); }; return (
Panel 4

} label="Display Panel" />
setTitle(e.target.value)} />
{error &&

{error}

}
*You cannot upload more than 1 image
{image && ( Panel Image handelDelete(image?.public_id)} fontSize="small" sx={{ color: "white", position: "absolute", cursor: "pointer", padding: "0.2rem", background: "black", borderRadius: "50%", }} /> )}
); }; export default Editpanel4;