conflits resolved
This commit is contained in:
commit
0e3b5c2d8e
@ -48,6 +48,7 @@
|
||||
"country-state-city": "^3.2.1",
|
||||
"draft-js": "^0.11.7",
|
||||
"draft-js-export-html": "^1.4.1",
|
||||
"draft-js-import-html": "^1.4.1",
|
||||
"md5": "^2.3.0",
|
||||
"moment": "^2.30.1",
|
||||
"prop-types": "^15.7.2",
|
||||
|
27
src/_nav.js
27
src/_nav.js
@ -251,18 +251,25 @@ const _nav = [
|
||||
},
|
||||
//Blog start
|
||||
{
|
||||
component: CNavGroup,
|
||||
component: CNavItem,
|
||||
name: "Blog",
|
||||
icon: <CIcon icon={cilCart} customClassName="nav-icon" />,
|
||||
items: [
|
||||
{
|
||||
component: CNavItem,
|
||||
name: "Blog",
|
||||
icon: <CIcon icon={cilNotes} customClassName="nav-icon" />,
|
||||
to: "/blogs",
|
||||
},
|
||||
],
|
||||
icon: <CIcon icon={cilNotes} customClassName="nav-icon" />,
|
||||
to: "/blogs",
|
||||
},
|
||||
|
||||
// {
|
||||
// component: CNavGroup,
|
||||
// name: "Blog",
|
||||
// icon: <CIcon icon={cilNotes} customClassName="nav-icon" />,
|
||||
// items: [
|
||||
// {
|
||||
// component: CNavItem,
|
||||
// name: "Blog",
|
||||
// icon: <CIcon icon={cilNotes} customClassName="nav-icon" />,
|
||||
// to: "/blogs",
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
];
|
||||
|
||||
export default _nav;
|
||||
|
@ -119,6 +119,8 @@ import EditTestimonial from "./views/Testimonials/EditTestimonial";
|
||||
import Blogs from "./views/Blog/Blogs";
|
||||
import CreateBlog from "./views/Blog/CreateBlog";
|
||||
import users from "./views/Users/users";
|
||||
import UpdateBlog from "./views/Blog/EditBlog";
|
||||
import ViewBlog from "./views/Blog/ViewBlog";
|
||||
const routes = [
|
||||
{ path: "/", exact: true, name: "Home" },
|
||||
{
|
||||
@ -493,6 +495,16 @@ const routes = [
|
||||
name: "Blogs",
|
||||
element: CreateBlog,
|
||||
},
|
||||
{
|
||||
path: "/blog/edit/:id",
|
||||
name: "Blogs",
|
||||
element: UpdateBlog,
|
||||
},
|
||||
{
|
||||
path: "/blog/view/:id",
|
||||
name: "Blogs",
|
||||
element: ViewBlog,
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
||||
|
@ -18,34 +18,33 @@ import SearchIcon from "@mui/icons-material/Search";
|
||||
import Fuse from "fuse.js";
|
||||
import { Typography } from "@material-ui/core";
|
||||
import { AppBlockingSharp } from "@mui/icons-material";
|
||||
|
||||
const Blogs = () => {
|
||||
const token = isAutheticated();
|
||||
const [query, setQuery] = useState("");
|
||||
const navigate = useNavigate();
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [success, setSuccess] = useState(true);
|
||||
const [productsData, setProductsData] = useState([]);
|
||||
const [filterData, setFilterData] = useState([]);
|
||||
const [queryData, setQueryData] = useState([]);
|
||||
const [BlogsData, setBlogsData] = useState([]);
|
||||
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const [itemPerPage, setItemPerPage] = useState(10);
|
||||
const [showData, setShowData] = useState(productsData);
|
||||
const [showData, setShowData] = useState(BlogsData);
|
||||
|
||||
const handleShowEntries = (e) => {
|
||||
setCurrentPage(1);
|
||||
setItemPerPage(e.target.value);
|
||||
};
|
||||
|
||||
const getProductsData = async () => {
|
||||
const getBlogsData = async () => {
|
||||
axios
|
||||
.get(`/api/product/getAll/`, {
|
||||
.get(`/api/v1/blog/getallblog/`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
setProductsData(res.data?.product);
|
||||
setBlogsData(res?.data?.BlogData);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch((error) => {
|
||||
@ -61,17 +60,17 @@ const Blogs = () => {
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
getProductsData();
|
||||
getBlogsData();
|
||||
}, [success]);
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = () => {
|
||||
const indexOfLastPost = currentPage * itemPerPage;
|
||||
const indexOfFirstPost = indexOfLastPost - itemPerPage;
|
||||
setShowData(productsData.slice(indexOfFirstPost, indexOfLastPost));
|
||||
setShowData(BlogsData.slice(indexOfFirstPost, indexOfLastPost));
|
||||
};
|
||||
loadData();
|
||||
}, [currentPage, itemPerPage, productsData]);
|
||||
}, [currentPage, itemPerPage, BlogsData]);
|
||||
|
||||
const handleDelete = (id) => {
|
||||
swal({
|
||||
@ -84,7 +83,8 @@ const Blogs = () => {
|
||||
}).then((value) => {
|
||||
if (value === true) {
|
||||
axios
|
||||
.delete(`/api/product/delete/${id}`, {
|
||||
.delete(`/api/v1/blog/deleteblog/${id}`, {
|
||||
// Correct the API endpoint
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
Authorization: `Bearer ${token}`,
|
||||
@ -93,7 +93,7 @@ const Blogs = () => {
|
||||
.then((res) => {
|
||||
swal({
|
||||
title: "Deleted",
|
||||
text: "Product Deleted successfully!",
|
||||
text: "Blog Deleted successfully!",
|
||||
icon: "success",
|
||||
button: "ok",
|
||||
});
|
||||
@ -111,54 +111,23 @@ const Blogs = () => {
|
||||
}
|
||||
});
|
||||
};
|
||||
const [filterCategory, setFilterCategory] = useState("");
|
||||
|
||||
const handleSearchClick = (query) => {
|
||||
const option = {
|
||||
isCaseSensitive: true,
|
||||
includeScore: false,
|
||||
shouldSort: true,
|
||||
includeMatches: false,
|
||||
findAllMatches: false,
|
||||
minMatchCharLength: 1,
|
||||
location: 0,
|
||||
threshold: 0.6,
|
||||
distance: 100,
|
||||
useExtendedSearch: true,
|
||||
ignoreLocation: false,
|
||||
ignoreFieldNorm: false,
|
||||
fieldNormWeight: 1,
|
||||
keys: ["name"],
|
||||
};
|
||||
|
||||
const fuse = new Fuse(productsData, option);
|
||||
const result = fuse.search(query);
|
||||
|
||||
const searchedResult = result.map((result) => result.item);
|
||||
console.log(searchedResult);
|
||||
setQueryData(searchedResult);
|
||||
};
|
||||
useEffect(() => {
|
||||
if (query !== "") {
|
||||
setFilterCategory("");
|
||||
}
|
||||
setTimeout(() => handleSearchClick(query), 100);
|
||||
}, [query]);
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
if (filterCategory !== "") {
|
||||
const filteredProducts = productsData.filter(
|
||||
(product) => product.category?.categoryName === filterCategory
|
||||
if (query !== "") {
|
||||
const lowerCaseQuery = query.toLowerCase(); // Convert query to lowercase
|
||||
|
||||
const searchedResult = BlogsData.filter((item) =>
|
||||
item.title.toString().toLowerCase().includes(lowerCaseQuery)
|
||||
);
|
||||
|
||||
setFilterData(filteredProducts);
|
||||
setShowData(searchedResult);
|
||||
} else {
|
||||
// If no category is selected, show all products
|
||||
setShowData(productsData);
|
||||
// setFilterData(filteredProducts);
|
||||
getBlogsData();
|
||||
}
|
||||
}, 100);
|
||||
}, [filterCategory, productsData]);
|
||||
}, [query]);
|
||||
|
||||
return (
|
||||
<div className="main-content">
|
||||
<div className="page-content">
|
||||
@ -167,11 +136,11 @@ const Blogs = () => {
|
||||
<div className="col-12">
|
||||
<div
|
||||
className="
|
||||
page-title-box
|
||||
d-flex
|
||||
align-items-center
|
||||
justify-content-between
|
||||
"
|
||||
page-title-box
|
||||
d-flex
|
||||
align-items-center
|
||||
justify-content-between
|
||||
"
|
||||
>
|
||||
<div style={{ fontSize: "22px" }} className="fw-bold">
|
||||
Blogs
|
||||
@ -201,20 +170,20 @@ const Blogs = () => {
|
||||
<div className="col-lg-12">
|
||||
<div className="card">
|
||||
<div className="card-body">
|
||||
<div className="row ml-0 mr-0 mb-10 ">
|
||||
<div className="row ml-0 mr-0 mb-10">
|
||||
<div className="col-sm-12 col-md-12">
|
||||
<div className="dataTables_length">
|
||||
<label className="w-100 d-flex gap-2 align-items-center">
|
||||
Show :
|
||||
<select
|
||||
style={{ width: "5%" }}
|
||||
style={{ width: "50px" }}
|
||||
name=""
|
||||
onChange={(e) => handleShowEntries(e)}
|
||||
className="
|
||||
select-w
|
||||
custom-select custom-select-sm
|
||||
form-control form-control-sm
|
||||
"
|
||||
select-w
|
||||
custom-select custom-select-sm
|
||||
form-control form-control-sm
|
||||
"
|
||||
>
|
||||
<option value="10">10</option>
|
||||
<option value="25">25</option>
|
||||
@ -240,7 +209,7 @@ const Blogs = () => {
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
>
|
||||
Search by Blog name :
|
||||
Search by Blog Title :
|
||||
</Typography>
|
||||
<TextField
|
||||
style={{
|
||||
@ -248,10 +217,11 @@ const Blogs = () => {
|
||||
padding: "0.5rem",
|
||||
borderRadius: "8px",
|
||||
flex: "1",
|
||||
border: " 1px solid grey",
|
||||
border: "1px solid grey",
|
||||
marginRight: "2rem",
|
||||
height: "3rem",
|
||||
position: "relative",
|
||||
width: "300px",
|
||||
}}
|
||||
placeholder="Search here..."
|
||||
variant="standard"
|
||||
@ -264,7 +234,6 @@ const Blogs = () => {
|
||||
sx={{
|
||||
background: "white",
|
||||
color: "grey",
|
||||
// marginTop: "0.1rem",
|
||||
height: "2.9rem",
|
||||
width: "3rem",
|
||||
position: "absolute",
|
||||
@ -272,7 +241,7 @@ const Blogs = () => {
|
||||
top: "-8px",
|
||||
borderRadius: "0px 8px 8px 0px",
|
||||
}}
|
||||
onClick={() => handleSearchClick(query)}
|
||||
// onClick={() => handleSearchClick(query)}
|
||||
>
|
||||
<SearchIcon fontSize="small" />
|
||||
</IconButton>
|
||||
@ -312,342 +281,103 @@ const Blogs = () => {
|
||||
<tbody>
|
||||
{!loading && showData.length === 0 && (
|
||||
<tr className="text-center">
|
||||
<td colSpan="6">
|
||||
<td colSpan="4">
|
||||
<h5>No Data Available</h5>
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
{loading ? (
|
||||
<tr>
|
||||
<td className="text-center" colSpan="6">
|
||||
<td className="text-center" colSpan="4">
|
||||
Loading...
|
||||
</td>
|
||||
</tr>
|
||||
) : query === "" && filterCategory == "" ? (
|
||||
showData.map((product, i) => {
|
||||
return (
|
||||
<tr key={i}>
|
||||
<th>
|
||||
{/* {product.image &&
|
||||
product.image.map((i, j) => (
|
||||
<img
|
||||
key={j}
|
||||
className="me-2"
|
||||
src={`${i?.url}`}
|
||||
width="40"
|
||||
alt=""
|
||||
/>
|
||||
))} */}
|
||||
{product.image && (
|
||||
<img
|
||||
key="test"
|
||||
className="me-2"
|
||||
src="https://img.freepik.com/free-photo/pink-headphones-wireless-digital-device_53876-96804.jpg"
|
||||
width="40"
|
||||
alt=""
|
||||
/>
|
||||
)}
|
||||
</th>
|
||||
{/* <td className="text-start">{product.name}</td> */}
|
||||
<td>
|
||||
The All new Samsumg s22 Ultra stormed the
|
||||
market of India
|
||||
</td>
|
||||
{/* <td className="text-start">
|
||||
{product.category?.categoryName !== ""
|
||||
? product.category?.categoryName
|
||||
: "Category Not selected "}
|
||||
</td> */}
|
||||
|
||||
<td className="text-start">
|
||||
{new Date(product.createdAt).toLocaleString(
|
||||
"en-IN",
|
||||
{
|
||||
weekday: "short",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
hour12: true,
|
||||
}
|
||||
)}
|
||||
</td>
|
||||
<td className="text-start">
|
||||
<Link to={`/product/view/${product._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-primary btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mx-1
|
||||
mt-1
|
||||
"
|
||||
>
|
||||
View
|
||||
</button>
|
||||
</Link>
|
||||
<Link to={`/product/edit/${product._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "black",
|
||||
backgroundColor: "white",
|
||||
marginRight: "1rem",
|
||||
borderColor: "black",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-info btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
</Link>
|
||||
<Link
|
||||
to={"#"}
|
||||
style={{
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
>
|
||||
<button
|
||||
style={{ color: "white" }}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-danger btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
|
||||
"
|
||||
onClick={() => {
|
||||
handleDelete(product._id);
|
||||
}}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
) : query !== "" ? (
|
||||
queryData.map((product, i) => {
|
||||
return (
|
||||
<tr key={i}>
|
||||
<th>
|
||||
{product.image &&
|
||||
product.image.map((i, j) => (
|
||||
<img
|
||||
key={j}
|
||||
className="me-2"
|
||||
src={`${i?.url}`}
|
||||
width="40"
|
||||
alt=""
|
||||
/>
|
||||
))}
|
||||
</th>
|
||||
<td className="text-start">{product.name}</td>
|
||||
<td className="text-start">
|
||||
{product.category !== ""
|
||||
? product.category?.categoryName
|
||||
: "Category Not selected "}
|
||||
</td>
|
||||
<th className="text-start">₹{product.price}</th>
|
||||
<td className="text-start">
|
||||
{new Date(product.createdAt).toLocaleString(
|
||||
"en-IN",
|
||||
{
|
||||
weekday: "short",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
hour12: true,
|
||||
}
|
||||
)}
|
||||
</td>
|
||||
<td className="text-start">
|
||||
<Link to={`/product/view/${product._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-primary btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mx-1
|
||||
mt-1
|
||||
"
|
||||
>
|
||||
View
|
||||
</button>
|
||||
</Link>
|
||||
<Link to={`/product/edit/${product._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-info btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
</Link>
|
||||
<Link
|
||||
to={"#"}
|
||||
style={{
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
>
|
||||
<button
|
||||
style={{ color: "white" }}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-danger btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
|
||||
"
|
||||
onClick={() => {
|
||||
handleDelete(product._id);
|
||||
}}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
) : (
|
||||
query == "" &&
|
||||
filterData.map((product, i) => {
|
||||
return (
|
||||
<tr key={i}>
|
||||
<th>
|
||||
{product.image &&
|
||||
product.image.map((i, j) => (
|
||||
<img
|
||||
key={j}
|
||||
className="me-2"
|
||||
src={`${i?.url}`}
|
||||
width="40"
|
||||
alt=""
|
||||
/>
|
||||
))}
|
||||
</th>
|
||||
<td className="text-start">{product.name}</td>
|
||||
<td className="text-start">
|
||||
{product.category?.categoryName}
|
||||
</td>
|
||||
<th className="text-start">₹{product.price}</th>
|
||||
<td className="text-start">
|
||||
{new Date(product.createdAt).toLocaleString(
|
||||
"en-IN",
|
||||
{
|
||||
weekday: "short",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
hour12: true,
|
||||
}
|
||||
)}
|
||||
</td>
|
||||
<td className="text-start">
|
||||
<Link to={`/product/view/${product._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-primary btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mx-1
|
||||
mt-1
|
||||
"
|
||||
>
|
||||
View
|
||||
</button>
|
||||
</Link>
|
||||
<Link to={`/product/edit/${product._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-info btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
</Link>
|
||||
<Link
|
||||
to={"#"}
|
||||
showData.map((blog, index) => (
|
||||
<tr key={index}>
|
||||
<td>
|
||||
{blog.image && (
|
||||
<img
|
||||
src={blog.image.url}
|
||||
alt="Blog Thumbnail"
|
||||
width="40"
|
||||
className="me-2"
|
||||
/>
|
||||
)}
|
||||
</td>
|
||||
<td className="text-start">{blog.title}</td>
|
||||
<td className="text-start">
|
||||
{new Date(blog.createdAt).toLocaleString(
|
||||
"en-IN",
|
||||
{
|
||||
weekday: "short",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
hour12: true,
|
||||
}
|
||||
)}
|
||||
</td>
|
||||
<td className="text-start">
|
||||
<Link to={`/blog/view/${blog._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-primary btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mx-1
|
||||
mt-1
|
||||
"
|
||||
>
|
||||
<button
|
||||
style={{ color: "white" }}
|
||||
type="button"
|
||||
className="
|
||||
View
|
||||
</button>
|
||||
</Link>
|
||||
<Link to={`/blog/edit/${blog._id}`}>
|
||||
<button
|
||||
style={{
|
||||
color: "white",
|
||||
marginRight: "1rem",
|
||||
}}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-info btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
</Link>
|
||||
<button
|
||||
style={{ color: "white" }}
|
||||
type="button"
|
||||
className="
|
||||
btn btn-danger btn-sm
|
||||
waves-effect waves-light
|
||||
btn-table
|
||||
mt-1
|
||||
mx-1
|
||||
|
||||
"
|
||||
onClick={() => {
|
||||
handleDelete(product._id);
|
||||
}}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
onClick={() => handleDelete(blog._id)}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div className="row mt-20">
|
||||
<div className="col-sm-12 col-md-6 mb-20">
|
||||
<div
|
||||
@ -657,11 +387,8 @@ const Blogs = () => {
|
||||
aria-live="polite"
|
||||
>
|
||||
Showing {currentPage * itemPerPage - itemPerPage + 1} to{" "}
|
||||
{Math.min(
|
||||
currentPage * itemPerPage,
|
||||
productsData.length
|
||||
)}{" "}
|
||||
of {productsData.length} entries
|
||||
{Math.min(currentPage * itemPerPage, BlogsData.length)}{" "}
|
||||
of {BlogsData.length} entries
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -709,7 +436,7 @@ const Blogs = () => {
|
||||
|
||||
{!(
|
||||
(currentPage + 1) * itemPerPage - itemPerPage >
|
||||
productsData.length - 1
|
||||
BlogsData.length - 1
|
||||
) && (
|
||||
<li className="paginate_button page-item ">
|
||||
<span
|
||||
@ -728,7 +455,7 @@ const Blogs = () => {
|
||||
className={
|
||||
!(
|
||||
(currentPage + 1) * itemPerPage - itemPerPage >
|
||||
productsData.length - 1
|
||||
BlogsData.length - 1
|
||||
)
|
||||
? "paginate_button page-item next"
|
||||
: "paginate_button page-item next disabled"
|
||||
|
@ -26,110 +26,100 @@ const CreateBlog = () => {
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const [imagesPreview, setImagesPreview] = useState([]);
|
||||
// const [allimage, setAllImage] = useState([]);
|
||||
const [image, setimage] = useState(null);
|
||||
const [title, setTitle] = useState("");
|
||||
const [tag, setTag] = useState([]); //tags array
|
||||
const [productImages, setProductImages] = useState([]);
|
||||
const [tag, setTag] = useState(""); //tags array
|
||||
const [blogContent, setBlogContent] = useState(EditorState.createEmpty());
|
||||
// const [htmlData, setHtmlData] = useState();
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const handleFileChange = (e) => {
|
||||
const files = e.target.files;
|
||||
// Reset error state
|
||||
setError("");
|
||||
|
||||
// Check the total number of selected files
|
||||
if (productImages.length + files.length > 4) {
|
||||
setError("You can only upload up to 4 images.");
|
||||
// Check if more than one image is selected
|
||||
if (files.length > 1) {
|
||||
setError("You can only upload one image.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check file types and append to selectedFiles
|
||||
const allowedTypes = ["image/jpeg", "image/png", "image/jpg"];
|
||||
const selected = [];
|
||||
const file = files[0]; // Only one file is selected, so we get the first one
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
if (productImages.length + selected.length >= 4) {
|
||||
break; // Don't allow more than 4 images
|
||||
}
|
||||
|
||||
if (allowedTypes.includes(files[i].type)) {
|
||||
selected.push(files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (selected.length === 0) {
|
||||
setError("Please upload only PNG, JPEG, or JPG files.");
|
||||
if (allowedTypes.includes(file.type)) {
|
||||
setimage(file);
|
||||
} else {
|
||||
setError("");
|
||||
setProductImages([...productImages, ...selected]);
|
||||
setError("Please upload only PNG, JPEG, or JPG files.");
|
||||
}
|
||||
};
|
||||
|
||||
const handelDelete = (image) => {
|
||||
const filtered = productImages.filter((item) => item !== image);
|
||||
setProductImages(filtered);
|
||||
setimage(null);
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
// Check if any of the required fields are empty
|
||||
if (!title || !image || !tag || !blogContent) {
|
||||
swal({
|
||||
title: "Warning",
|
||||
text: "All fields are required!",
|
||||
icon: "error",
|
||||
button: "Retry",
|
||||
dangerMode: true,
|
||||
});
|
||||
return; // Exit the function early if any field is empty
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
|
||||
const contentState = blogContent.getCurrentContent();
|
||||
const htmlData = stateToHTML(contentState);
|
||||
console.log(title, typeof htmlData, productImages, tag);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("title", title);
|
||||
formData.append("blog_content", htmlData);
|
||||
formData.append("image", image);
|
||||
formData.append("tags", tag);
|
||||
|
||||
// Append each file from productImages array individually
|
||||
productImages.forEach((file, index) => {
|
||||
formData.append(`image${index}`, file);
|
||||
});
|
||||
|
||||
// Append each tag from selected array individually
|
||||
tag.forEach((tag, index) => {
|
||||
formData.append(`tag${index}`, tag);
|
||||
});
|
||||
for (let entry of formData.entries()) {
|
||||
console.log(entry);
|
||||
}
|
||||
// axios
|
||||
// .post(`/api/v1/blog/create`, formData, {
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${token}`,
|
||||
// "Content-Type": "multipart/form-data",
|
||||
// "Access-Control-Allow-Origin": "*",
|
||||
// },
|
||||
// })
|
||||
// .then((res) => {
|
||||
// swal({
|
||||
// title: "Added",
|
||||
// text: "Product added successfully!",
|
||||
// icon: "success",
|
||||
// button: "ok",
|
||||
// });
|
||||
// setLoading(false);
|
||||
// // navigate("/products", { replace: true });
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// setLoading(false);
|
||||
// const message = err.response?.data?.message
|
||||
// ? err.response?.data?.message
|
||||
// : "Something went wrong!";
|
||||
// swal({
|
||||
// title: "Warning",
|
||||
// text: message,
|
||||
// icon: "error",
|
||||
// button: "Retry",
|
||||
// dangerMode: true,
|
||||
// });
|
||||
// });
|
||||
axios
|
||||
.post(`/api/v1/blog/create`, formData, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "multipart/form-data",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
swal({
|
||||
title: "Added",
|
||||
text: "Blog added successfully!",
|
||||
icon: "success",
|
||||
button: "ok",
|
||||
});
|
||||
setLoading(false);
|
||||
navigate("/blogs");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
setLoading(false);
|
||||
const message = err.response?.data?.message
|
||||
? err.response?.data?.message
|
||||
: "Something went wrong!";
|
||||
swal({
|
||||
title: "Warning",
|
||||
text: message,
|
||||
icon: "error",
|
||||
button: "Retry",
|
||||
dangerMode: true,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// console.log(data);
|
||||
// console.log(productImages);
|
||||
// To log the content when needed...
|
||||
|
||||
// console.log(tag);
|
||||
return (
|
||||
<div className="container">
|
||||
<div className="row">
|
||||
@ -143,7 +133,7 @@ const CreateBlog = () => {
|
||||
"
|
||||
>
|
||||
<div style={{ fontSize: "22px" }} className="fw-bold">
|
||||
Add Product
|
||||
Add Blog
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: "1rem" }}>
|
||||
<h4 className="mb-0"></h4>
|
||||
@ -193,30 +183,26 @@ const CreateBlog = () => {
|
||||
type="text"
|
||||
className="form-control"
|
||||
id="name"
|
||||
placeHolder="enter Title"
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
/>
|
||||
{name ? (
|
||||
<>
|
||||
<small className="charLeft mt-4 fst-italic">
|
||||
{25 - name.length} characters left
|
||||
</small>
|
||||
</>
|
||||
) : (
|
||||
<></>
|
||||
)}{" "}
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
<label>Tags</label>
|
||||
<TagsInput
|
||||
<label htmlFor="tag" className="form-label">
|
||||
Tags
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={tag}
|
||||
onChange={setTag}
|
||||
name="Tags"
|
||||
onChange={(e) => setTag(e.target.value)}
|
||||
className="form-control"
|
||||
id="tag"
|
||||
placeHolder="enter Tags"
|
||||
/>
|
||||
<em style={{ fontSize: "0.8rem" }}>
|
||||
press enter or comma to add new tag
|
||||
enter space or comma to add new tag
|
||||
</em>
|
||||
</div>
|
||||
<div>
|
||||
@ -273,48 +259,42 @@ const CreateBlog = () => {
|
||||
<p className="pt-1 pl-2 text-secondary">
|
||||
Upload jpg, jpeg and png only*
|
||||
</p>
|
||||
<Box style={{ display: "flex" }}>
|
||||
{productImages &&
|
||||
productImages.map((image, i) => (
|
||||
<Box marginRight={"2rem"}>
|
||||
<img
|
||||
src={URL.createObjectURL(image)}
|
||||
alt="BlogImage"
|
||||
style={{
|
||||
width: 70,
|
||||
height: 70,
|
||||
{image && (
|
||||
<Box marginRight={"2rem"}>
|
||||
<img
|
||||
src={image ? URL.createObjectURL(image) : ""}
|
||||
alt="BlogImage"
|
||||
style={{
|
||||
width: 70,
|
||||
height: 70,
|
||||
|
||||
marginBottom: "1rem",
|
||||
}}
|
||||
/>
|
||||
<DeleteSharpIcon
|
||||
onClick={() => handelDelete(image)}
|
||||
fontSize="small"
|
||||
sx={{
|
||||
color: "white",
|
||||
position: "absolute",
|
||||
cursor: "pointer",
|
||||
padding: "0.2rem",
|
||||
background: "black",
|
||||
borderRadius: "50%",
|
||||
}}
|
||||
/>
|
||||
{/* </IconButton> */}
|
||||
</Box>
|
||||
))}
|
||||
</Box>
|
||||
marginBottom: "1rem",
|
||||
}}
|
||||
/>
|
||||
{/* <DeleteSharpIcon
|
||||
onClick={() => handelDelete(image)}
|
||||
fontSize="small"
|
||||
sx={{
|
||||
color: "white",
|
||||
position: "absolute",
|
||||
cursor: "pointer",
|
||||
padding: "0.2rem",
|
||||
background: "black",
|
||||
borderRadius: "50%",
|
||||
}}
|
||||
/> */}
|
||||
{/* </IconButton> */}
|
||||
</Box>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div id="createProductFormImage" className="w-25 d-flex">
|
||||
{imagesPreview.map((image, index) => (
|
||||
{/* <div id="createProductFormImage" className="w-25 d-flex">
|
||||
<img
|
||||
className=" w-50 p-1 "
|
||||
key={index}
|
||||
src={image}
|
||||
alt="Blog Image Preview"
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div> */}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
410
src/views/Blog/EditBlog.jsx
Normal file
410
src/views/Blog/EditBlog.jsx
Normal file
@ -0,0 +1,410 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
import Button from "@material-ui/core/Button";
|
||||
import { Link, useNavigate, useParams } from "react-router-dom";
|
||||
import swal from "sweetalert";
|
||||
import axios from "axios";
|
||||
import { isAutheticated } from "src/auth";
|
||||
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
|
||||
import DeleteSharpIcon from "@mui/icons-material/DeleteSharp";
|
||||
import {
|
||||
Box,
|
||||
FormControl,
|
||||
IconButton,
|
||||
MenuItem,
|
||||
Select,
|
||||
TextField,
|
||||
} from "@mui/material";
|
||||
import { Editor } from "react-draft-wysiwyg";
|
||||
import { EditorState, convertFromHTML, ContentState } from "draft-js";
|
||||
import { stateToHTML } from "draft-js-export-html"; // This is for converting Draft.js content state to HTML
|
||||
import { stateFromHTML } from "draft-js-import-html"; // This is for converting HTML to Draft.js content state
|
||||
|
||||
const UpdateBlog = () => {
|
||||
const token = isAutheticated();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const [image, setimage] = useState(null);
|
||||
const [title, setTitle] = useState("");
|
||||
const [tag, setTag] = useState(""); //tags array
|
||||
const [blogContent, setBlogContent] = useState(EditorState.createEmpty());
|
||||
const [error, setError] = useState("");
|
||||
const [newUpdatedImages, setNewUpdatedImages] = useState(null);
|
||||
const [Img, setImg] = useState(true);
|
||||
const id = useParams()?.id;
|
||||
//get Blogdata
|
||||
const getBlog = async () => {
|
||||
try {
|
||||
const res = await axios.get(`/api/v1/blog/getoneblog/${id}`, {
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
// console.log(res?.data?.blog);
|
||||
setTitle(res?.data?.blog?.title);
|
||||
setimage(res?.data?.blog?.image);
|
||||
// Convert HTML content to Draft.js EditorState
|
||||
const contentState = stateFromHTML(res?.data?.blog?.blog_content);
|
||||
const editorState = EditorState.createWithContent(contentState);
|
||||
setBlogContent(editorState);
|
||||
|
||||
setImg(false);
|
||||
|
||||
// Joining the tags array into a string separated by commas
|
||||
const tagsString = res?.data?.blog?.tags.join(",");
|
||||
setTag(tagsString);
|
||||
} catch (err) {
|
||||
swal({
|
||||
title: "Error",
|
||||
text: "Unable to fetch the blog",
|
||||
icon: "error",
|
||||
button: "Retry",
|
||||
dangerMode: true,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
getBlog();
|
||||
}, []);
|
||||
|
||||
const handleFileChange = (e) => {
|
||||
const files = e.target.files;
|
||||
// Reset error state
|
||||
setError("");
|
||||
|
||||
// Check if more than one image is selected
|
||||
if (files.length > 1 || Img === false || newUpdatedImages !== null) {
|
||||
setError("You can only upload one image.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check file types and append to selectedFiles
|
||||
const allowedTypes = ["image/jpeg", "image/png", "image/jpg"];
|
||||
const file = files[0]; // Only one file is selected, so we get the first one
|
||||
|
||||
if (allowedTypes.includes(file.type)) {
|
||||
setNewUpdatedImages(file);
|
||||
} else {
|
||||
setError("Please upload only PNG, JPEG, or JPG files.");
|
||||
}
|
||||
};
|
||||
|
||||
const handelDelete = async (public_id) => {
|
||||
const ary = public_id.split("/");
|
||||
|
||||
const res = await axios.delete(
|
||||
`/api/v1/blog/deleteImage/jatinMor/Blog/${ary[2]}`,
|
||||
{
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
if (res) {
|
||||
setimage(null);
|
||||
setImg(true);
|
||||
}
|
||||
};
|
||||
const handellocalDelete = () => {
|
||||
setNewUpdatedImages(null);
|
||||
};
|
||||
const handleSubmit = () => {
|
||||
if (title === "" || blogContent === "" || tag === "") {
|
||||
swal({
|
||||
title: "Warning",
|
||||
text: "Fill all mandatory fields",
|
||||
icon: "error",
|
||||
button: "Close",
|
||||
dangerMode: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
const contentState = blogContent.getCurrentContent();
|
||||
const htmlData = stateToHTML(contentState);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("title", title);
|
||||
formData.append("blog_content", htmlData);
|
||||
formData.append("tags", tag);
|
||||
|
||||
if (newUpdatedImages !== null) {
|
||||
formData.append("image", newUpdatedImages);
|
||||
}
|
||||
|
||||
axios
|
||||
.patch(`/api/v1/blog/updateblog/${id}`, formData, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "multipart/form-data",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
swal({
|
||||
title: "Updated",
|
||||
text: "Blog Updated successfully!",
|
||||
icon: "success",
|
||||
button: "ok",
|
||||
});
|
||||
setLoading(false);
|
||||
navigate("/blogs", { replace: true });
|
||||
})
|
||||
.catch((err) => {
|
||||
setLoading(false);
|
||||
|
||||
const message = err.response?.data?.message
|
||||
? err.response?.data?.message
|
||||
: "Something went wrong!";
|
||||
swal({
|
||||
title: "Warning",
|
||||
text: message,
|
||||
icon: "error",
|
||||
button: "Retry",
|
||||
dangerMode: true,
|
||||
});
|
||||
});
|
||||
};
|
||||
return (
|
||||
<div className="container">
|
||||
<div className="row">
|
||||
<div className="col-12">
|
||||
<div
|
||||
className="
|
||||
page-title-box
|
||||
d-flex
|
||||
align-items-center
|
||||
justify-content-between
|
||||
"
|
||||
>
|
||||
<div style={{ fontSize: "22px" }} className="fw-bold">
|
||||
Update Blog
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: "1rem" }}>
|
||||
<h4 className="mb-0"></h4>
|
||||
</div>
|
||||
|
||||
<div className="page-title-right">
|
||||
<Button
|
||||
variant="contained"
|
||||
color="primary"
|
||||
style={{
|
||||
fontWeight: "bold",
|
||||
marginBottom: "1rem",
|
||||
textTransform: "capitalize",
|
||||
marginRight: "5px",
|
||||
}}
|
||||
onClick={() => handleSubmit()}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? "Loading" : "Save"}
|
||||
</Button>
|
||||
<Link to="/blogs">
|
||||
<Button
|
||||
variant="contained"
|
||||
color="secondary"
|
||||
style={{
|
||||
fontWeight: "bold",
|
||||
marginBottom: "1rem",
|
||||
textTransform: "capitalize",
|
||||
}}
|
||||
>
|
||||
Back
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="row">
|
||||
<div className="col-lg-6 col-md-6 col-sm-12 my-1">
|
||||
<div className="card h-100">
|
||||
<div className="card-body px-5">
|
||||
<div className="mb-3">
|
||||
<label htmlFor="title" className="form-label">
|
||||
Blog Title
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
className="form-control"
|
||||
id="name"
|
||||
placeHolder="enter Title"
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
<label htmlFor="tag" className="form-label">
|
||||
Tags
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={tag}
|
||||
onChange={(e) => setTag(e.target.value)}
|
||||
className="form-control"
|
||||
id="tag"
|
||||
placeHolder="enter Tags"
|
||||
/>
|
||||
<em style={{ fontSize: "0.8rem" }}>
|
||||
enter space or comma to add new tag
|
||||
</em>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="image" className="form-label">
|
||||
Blog Image
|
||||
</label>
|
||||
<Box>
|
||||
<label htmlFor="upload-Image">
|
||||
<TextField
|
||||
style={{
|
||||
display: "none",
|
||||
width: "350px",
|
||||
height: "350px",
|
||||
borderRadius: "10%",
|
||||
}}
|
||||
fullWidth
|
||||
id="upload-Image"
|
||||
type="file"
|
||||
accept=".jpg , .png ,.jpeg"
|
||||
label="file"
|
||||
multiple
|
||||
variant="outlined"
|
||||
onChange={(e) => handleFileChange(e)}
|
||||
/>
|
||||
<Box
|
||||
style={{ borderRadius: "10%" }}
|
||||
sx={{
|
||||
margin: "1rem 0rem",
|
||||
cursor: "pointer",
|
||||
width: "100px",
|
||||
height: "100px",
|
||||
border: "2px solid grey",
|
||||
// borderRadius: '50%',
|
||||
|
||||
"&:hover": {
|
||||
background: "rgba(112,112,112,0.5)",
|
||||
},
|
||||
}}
|
||||
>
|
||||
<CloudUploadIcon
|
||||
style={{
|
||||
color: "grey",
|
||||
margin: "auto",
|
||||
fontSize: "5rem",
|
||||
marginLeft: "0.5rem",
|
||||
marginTop: "0.5rem",
|
||||
}}
|
||||
fontSize="large"
|
||||
/>
|
||||
</Box>
|
||||
</label>
|
||||
</Box>
|
||||
{error && <p style={{ color: "red" }}>{error}</p>}
|
||||
<div>
|
||||
<strong className="fs-6 fst-italic">
|
||||
*You cannot upload more than 1 image
|
||||
</strong>
|
||||
</div>
|
||||
|
||||
<Box style={{ display: "flex" }}>
|
||||
{Img === false && image !== null ? (
|
||||
<Box marginRight={"2rem"}>
|
||||
<img
|
||||
src={image.url}
|
||||
alt="profileImage"
|
||||
style={{
|
||||
width: 70,
|
||||
height: 70,
|
||||
marginBottom: "1rem",
|
||||
}}
|
||||
/>
|
||||
<DeleteSharpIcon
|
||||
onClick={() => handelDelete(image.public_id)}
|
||||
fontSize="small"
|
||||
sx={{
|
||||
color: "white",
|
||||
position: "absolute",
|
||||
cursor: "pointer",
|
||||
padding: "0.2rem",
|
||||
background: "black",
|
||||
borderRadius: "50%",
|
||||
}}
|
||||
/>
|
||||
</Box>
|
||||
) : null}
|
||||
|
||||
{newUpdatedImages !== null && Img && (
|
||||
<Box marginRight={"2rem"}>
|
||||
<img
|
||||
src={
|
||||
newUpdatedImages
|
||||
? URL.createObjectURL(newUpdatedImages)
|
||||
: ""
|
||||
}
|
||||
// src={newUpdatedImages?.url}
|
||||
alt="profileImage"
|
||||
style={{
|
||||
width: 70,
|
||||
height: 70,
|
||||
|
||||
marginBottom: "1rem",
|
||||
}}
|
||||
/>
|
||||
<DeleteSharpIcon
|
||||
onClick={() => handellocalDelete()}
|
||||
fontSize="small"
|
||||
sx={{
|
||||
color: "white",
|
||||
position: "absolute",
|
||||
cursor: "pointer",
|
||||
padding: "0.2rem",
|
||||
background: "black",
|
||||
borderRadius: "50%",
|
||||
}}
|
||||
/>
|
||||
{/* </IconButton> */}
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
</div>
|
||||
|
||||
{/* <div id="createProductFormImage" className="w-25 d-flex">
|
||||
<img
|
||||
className=" w-50 p-1 "
|
||||
src={image}
|
||||
alt="Blog Image Preview"
|
||||
/>
|
||||
</div> */}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="col-lg-6 col-md-6 col-sm-12 my-1">
|
||||
<div className="card h-100">
|
||||
<div className="card-body px-5">
|
||||
<label>Blog Content</label>
|
||||
{/* Note : style at _custom.scss */}
|
||||
<Editor
|
||||
editorState={blogContent}
|
||||
toolbarClassName="blog-toolbar"
|
||||
wrapperClassName="wrapperClassName"
|
||||
editorClassName="blog-content"
|
||||
onEditorStateChange={(editorState) => {
|
||||
setBlogContent(editorState);
|
||||
}}
|
||||
/>
|
||||
{/* <Button onClick={logContent}>Log Content</Button> */}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default UpdateBlog;
|
135
src/views/Blog/ViewBlog.jsx
Normal file
135
src/views/Blog/ViewBlog.jsx
Normal file
@ -0,0 +1,135 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
import Button from "@material-ui/core/Button";
|
||||
import { Link, useParams } from "react-router-dom";
|
||||
import swal from "sweetalert";
|
||||
import axios from "axios";
|
||||
import { Box } from "@mui/material";
|
||||
|
||||
const ViewBlog = () => {
|
||||
const [image, setImage] = useState(null);
|
||||
const [title, setTitle] = useState("");
|
||||
const [tag, setTag] = useState([]);
|
||||
const [blogContent, setBlogContent] = useState(""); // Changed to string
|
||||
|
||||
const { id } = useParams();
|
||||
|
||||
const getBlog = async () => {
|
||||
try {
|
||||
const res = await axios.get(`/api/v1/blog/getoneblog/${id}`);
|
||||
|
||||
setTitle(res?.data?.blog?.title);
|
||||
setImage(res?.data?.blog?.image);
|
||||
setTag(res?.data?.blog?.tags);
|
||||
// setBlogContent(res?.data?.blog?.blog_content);
|
||||
setBlogContent(addStylesToHTML(res?.data?.blog?.blog_content));
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
swal({
|
||||
title: "Error",
|
||||
text: "Unable to fetch the blog",
|
||||
icon: "error",
|
||||
button: "Retry",
|
||||
dangerMode: true,
|
||||
});
|
||||
}
|
||||
};
|
||||
const addStylesToHTML = (content) => {
|
||||
// Example: Add styles to <p> and <ul> elements
|
||||
content = content.replace(
|
||||
/<p>/g,
|
||||
'<p style="fontSize:1.5rem; margin-top: 1rem; text-transform: capitalize;">'
|
||||
);
|
||||
content = content.replace(/<ul>/g, '<ul style="list-style-type: circle;">');
|
||||
return content;
|
||||
};
|
||||
useEffect(() => {
|
||||
getBlog();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<div className="row">
|
||||
<div className="col-12">
|
||||
<div className="page-title-box d-flex align-items-center justify-content-between">
|
||||
<div style={{ fontSize: "22px" }} className="fw-bold">
|
||||
View Blog
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: "1rem" }}>
|
||||
<h4 className="mb-0"></h4>
|
||||
</div>
|
||||
|
||||
<div className="page-title-right">
|
||||
<Link to="/blogs">
|
||||
<Button
|
||||
variant="contained"
|
||||
color="secondary"
|
||||
style={{
|
||||
fontWeight: "bold",
|
||||
marginBottom: "1rem",
|
||||
textTransform: "capitalize",
|
||||
}}
|
||||
>
|
||||
Back
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="row">
|
||||
<div className="col-lg-12 col-md-12 col-sm-12 my-1">
|
||||
<div className="card h-100">
|
||||
<div className="card-body px-5">
|
||||
<div className="mb-3">
|
||||
{image && (
|
||||
<img
|
||||
src={image.url}
|
||||
alt="blog"
|
||||
style={{ width: "100%", height: "50vh" }}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<h4
|
||||
className="card-title"
|
||||
style={{
|
||||
fontWeight: "bold",
|
||||
fontSize: "3rem",
|
||||
marginBottom: "1rem",
|
||||
textTransform: "capitalize",
|
||||
}}
|
||||
>
|
||||
{title}
|
||||
</h4>
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
flexWrap: "wrap",
|
||||
gap: "1rem",
|
||||
}}
|
||||
>
|
||||
{tag.map((tag, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="badge bg-primary font-size-14"
|
||||
style={{ padding: "0.5rem" }}
|
||||
>
|
||||
#{tag}
|
||||
</div>
|
||||
))}
|
||||
</Box>
|
||||
<div
|
||||
dangerouslySetInnerHTML={{ __html: blogContent }}
|
||||
style={{
|
||||
fontWeight: 600,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ViewBlog;
|
@ -1,6 +1,7 @@
|
||||
import axios from "axios";
|
||||
import React, { useState, useEffect } from "react";
|
||||
import { isAutheticated } from "src/auth";
|
||||
import swal from "sweetalert";
|
||||
|
||||
const OrderDetails = ({ _id, setLoading1 }) => {
|
||||
const token = isAutheticated();
|
||||
@ -18,7 +19,14 @@ const OrderDetails = ({ _id, setLoading1 }) => {
|
||||
setLoading1(false);
|
||||
} catch (error) {
|
||||
console.error("Error fetching orders:", error);
|
||||
// setLoading1(false);
|
||||
swal({
|
||||
title: "Warning",
|
||||
text: error.message,
|
||||
icon: "error",
|
||||
button: "Close",
|
||||
dangerMode: true,
|
||||
});
|
||||
setLoading1(false);
|
||||
}
|
||||
};
|
||||
|
||||
@ -44,10 +52,16 @@ const OrderDetails = ({ _id, setLoading1 }) => {
|
||||
minute: "numeric",
|
||||
hour12: true,
|
||||
})
|
||||
: "No Purchase"}
|
||||
: userOrder
|
||||
? "No Purchase"
|
||||
: "Error"}
|
||||
</td>
|
||||
<td className="text-start">
|
||||
{userOrder?.length > 0 ? userOrder?.length : "No Order"}
|
||||
{userOrder?.length > 0
|
||||
? userOrder?.length
|
||||
: userOrder
|
||||
? "No Order"
|
||||
: "Error"}
|
||||
</td>
|
||||
</>
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user