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 { Country, State, City } from "country-state-city"; import swal from 'sweetalert'; import { Link, useParams } from "react-router-dom"; import { useHistory } from "react-router-dom"; const EditBisuness = () => { const [categoryName, setCategoryName] = useState([]); const [image, setImage] = useState(); const token = isAutheticated(); let history = useHistory(); const [state, setState] = useState({ name: "", phone: "", email: "", Building_Name: "", Street_Name: "", country: "", city: "", loading: false, description: "", category: "", status: "", Glocation: "", LinkedinUrl: "", FacebookUrl: "", InstagramUrl: "" }); const { id } = useParams(); // console.log(id) const { description, loading } = state; const changeState = (newState) => setState((prevState) => ({ ...prevState, ...newState })); const handleChange = (e) => { changeState({ ...state, [e.target.name]: e.target.value }) }; //category const fetchCategory = useCallback(async () => { const res = await axios.get(`/api/category/getAll`, { headers: { Authorization: `Bearer ${token}`, }, }); // console.log(res.data.category); setCategoryName(res.data.category) if (res.status === 200) changeState({ ...res.data }); }, [token]); useEffect(async () => { fetchCategory(); }, [fetchCategory]); const fetchDirectory = useCallback(async () => { const res = await axios.get(`/api/directory/getOne/${id}`, { headers: { Authorization: `Bearer ${token}`, }, }); setState(res.data.directory) changeState({ loading: false }); if (res.status === 200) changeState({ ...res.data }); }, [token]); useEffect(() => { fetchDirectory(); }, [fetchDirectory]); const handleSubmit = async () => { if (!(state.name && state.phone && state.email && state.Building_Name && state.Street_Name && state.country && state.city && state.description && state.category && state.status && image)) { return swal('Error!', 'All fields are required', 'error') } const myForm = new FormData(); myForm.set('name', state.name) myForm.set('phone', state.phone) myForm.set('email', state.email) myForm.set('Building_Name', state.Building_Name) myForm.set('Street_Name', state.Street_Name) myForm.set('country', state.country) myForm.set('city', state.city) myForm.set('description', state.description) myForm.set('category', state.category) myForm.set('status', state.status) myForm.set('Glocation', state.Glocation) myForm.set('LinkedinUrl', state.Glocation) myForm.set('FacebookUrl', state.FacebookUrl) myForm.set('InstagramUrl', state.InstagramUrl) myForm.set("image", image); changeState({ loading: true }); try { let res = await axios.put( `/api/directory/update/${id}`, myForm, { headers: { "content-Type": 'multipart/form-data', Authorization: `Bearer ${token}`, }, } ); if (res.status == 200) { changeState({ loading: false }); swal("Edit Business successfully!"); history.goBack() } } catch (error) { swal('Error!', error, 'error') changeState({ loading: false }); } }; const onCancel = () => { history.goBack() }; // console.log(image) return ( <>