File size: 4,385 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
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 = () => (
<div className="wrap-login100 p-l-85 p-r-85 p-t-55 p-b-55">
<form className="mb-5" onSubmit={submitCategoryForm}>
<span className="login100-form-title p-b-32 m-b-7">Update Category Form</span>
<span className="txt1 p-b-11">Category Name</span>
<br />
<br />
<div className="wrap-input100 validate-input m-b-36">
<input
onChange={handleChange('name')}
value={name}
className="input100"
type="text"
required
name="name"
/>
</div>
<div className="w-size25">
<button type="submit" className="flex-c-m size2 bg1 bo-rad-23 hov1 m-text3 trans-0-4">
Save Changes
</button>
</div>
</form>
</div>
);
const showError = () => (
<div className={'alert alert-danger my-3'} role="alert" style={{ display: error ? '' : 'none' }}>
<button type="button" className="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{error}
</div>
);
const redirectUser = () => {
if (redirectToProfile) {
if (!error) {
return <Redirect to="/admin/categories" />;
}
}
};
const goBackBTN = () => {
return (
<div className="my-5 p-3 border rounded">
<Link to="/admin/categories" className="text-info">
Back To Admin Home
</Link>
</div>
);
};
return (
<Layout
title={`Hi ${user.name}`}
description={`This is Update Product Action Page`}
className="container-fluid"
>
<div className="row">
<div className="col-md-8 offset-md-2 m-b-250 mb-5">
{showError()}
{updateCategoryForm()}
{goBackBTN()}
{redirectUser()}
</div>
</div>
</Layout>
);
};
export default UpdateCategory;
|