Skip to content

Commit

Permalink
feat: rendering the comments and pull requests for #191 (#217)
Browse files Browse the repository at this point in the history
* fix: resolved delete handler bug

* feat: rendering the comments

* refactor: some code refactoring

* refactor: removed some consoles

* feat: rendering comments with necessary UI
  • Loading branch information
Girishbari authored Jan 27, 2024
1 parent d1308bc commit a89649f
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/app/post/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const PostDisplay = ({ params: { id } }: Prop) => {
<Suspense fallback={<Loader />}>
<PostById singlePostInfo={post} />
</Suspense>
<Footer isFixed />
<Footer />
</>
);
};
Expand Down
48 changes: 48 additions & 0 deletions src/components/core/comment/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ThumbsUp, ThumbsDown, Repeat } from "react-feather"
import Image from "next/image"

type propType = {
comment: string
}

export default function Comment(props: propType) {
return (
<div className="p-3 rounded-md shadow dark:shadow-gray-600 ">
<a className="flex items-center gap-3 mb-3" href="/user/65a4bf4bea208a60faff">
<div className="w-12 h-12 rounded-full border flex items-center justify-center shadow">
<Image
src="/assets/user.png"
alt="user"
width={135}
height={135}
className=" border-4 border-white dark:border-slate-800 rounded-full object-contain"
/>
</div>
<div>
<h5 className="font-medium text-base">Anonymous User</h5>
<p className="font-thin text-xs/[10px] text-slate-950 dark:text-slate-400">3d ago</p>
</div>
</a>
<a href="/post/65af7e86627eb6f37cf4">
<p className="text-md mb-4">{props.comment}</p>
</a>
<div className="flex justify-around">
<article
className="flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer text-secondary-light dark:text-white hover:text-primary"
>
<ThumbsUp size={22} />
</article>

<article className="flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer text-secondary-light dark:text-white hover:text-primary">
<ThumbsDown size={22} />
</article>

<article className="flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer text-secondary-light dark:text-white hover:text-primary">
<Repeat size={22} orientation={'vertical'} />
</article>


</div>
</div>
)
}
108 changes: 29 additions & 79 deletions src/components/core/posts/SinglePost.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from "next/link";
import { redirect } from 'next/navigation'
import Image from "next/image";
import { Download, Heart, MessageCircle, Share, Bookmark } from "react-feather";
import { PostInstanceType } from "@/types/index.d";
Expand Down Expand Up @@ -28,8 +29,6 @@ export default function SinglePost({
width?: string;
}) {
const [userDetails, setUserDetails] = useState<UserDetails | null>(null);
const [comment_message, setComment_message] = useState("");
const [showCommentBox, setShowCommentBox] = useState(false);
const [commentCount, setCommentCount] = useState(singlePost?.comments?.length || 0);

const dispatch = useDispatch();
Expand Down Expand Up @@ -145,36 +144,16 @@ export default function SinglePost({
}
};

const handleComment = () => {
setShowCommentBox(!showCommentBox);
console.log("comment");
};

const uploadComment = async (id: string | undefined, comment_message: string) => {
const previousComments = singlePost.comments;
try {
if (previousComments === undefined || !id) return;
const Comments = [...previousComments, comment_message];
const res = await addComment(id, Comments);
setCommentCount(res?.comments.length || singlePost?.comments?.length);
toastify("Comment added successfully", "success");
} catch (error) {
console.log(error);
toastify("Comment cannot be added", "error");
}
};

useEffect(() => {
fetchUserDetails();
}, [fetchUserDetails]);

return (
<div
className={` ${
width
? "w-96 p-3 m-auto rounded-md shadow dark:shadow-gray-600 mb-4 mt-40 "
: "p-3 rounded-md shadow dark:shadow-gray-600 mb-4"
} `}
className={` ${width
? "w-96 p-3 m-auto rounded-md shadow dark:shadow-gray-600 mb-4 mt-40 "
: "p-3 rounded-md shadow dark:shadow-gray-600 mb-4"
} `}
>
<Link
className="flex items-center gap-3 mb-3"
Expand Down Expand Up @@ -230,56 +209,56 @@ export default function SinglePost({
<div className="flex justify-around">
<article
onClick={() => onLikeClick(singlePost)}
className={`flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer ${
singlePost?.likes && singlePost?.likes.includes(authState?.userId)
? "text-primary hover:text-primary"
: "text-secondary-light dark:text-white hover:text-primary dark:hover:text-primary"
}`}
className={`flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer ${singlePost?.likes && singlePost?.likes.includes(authState?.userId)
? "text-primary hover:text-primary"
: "text-secondary-light dark:text-white hover:text-primary dark:hover:text-primary"
}`}
>
<Heart
size={22}
fill="true"
className={`${
singlePost?.likes && singlePost?.likes.includes(authState?.userId)
? "fill-primary"
: "fill-transparent"
}`}
className={`${singlePost?.likes && singlePost?.likes.includes(authState?.userId)
? "fill-primary"
: "fill-transparent"
}`}
/>
<span className="text-base">
{singlePost && singlePost?.likes && singlePost?.likes.length}
</span>
</article>

<article
onClick={handleComment}
<Link
href={`/post/${singlePost && singlePost?.$id}`}
className="flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer text-secondary-light dark:text-white hover:text-primary"
>
<MessageCircle size={22} />
<span className="text-base">{commentCount}</span>
</article>
{commentCount ? (
<span className="text-base">{commentCount}</span>
) : (
<span className="text-base">{singlePost.comments?.length} </span>
)}
</Link>

<article
onClick={() => handleUpdateBookmark(singlePost?.$id)}
className={`flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer ${
userBookmarks &&
className={`flex flex-row gap-3 items-center transition ease-in-out duration-200 hover:cursor-pointer ${userBookmarks &&
userBookmarks?.bookmark &&
userBookmarks?.bookmark?.length > 0 &&
userBookmarks?.bookmark.includes(singlePost && singlePost?.$id!)
? "text-primary hover:text-primary dark:hover:text-primary"
: "text-secondary-light dark:text-white hover:text-primary dark:hover:text-primary"
}`}
? "text-primary hover:text-primary dark:hover:text-primary"
: "text-secondary-light dark:text-white hover:text-primary dark:hover:text-primary"
}`}
>
<Bookmark
size={22}
fill="true"
className={`${
userBookmarks &&
className={`${userBookmarks &&
userBookmarks?.bookmark &&
userBookmarks?.bookmark?.length > 0 &&
userBookmarks?.bookmark.includes(singlePost && singlePost?.$id!)
? "fill-primary"
: "fill-transparent"
}`}
? "fill-primary"
: "fill-transparent"
}`}
/>
</article>

Expand All @@ -294,35 +273,6 @@ export default function SinglePost({
<Download size={22} />
</article>
</div>
{showCommentBox ? (
<div>
<div className="flex flex-1">
<textarea
onChange={(event: any) => setComment_message(event.target.value)}
value={comment_message}
name="postTitle"
className="mt-2 dark:bg-secondary-light outline-none focus:ring rounded-lg p-3 text-black dark:text-white placholder:text-gray-400 text-lg w-full mb-2"
rows={2}
cols={50}
placeholder="Type your comment here"
onKeyDown={(e) => {
if (isCtrlEnter(e)) uploadComment(singlePost && singlePost?.$id, comment_message);
}}
/>
</div>
<div className="flex flex-end">
<button
onClick={() => {
uploadComment(singlePost && singlePost?.$id, comment_message);
}}
className="transition-all duration-300 bg-primary hover:bg-primary-light text-white font-normal py-1 px-8 rounded-full"
>
{"Post"}
</button>
{/* <button onClick={handleTest}>test</button> */}
</div>
</div>
) : null}
</div>
);
}
1 change: 0 additions & 1 deletion src/components/core/posts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default function Posts() {
(a: any, b: any) => new Date(b["$createdAt"]).getTime() - new Date(a["$createdAt"]).getTime(),
);
copyPosts = copyPosts.filter((post: PostInstanceType) => post.isActive === true);
console.log(copyPosts);
}

if (postState.loading) {
Expand Down
140 changes: 121 additions & 19 deletions src/components/pages/post/index.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,131 @@
import Link from "next/link";
import { useRouter } from "next/router";
import { Suspense } from "react";
import { useRouter } from "next/navigation";
import { Suspense, useState } from "react";

import SinglePost from "@/components/core/posts/SinglePost";
import TrendingFeed from "@/components/core/trendingFeed";
import Footer from "@/components/core/footer";

import { PostInstanceType } from "@/types";
import { ArrowLeft } from "react-feather";


export default function PostById({ singlePostInfo }: { singlePostInfo: PostInstanceType }) {
const router = useRouter();
import { toastify } from "@/helper/toastify";
import isCtrlEnter from "@/helper/isCtrlEnter";

return (
<main className="flex sm:flex-row flex-col max-w-screen-lg mx-auto pt-8 content-center px-4">
<div className="flex-[1]">
<button onClick={() => router.back()} className="text-primary hover:text-primary-light">
<ArrowLeft size={20} className="mr-2" />
{"Back"}
</button>
</div>
<div className="flex-[5] mt-4 sm:mt-0">
<SinglePost singlePost={singlePostInfo} />
</div>
<div className="flex-[2] hidden md:block">
<TrendingFeed />

import { addComment } from "@/backend/posts.api";

import { Loader } from "react-feather";
import Loading from "@/app/feed/loading";
import Comment from "@/components/core/comment";

export default function PostById({ singlePostInfo, onLikeClick }: { singlePostInfo: PostInstanceType, onLikeClick?: any; }) {
const router = useRouter();
let CHAR_LIMIT = 250;
const [comment_message, setComment_message] = useState("");
const [commentCount, setCommentCount] = useState(singlePostInfo?.comments?.length || 0);
const [loading, setLoading] = useState(false);






const uploadComment = async (id: string | undefined, comment_message: string) => {
setLoading(true);
const previousComments = singlePostInfo.comments;
try {
if (previousComments === undefined || !id) return;
if (comment_message === "") {
toastify("Comment can not be empty", "error")
setLoading(false)
return;
}
const Comments = [...previousComments, comment_message];
const res = await addComment(id, Comments);
setCommentCount(res?.comments.length || singlePostInfo?.comments?.length);
toastify("Comment added successfully", "success");
singlePostInfo.comments?.push(comment_message)
setComment_message("");
setLoading(false)
} catch (error) {
console.log(error);
toastify("Comment cannot be added", "error");
setLoading(false)
}
};


return (
<>
<main className="flex flex-col sm:flex-row max-w-screen-lg mx-auto pt-8 content-center px-4">
<div className="flex-[1]">
<button onClick={() => router.back()} className="text-primary hover:text-primary-light">
<ArrowLeft size={20} className="mr-2" />
{"Back"}
</button>
</div>
<div>
<div className="flex-[5] mt-4 sm:mt-0">
<div className="flex-[5] mt-2 sm:mt-0">
<SinglePost singlePost={singlePostInfo} onLikeClick={singlePostInfo} />
<div className=" flex flex-col shadow dark:shadow-gray-600 p-2 ">
<div className="relative">

<textarea
onChange={(event: any) => setComment_message(event.target.value)}
value={comment_message}
name="comment"
className="dark:bg-secondary-light outline-none focus:ring rounded-lg p-3 text-black dark:text-white placholder:text-gray-400 text-lg w-full mb-2"
rows={4}
cols={50}
placeholder="Type your comment here"
maxLength={CHAR_LIMIT}
required
onKeyDown={(e) => {
if (isCtrlEnter(e)) uploadComment(singlePostInfo && singlePostInfo?.$id, comment_message);
}}
/>
<button
onClick={() => {
uploadComment(singlePostInfo && singlePostInfo?.$id, comment_message);
}}
className="absolute left-3 bottom-3 transition-all duration-300 bg-primary hover:bg-primary-light text-white font-normal py-1 px-8 my-3 rounded-full"
>
{loading ? (
<Loader size={24} className="mx-auto animate-spin self-center" />
) : (
<p>post</p>
)}
</button>
</div>
<div className=" ">
{/* <button onClick={handleTest}>test</button> */}
</div>
</div>
<div>
{singlePostInfo.comments?.length == 0 &&
<h1 className="text-white text-2xl text-center mt-5">There are no Comments!</h1>
}
<Suspense fallback={<Loading />}>
{singlePostInfo.comments != undefined
&& singlePostInfo.comments?.length > 0
&& singlePostInfo.comments?.slice().reverse().map((comment, idx) => (
<ul key={idx} className="rounded-lg">
<Comment comment={comment} />
</ul>
))}
</Suspense>
</div>
</div>
</main>
);

</div>
</div>
<div className="flex-[2] hidden md:block">
<TrendingFeed />
</div>
</main>
</>
);
}

1 comment on commit a89649f

@vercel
Copy link

@vercel vercel bot commented on a89649f Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.