Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend Integrated for Recovering the blogs after 30 days and Backend Integrated for blocking the post #1182

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion website3.0/components/ParticleBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const ParticlesComponent = (props) => {

// Function to handle when particles are loaded into the container
const particlesLoaded = (container) => {
console.log(container); // Example: Log the container where particles are loaded
};

// Memoized options for particles configuration
Expand Down
1 change: 0 additions & 1 deletion website3.0/components/Popup1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default function Popup1(props) {
return () => clearTimeout(timer);
}, []);

console.log('sdsdsdsdsd')
return (
<div className={`${error} popup ${none ? "none1" : ""}`}>
{msg}
Expand Down
2 changes: 0 additions & 2 deletions website3.0/components/blogpage/CreateBlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export default function CreateBlog({id}) {
id: draftId,
author_id: finalUser._id,
};
console.log('sddddddddddddddd')
// Convert data to FormData for sendBeacon
const formData = new FormData();
for (const key in draftData) {
Expand Down Expand Up @@ -224,7 +223,6 @@ console.log('sddddddddddddddd')
});
if (response.ok) {
let data = await response.json();
console.log('sdsaveing id ddddd',data.id)
setDraftId(data.id);

localStorage.setItem('draftId',data.id)
Expand Down
2 changes: 1 addition & 1 deletion website3.0/components/profile/NotificationTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function NotificationTab() {
}
);

let existingNotifications = { followerList: {}, blogList: {} };
let existingNotifications = { followerList: {}, blogList: {},blogComments:{} };

