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 } from "react-router-dom"; import { useHistory } from "react-router-dom"; import swal from 'sweetalert'; const AddProduct = () => { const { token } = isAutheticated(); let history = useHistory(); const [state, setstate] = useState({ title: "", description: "", status: "", tax: "", price: "", taxes: [], loading: false, }); const { title, description, status, tax, price, taxes, loading } = state; const changeState = (newState) => setstate((prevState) => ({ ...prevState, ...newState })); const fetchTax = useCallback(async () => { let res = await axios.get(`${API}/api/tax/view_tax`, { headers: { Authorization: `Bearer ${token}`, }, }); if (res.status === 200) changeState({ taxes: res.data }); }, [token]); useEffect(() => { fetchTax(); }, [fetchTax]); const handleSubmit = async () => { if (!(title || description || tax || price)) { alert("Please fill required field "); return; } changeState({ loading: true }); let res = await axios.post( `${API}/api/product`, { title, description, status, tax: taxes.find((taxObj) => taxObj.name === tax)?._id, price, }, { headers: { Authorization: `Bearer ${token}`, }, } ); if (res.status === 200) { swal("success!", "Product Added Successfully!", "success"); history.goBack(); } changeState({ loading: false }); }; // const onCancel = () => { // window.location = "/comproducts"; history.goBack() }; const handleChange = (e) => { const { name, value } = e.target; changeState({ [name]: value }); }; return ( <>