import axios from "axios"; import React, { useCallback, useEffect, useState } from "react"; import { API } from "../../data"; import { isAutheticated } from "../../auth"; import ClipLoader from "react-spinners/ClipLoader"; //import Footer from "../../Footer"; import { Link, useParams } from "react-router-dom"; import { useHistory } from "react-router-dom"; import swal from 'sweetalert'; import { CButton, CCard, CCardBody, CCol, CContainer, CForm, CFormInput, CInputGroup, CInputGroupText, CRow, } from '@coreui/react' import CIcon from '@coreui/icons-react' import { cilLockLocked, cilUser } from '@coreui/icons' const AddProduct = () => { const token = isAutheticated(); let history = useHistory(); const { id } = useParams(); // console.log(id) const [image, setImage] = useState(""); const [name, setName] = useState(""); const [loading, setLoading] = useState(false); useEffect(async () => { const res = await axios.get(`/api/category/getOne/${id}`, { headers: { Authorization: `Bearer ${token}`, }, }); setName(res.data.category.name) }, [id]); const handleSubmit = async () => { if (!(name && image)) { return swal('Error!', 'All fields are required', 'error') } const myForm = new FormData(); myForm.set("name", name); myForm.set("image", image); setLoading({ loading: true }); // console.log(image) try { let res = await axios.put( `/api/category/update/${id}`, myForm, { headers: { "Content-Type": 'multipart/form-data', // Authorization: `Bearer ${token}`, }, } ); //console.log(res.data.data.name) if (res.data) { swal("success!", "Category Edited Successfully!", "success"); setLoading(false); history.goBack(); } } catch (error) { swal('Error!', "something went wrong", 'error') setLoading(false); } } const handleImage = (e) => { const files = e.target.files[0]; // console.log(files) setImage(files); }; // const onCancel = () => { history.goBack() }; return ( <>

Edit {name} Category

setName(e.target.value)} value={name} placeholder="Name" /> {/* */}
) } export default AddProduct