import React, { useState } from "react"; import Layout from "../core/Layout"; import { isAuthenticated } from "../auth"; import { Link } from "react-router-dom"; import { createCategory } from "./apiAdmin"; const AddCategory = () => { const [name, setName] = useState(""); const [error, setError] = useState(false); const [success, setSuccess] = useState(false); // destructure user and token from localstorage const { user, token } = isAuthenticated(); const handleChange = e => { setError(""); setName(e.target.value); }; const clickSubmit = e => { e.preventDefault(); setError(""); setSuccess(false); // make request to api to create category createCategory(user._id, token, { name }).then(data => { if (data.error) { setError(data.error); } else { setError(""); setSuccess(true); } }); }; const newCategoryFom = () => (
); const showSuccess = () => { if (success) { return

{name} is created

; } }; const showError = () => { if (error) { return

Category should be unique

; } }; const goBack = () => (
Back to Dashboard
); return (
{showSuccess()} {showError()} {newCategoryFom()} {goBack()}
); }; export default AddCategory;