if (checkResponse.ok) {
existingNotifications = await checkResponse.json();
Expand Down
250 changes: 152 additions & 98 deletions website3.0/pages/BlogPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import "react-loading-skeleton/dist/skeleton.css";
import { FaEye, FaPen } from "react-icons/fa";
import { motion } from "framer-motion";

function BlogPage({ theme,finalUser,searchedBlog,setFinalUser,subject }) {
function BlogPage({ theme,finalUser,searchedBlog,setFinalUser,subject,updateUser }) {
const [blogs, setBlogs] = useState([]);
const [authorDetails, setAuthorDetails] = useState({});
const [error, setError] = useState("");
Expand Down Expand Up @@ -97,6 +97,25 @@ function BlogPage({ theme,finalUser,searchedBlog,setFinalUser,subject }) {
useEffect(()=>{
fetchTagsData()
},[])
useEffect(()=>{
console.log(finalUser)
},[])
async function handleBlockBlog(data){
console.log("fetching...")
let user=finalUser
console.log(user.blockedBlogs)
user.blockedBlogs=[...user.blockedBlogs,data]
console.log(user.blockedBlogs)
updateUser(user)
let res=await fetch("/api/blockblog",{
method:"POST",
body:JSON.stringify({
user_id:finalUser._id,
blog_id:data
})
})

}
useEffect(() => {
const fetchBlogs = async () => {
try {
Expand Down Expand Up @@ -334,10 +353,13 @@ useEffect(()=>{
filtered = blogs;

if (filter === "mustRead") {
filtered = mustReadBlogs;
filtered = mustReadBlogs.filter((data)=>!data.isDeleted);
} else if (filter === "bookmarked") {
const reactionIds = finalUser && finalUser.reactions ? Object.keys(finalUser.reactions) : [];
filtered = blogs.filter((blog) => reactionIds.includes(blog._id));
}else if(filter=="delete"){
filtered=blogs.filter((data)=>data.isDeleted&& data.authorId==finalUser._id)

}
if (searchedBlog) {
filtered = filtered.filter((blog) => {
Expand Down Expand Up @@ -371,6 +393,10 @@ useEffect(()=>{
setFilter("Tags")
setShowTags(true)
}
function handleDeleteTabClick(){
setFilter("delete")
filteredBlogs()
}
useEffect(()=>{
setTimeout(()=>{setLoading(false)},4000)
},[])
Expand Down Expand Up @@ -512,6 +538,15 @@ setTimeout(()=>{setLoading(false)},4000)
setIsShowFollow(false)
}
}
async function handleRecoverBlog(id){
let res=await fetch('/api/recoverblog',{
method:"PUT",
body:JSON.stringify({
id:id
})
})
window.location.reload()
}
return (
<>
{confetti && <Confetti/>}
Expand Down Expand Up @@ -605,6 +640,20 @@ setTimeout(()=>{setLoading(false)},4000)
>
Tags
</div>
<div
className={`w-[100px] ${
theme
? filter === "Tags"
? "text-gray-900 underline underline-offset-[30px]"
: ""
: filter === "Tags"
? "underline text-gray-400 underline-offset-[30px]"
: ""
}`}
onClick={handleDeleteTabClick}
>
Deleted Blogs
</div>
<div className="lg:hidden ml-auto" onClick={toggleSidebar}>
<FontAwesomeIcon icon={faBars} />
</div>
Expand Down Expand Up @@ -638,110 +687,115 @@ setTimeout(()=>{setLoading(false)},4000)
? finalUser.reactions && finalUser.reactions.hasOwnProperty(blog._id)
: false;

return (
<div
className="cursor-pointer relative"
key={index}

>
<div className="flex items-center mb-2" onClick={() => navigateToBlogDetails(blog._id)}>
<img
src={author.image1}
onError={handleImageError}
className="w-6 h-6 rounded-full mr-3"
/>
<div className="text-sm">{blog.authorName}</div>
</div>
<div className="flex gap-10 items-center max-md:flex-col max-md:items-start">
<div className="flex-1">
<div onClick={() => navigateToBlogDetails(blog._id)}
className="text-2xl mb-2 font-normal max-sm:text-xl"
dangerouslySetInnerHTML={{ __html: blog.title }}
></div>
<div className="flex gap-[10px] mb-[10px] flex-wrap">
{blog.tags && blog.tags.map(data=>{
console.log(data)
return <div className="text-[14px]">{"#"+data}</div>})}
</div>
<div onClick={() => navigateToBlogDetails(blog._id)}
className={`${
theme ? "text-gray-600" : "text-gray-300"
} font-medium max-sm:text-sm transition-all duration-200`}
>
{renderBlogDescription(blog.description)}
</div>
<div
className={`${
theme ? "text-gray-500" : "text-gray-300"
} flex text-sm justify-between items-center max-sm:flex-wrap max-sm:gap-2 transition-all duration-200`}
>
<div className="flex gap-5 items-center max-sm:flex-wrap" onClick={() => navigateToBlogDetails(blog._id)}>
<div className="my-2 font-medium">
{formatDate(blog.date)}
</div>
<div>
<FontAwesomeIcon onClick={() => navigateToBlogDetails(blog._id)}
icon={faHands}
className="mr-2"
/>
{blog.reactionList.reduce(
(sum, reaction) => sum + reaction.count,
0
)}
if(finalUser&&(!finalUser?.blockedBlogs?.includes(blog._id))&&(filter=="delete"?blog.isDeleted:(!blog.isDeleted&&blog.authorId==finalUser._id))){


return (
<div
className="cursor-pointer relative"
key={index}

>
<div className="flex items-center mb-2" onClick={() => navigateToBlogDetails(blog._id)}>
<img
src={author.image1}
onError={handleImageError}
className="w-6 h-6 rounded-full mr-3"
/>
<div className="text-sm">{blog.authorName}</div>
</div>
<div className="flex gap-10 items-center max-md:flex-col max-md:items-start">
<div className="flex-1">
<div onClick={() => navigateToBlogDetails(blog._id)}
className="text-2xl mb-2 font-normal max-sm:text-xl"
dangerouslySetInnerHTML={{ __html: blog.title }}
></div>
<div className="flex gap-[10px] mb-[10px] flex-wrap">
{blog.tags && blog.tags.map(data=>{
console.log(data)
return <div className="text-[14px]">{"#"+data}</div>})}
</div>
<div onClick={() => navigateToBlogDetails(blog._id)}
className={`${
theme ? "text-gray-600" : "text-gray-300"
} font-medium max-sm:text-sm transition-all duration-200`}
>
{renderBlogDescription(blog.description)}
</div>
<div
className={`${
theme ? "text-gray-500" : "text-gray-300"
} flex text-sm justify-between items-center max-sm:flex-wrap max-sm:gap-2 transition-all duration-200`}
>
<div className="flex gap-5 items-center max-sm:flex-wrap" onClick={() => navigateToBlogDetails(blog._id)}>
<div className="my-2 font-medium">
{formatDate(blog.date)}
</div>
<div>
<FontAwesomeIcon onClick={() => navigateToBlogDetails(blog._id)}
icon={faHands}
className="mr-2"
/>
{blog.reactionList.reduce(
(sum, reaction) => sum + reaction.count,
0
)}
</div>
<div>
<FontAwesomeIcon
icon={faComment}
className="mr-2"
/>
{blog.comments.length}
</div>
<div className="flex gap-2 items-center">
<FaEye/> {Math.ceil(blog.views/2)} &nbsp; &nbsp;
{blog.average?Math.ceil(Math.ceil(blog.average/Math.ceil((blog.views/2)))/60):0 }&nbsp;min read
</div>
</div>
<div>
<div className="flex gap-4">
<FontAwesomeIcon
icon={faComment}
icon={
isBookmarked ? solidBookmark : regularBookmark
}
className="mr-2"
/>
{blog.comments.length}
</div>
<div className="flex gap-2 items-center">
<FaEye/> {Math.ceil(blog.views/2)} &nbsp; &nbsp;
{blog.average?Math.ceil(Math.ceil(blog.average/Math.ceil((blog.views/2)))/60):0 }&nbsp;min read
</div>
{blog.authorId==finalUser._id && <FaEllipsis onClick={()=>modalIndex==index?setModalIndex(-1):setModalIndex(index)}/>
} </div>

</div>
<div className="flex gap-4">
<FontAwesomeIcon
icon={
isBookmarked ? solidBookmark : regularBookmark
}
className="mr-2"
/>
{blog.authorId==finalUser._id && <FaEllipsis onClick={()=>modalIndex==index?setModalIndex(-1):setModalIndex(index)}/>
} </div>

</div>
<img onClick={() => navigateToBlogDetails(blog._id)}
src={blog.image}
onError={handleImageError}
className="h-[150px] w-[200px] bg-white object-cover object-center max-md:w-full max-md:h-[200px]"
/>
</div>
<img onClick={() => navigateToBlogDetails(blog._id)}
src={blog.image}
onError={handleImageError}
className="h-[150px] w-[200px] bg-white object-cover object-center max-md:w-full max-md:h-[200px]"
/>
{index == modalIndex && !blog.isDeleted && <div className="absolute h-[auto] w-[200px] flex flex-col gap-[20px] right-36 p-[20px] bottom-20 rounded-lg bg-white">
<> <div className="flex flex-row gap-[10px] justify-center" onClick={()=>handleBlogDelete(blog)}> <FaTrash className="hover:cursor-pointer" color="red" onClick={()=>handleBlogDelete(blog)}/>Delete Post</div>
<div className="z-[100000] flex w-[100%] gap-[10px] items-center justify-center">
<div onClick={(e)=>{
e.preventDefault();
let res=confirm("Are you sure that you want to edit this blog ")
if(res){ router.push(`/editblog?id=${blog._id}`)}else{
return
}}} className=" flex gap-[10px] ">
<FaPen color="#5a6370"/>
</div>
<span onClick={(e)=>{
e.preventDefault();
let res=confirm("Are you sure that you want to edit this blog ")
if(res){ router.push(`/editblog?id=${blog._id}`)}else{
return
}}} className="max-md:hidden text-[#5a6370] font-semibold">Edit Blog</span></div>
<p className="w-[100%] flex justify-center items-center" onClick={()=>handleBlockBlog(blog._id)}>Block Blog</p></>
</div>}
{blog.isDeleted && <p onClick={()=>handleRecoverBlog(blog._id)}>Recover Blog</p>}

<hr className="w-full mt-5 mb-5 border-gray-200" />
</div>
{index == modalIndex && <div className="absolute h-[auto] w-[200px] flex flex-col gap-[20px] right-36 p-[20px] bottom-20 rounded-lg bg-white">
<div className="flex flex-row gap-[10px] justify-center" onClick={()=>handleBlogDelete(blog)}> <FaTrash className="hover:cursor-pointer" color="red" onClick={()=>handleBlogDelete(blog)}/>Delete Post</div>
<div className="z-[100000] flex w-[100%] gap-[10px] items-center justify-center">
<div onClick={(e)=>{
e.preventDefault();
let res=confirm("Are you sure that you want to edit this blog ")
if(res){ router.push(`/editblog?id=${blog._id}`)}else{
return
}}} className=" flex gap-[10px] ">
<FaPen color="#5a6370"/>
</div>
<span onClick={(e)=>{
e.preventDefault();
let res=confirm("Are you sure that you want to edit this blog ")
if(res){ router.push(`/editblog?id=${blog._id}`)}else{
return
}}} className="max-md:hidden text-[#5a6370] font-semibold">Edit Blog</span></div>

</div>}

<hr className="w-full mt-5 mb-5 border-gray-200" />
</div>
);
);
}
}))}
{
showTags && <div className="w-[100%] flex flex-wrap max-sm:justify-center justify-between">
Expand Down
42 changes: 42 additions & 0 deletions website3.0/src/app/api/blockblog/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Blogs from "@utils/models/blog"; // Importing Mongoose model for blog collection
import user from "@utils/models/user";
import mongoose from "mongoose"; // Importing Mongoose for MongoDB interactions
import { NextResponse } from "next/server"; // Importing Next.js server response utility

export async function GET() {
const { MONGO_URI } = process.env;
// Connect to MongoDB using Mongoose
await mongoose.connect(MONGO_URI);
let data = [];
data = await Blogs.find();
console.log(data)
return NextResponse.json({data});
}

export async function POST(req) {
try {
// Parse JSON payload from request body
const {user_id,blog_id} = await req.json();

const { MONGO_URI } = process.env;

// Connect to MongoDB using Mongoose
await mongoose.connect(MONGO_URI);

// Create a new instance of Blogs model with the received payload
let user1 = await user.findById(user_id)
console.log(user1)
user1.blockedBlogs.push(blog_id)
// Save the new blog record to MongoDB
const result = await user1.save();
console.log(user1)
// Return success response with saved blog details
return NextResponse.json({success: true });
} catch (error) {
console.error("Error in POST /api/blog:", error);
return NextResponse.json({ success: false, error: error.message });
}
}



Loading
Loading