import React, { useState, useEffect, useCallback, useRef } from "react"; import axios from "axios"; import { Link } from "react-router-dom"; import { isAutheticated } from "src/auth"; import { toast } from "react-hot-toast"; import { Button, Box, IconButton, Modal, TextField, Typography, } from "@mui/material"; import CloseIcon from "@mui/icons-material/Close"; import { ClipLoader } from "react-spinners"; import swal from "sweetalert"; import { useNavigate } from "react-router-dom"; import debounce from "lodash/debounce"; const style = { position: "absolute", top: "50%", left: "50%", transform: "translate(-50%, -50%)", width: 500, bgcolor: "background.paper", borderRadius: "0.5rem", boxShadow: 24, }; const ProductManual = () => { const token = isAutheticated(); const navigate = useNavigate(); const [loading, setLoading] = useState(true); const [updating, setUpdating] = useState(true); const [saveLoding, setSaveLoading] = useState(true); const [edit, setEdit] = useState(false); const [title, setTitle] = useState(""); const [pdfFile, setPdfFile] = useState(null); const [itemPerPage, setItemPerPage] = useState(10); const [currentPage, setCurrentPage] = useState(1); const [open, setOpen] = useState(false); const [currentManual, setCurrentManual] = useState(null); const [productManuals, setProductManuals] = useState([]); const [fileError, setFileError] = useState(""); const [totalData, setTotalData] = useState(0); const [success, setSuccess] = useState(true); const titleRef = useRef(); // const handleOpen = (manual = null) => { // setOpen(true); // setCurrentManual(manual); // if (manual) { // setTitle(manual.title); // setPdfFile(null); // setEdit(true); // } else { // setTitle(""); // setPdfFile(null); // setEdit(false); // } // }; const handleOpen = () => setOpen(true); const handleClose = () => { setOpen(false); setEdit(false); setTitle(""); setPdfFile(null); setFileError(""); }; const handleEditClick = (manual) => { setOpen(true); setCurrentManual(manual); setTitle(manual.title); setPdfFile(null); setEdit(true); }; const getProductManuals = async () => { try { setLoading(true); const response = await axios.get(`/api/productmanual`, { headers: { Authorization: `Bearer ${token}` }, params: { page: currentPage, show: itemPerPage, title: titleRef.current?.value || "", }, }); if (response.status === 200) { const { productManuals, total } = response.data; setProductManuals(productManuals); setTotalData(total); setLoading(false); } } catch (error) { console.error("Failed to fetch product manuals:", error); setLoading(false); } }; useEffect(() => { getProductManuals(); }, [itemPerPage, currentPage, success]); const validateFile = (file) => { return file && file.type === "application/pdf"; }; const handleSave = async () => { if (!title || !pdfFile || !validateFile(pdfFile)) { setFileError("Please upload a valid PDF file!"); return; } setFileError(""); setSaveLoading(false); const formData = new FormData(); formData.append("title", title); formData.append("pdfFile", pdfFile); try { await axios.post("/api/productmanual/create", formData, { headers: { Authorization: `Bearer ${token}`, "Content-Type": "multipart/form-data", }, }); handleClose(); setSuccess((prev) => !prev); toast.success("New product manual added successfully!"); // getProductManuals(); } catch (error) { swal("Error", "Failed to add product manual", "error"); } finally { setSaveLoading(true); } }; const handleUpdate = async () => { if (!title) { setFileError("Please upload a valid PDF file!"); return; } setFileError(""); setUpdating(false); const formData = new FormData(); formData.append("title", title); formData.append("pdfFile", pdfFile); try { await axios.put( `/api/productmanual/update/${currentManual._id}`, formData, { headers: { Authorization: `Bearer ${token}` }, } ); handleClose(); setSuccess((prev) => !prev); toast.success("The product manual was updated successfully!"); // getProductManuals(); } catch (err) { swal("Error", "Failed to update product manual", "error"); } finally { setUpdating(true); } }; const handleDelete = async (_id) => { swal({ title: "Are you sure?", icon: "warning", buttons: { Yes: { text: "Yes", value: true }, Cancel: { text: "Cancel", value: "cancel" }, }, }).then(async (value) => { if (value === true) { try { await axios.delete(`/api/productmanual/delete/${_id}`, { headers: { Authorization: `Bearer ${token}` }, }); setSuccess((prev) => !prev); toast.success("The product manual was deleted successfully!"); // getProductManuals(); } catch (err) { swal("Error", "Failed to delete product manual", "error"); } } }); }; const debouncedSearch = useCallback( debounce(() => { setCurrentPage(1); getProductManuals(); }, 500), [] ); const handleSearchChange = (e) => { debouncedSearch(); }; return (
Title | File Name | Updated Date | Actions | ||
---|---|---|---|---|---|
Loading... | |||||
{manual.title} | {manual.product_manual.filename} | {new Date(manual.updatedAt).toLocaleString( "en-IN", { weekday: "short", month: "short", day: "numeric", year: "numeric", hour: "numeric", minute: "numeric", hour12: true, } )} | |||
No Product manual Available... |