import React, { useState, useEffect } from 'react'; import Layout from '../core/Layout'; import { isAuthenticated } from '../auth'; import { Link, Redirect } from 'react-router-dom'; import { getCategory, updateCategory } from './apiAdmin'; // {category: ["5cd0258f2793ec6e100bc191"], price: []} // http://localhost:3000/admin/category/update/5cd0258f2793ec6e100bc191 const UpdateCategory = ({ match }) => { const [values, setValues] = useState({ name: '', error: '', redirectToProfile: false, formData: '' }); // destructure user and token from localStorage const { user, token } = isAuthenticated(); const { name, error, redirectToProfile } = values; const init = categoryId => { getCategory(categoryId, token).then(data => { if (data.error) { setValues({ ...values, error: data.error }); } else { // populate the state setValues({ ...values, name: data.name }); } }); }; useEffect(() => { init(match.params.categoryId); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleChange = name => event => { setValues({ ...values, error: false, [name]: event.target.value }); }; const submitCategoryForm = e => { e.preventDefault(); // update with ? you should send category name otherwise what to update? const category = { name: name }; updateCategory(match.params.categoryId, user._id, token, category).then(data => { if (data.error) { setValues({ ...values, error: data.error }); } else { setValues({ ...values, name: data.name, error: false, redirectToProfile: true }); } }); }; const updateCategoryForm = () => (
Update Category Form Category Name

); const showError = () => (
{error}
); const redirectUser = () => { if (redirectToProfile) { if (!error) { return ; } } }; const goBackBTN = () => { return (
Back To Admin Home
); }; return (
{showError()} {updateCategoryForm()} {goBackBTN()} {redirectUser()}
); }; export default UpdateCategory;