Skip to content

Commit

Permalink
snapgram v2 latest
Browse files Browse the repository at this point in the history
  • Loading branch information
ammrshmbng committed Oct 7, 2024
1 parent 991b90e commit eae5bfe
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 73 deletions.
1 change: 0 additions & 1 deletion src/_root/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useGetPosts } from "@/lib/react-query/queries";

const Home = () => {
const { data: posts, fetchNextPage, hasNextPage } = useGetPosts() as any;
console.log(posts)
const { ref, inView } = useInView();

useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/_root/pages/PostDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useParams, Link, useNavigate } from "react-router-dom";

import { Button } from "@/components/ui";
import { Loader } from "@/components/shared";
import { Loader, PostComment } from "@/components/shared";
import { GridPostList, PostStats } from "@/components/shared";

import { multiFormatDateString } from "@/lib/utils";
Expand Down Expand Up @@ -132,6 +132,7 @@ const PostDetails = () => {

<div className="w-full">
<PostStats post={post} userId={user.id} />
<PostComment postId={post.id} />
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/_root/pages/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ const Profile = () => {
</p>
</Link>
</div>
<div className={`${myFollowingUsersId.includes(id) && "hidden"}`}>
<div className={`${myFollowingUsersId?.includes(id) && "hidden"}`}>
<Button type="button" className="px-8 shad-button_primary"
onClick={handleFollowUser}
>
Follow
</Button>
</div>
<div className={`${!myFollowingUsersId.includes(id) && "hidden"}`}>
<div className={`${!myFollowingUsersId?.includes(id) && "hidden"}`}>
<Button type="button" className="px-8 shad-button_primary"
onClick={handleUnfollowUser}
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/shared/GridPostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const GridPostList = ({
"/assets/icons/profile-placeholder.svg"
}
alt="creator"
className="w-8 h-8 rounded-full"
className="w-8 h-8 rounded-full "
/>
<p className="line-clamp-1">{post.user?.username}</p>
</div>
Expand Down
60 changes: 31 additions & 29 deletions src/components/shared/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import { Link } from "react-router-dom";
import { PostElement } from "@/types";
import { useUserContext } from "@/context/useUserContext";
import { multiFormatDateString } from "@/lib/utils";
import {PostStats} from "./";
import {PostComment, PostStats} from "./";
import { Card, CardContent, CardFooter, CardHeader } from "@/components/ui/card"
import { useState } from "react";

type PostCardProps = {
post: PostElement;
};

const PostCard = ({ post }: PostCardProps) => {
const { user } = useUserContext();

const [totalLikes, setTotalLikes] = useState(post.totalLikes || 0);

return (
<div className="post-card">
<div className="flex-between">
<div className="flex items-center gap-3">
<div className="post-card">
<Card className="w-full text-white bg-black border-none">
<CardHeader className="flex flex-row items-center gap-4 p-4">
<div className="flex items-center gap-3">
<Link to={`/profile/${post.userId}`}>
<img
src={
post.user?.profilePictureUrl ||
post.user?.profilePictureUrl ||
"/assets/icons/profile-placeholder.svg"
}
alt="creator"
Expand Down Expand Up @@ -52,30 +55,29 @@ const PostCard = ({ post }: PostCardProps) => {
height={20}
/>
</Link>
</div>

<Link to={`/posts/${post.id}`}>
<div className="py-5 small-medium lg:base-medium">
<p>{post.caption}</p>
<ul className="flex gap-1 mt-2">
{/* {post.tags.map((tag: string, index: string) => (
<li key={`${tag}${index}`} className="text-light-3 small-regular">
#{tag}
</li>
))} */}
</ul>
</div>
</CardHeader>
<CardContent className="p-0">
<img
src={post.imageUrl || "https://via.placeholder.com/150"}
alt="post image"
className="post-card_img "
/>
</CardContent>
<CardFooter className="flex flex-col items-start p-4">
{/* post stats */}
<PostStats post={post} userId={user.id} totalLikes={totalLikes} setTotalLikes={setTotalLikes} />
<p className="mb-2 font-semibold">{totalLikes} likes</p>
<p className="mb-2 text-sm">
<span className="font-semibold">muhammadrafi</span> Pengen ayam
</p>
{/* post comment */}
<PostComment postId={post.id} />
</CardFooter>
</Card>
</div>
)

<img
src={post.imageUrl || "/assets/icons/profile-placeholder.svg"}
alt="post image"
className="post-card_img"
/>
</Link>
}

<PostStats post={post} userId={user.id} />
</div>
);
};

export default PostCard;
102 changes: 102 additions & 0 deletions src/components/shared/PostComment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { useEffect, useState } from "react";
import { Button, Input } from "../ui";
import { useCreateComment, useDeleteComment, useGetPostById } from "@/lib/react-query/queries";
import { X } from "lucide-react";
import { useUserContext } from "@/context/useUserContext";

const PostComment = ({postId}:any) => {
const [comments, setComments] = useState<any>([]);
const [commentInput, setCommentInput] = useState<any>([]);
const [showAllComments, setShowAllComments] = useState(false);
const {user} = useUserContext()

// queries
const { mutate: createComment } = useCreateComment();
const {data} = useGetPostById(postId);
const { mutate: deleteComment } = useDeleteComment();
const commentsParsed = data?.data?.data?.comments.map((comment:any)=>{
return {
comment: comment.comment,
commentId: comment.id,
username: comment.user.username,
userId: comment.user.id,
}
});



const visibleComments = showAllComments ? comments : comments?.slice(0, 2)


useEffect(()=>{
setComments(commentsParsed);
},[data]);

const toggleComments = () => {
setShowAllComments(!showAllComments)
}


const handleCommentSubmit = (e: any) => {
e.preventDefault();
if (commentInput.trim() !== '') {
createComment({ postId, comment: commentInput });
setCommentInput('');
}
};

const handleDeleteComment = (commentId: string) => {
deleteComment(commentId);
console.log(commentsParsed)
};



return (
<div className="w-full">
{visibleComments?.map((comment:any, index:any) => (
<div className="flex items-center justify-between w-full mb-1 text-sm">
<p key={index+comment.userId} className="mb-1 text-sm">
<span className="font-semibold">{comment.username}</span> {comment.comment}
</p>

<Button
variant="ghost"
size="icon"
onClick={() => handleDeleteComment(comment.commentId)}
className="w-4 h-4 p-0 hover:bg-transparent focus:bg-transparent"
>
<X className={`w-3 h-3 text-gray-400 transition-colors hover:text-white
${!(user.id == comment.userId)&& "hidden"}`}
/>
</Button>

</div>

))}
{comments?.length > 2 && (
<Button
variant="link"
className="h-auto p-0 text-sm text-gray-400"
onClick={toggleComments}
>
{showAllComments ? "Hide comments" : `View all ${comments.length} comments`}
</Button>
)}
<form onSubmit={(e:any)=>{handleCommentSubmit(e)}} className="flex w-full gap-3 mt-4">
<Input
className="shad-input"
placeholder="Add a comment..."
type="text"
value={commentInput}
onChange={(e) => setCommentInput(e.target.value)}
/>
<Button type="submit" variant="ghost" className="self-center shad-button_primary">
Post
</Button>
</form>
</div>
)
}

export default PostComment
95 changes: 60 additions & 35 deletions src/components/shared/PostStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import Loader from "./Loader";
import { PostElement } from "@/types";
import { useLikePost, useUnlikePost } from "@/lib/react-query/queries";
import { useState } from "react";
import { Button } from "../ui";
import { Bookmark, Heart, MessageCircle, Send } from "lucide-react";



type PostCardProps = {
post: PostElement,
Expand All @@ -14,15 +18,15 @@ type PostCardProps = {



const PostStats = ({ post }: PostCardProps) => {
const PostStats = ({ post, totalLikes, setTotalLikes }: any) => {
const location = useLocation();
const [isLiked, setIsLiked] = useState(post.isLike);
const isDeletingSaved = false;
const isSavingPost = false;
const isSaved = false



const { mutate: likePost } = useLikePost();
const { mutate: unlikePost } = useUnlikePost();
// const { mutate: savePost, isPending: isSavingPost } = useSavePost();
Expand All @@ -39,14 +43,21 @@ const PostStats = ({ post }: PostCardProps) => {
// }, [currentUser]);

const handleLikePost = (
e: React.MouseEvent<HTMLImageElement, MouseEvent>
e: React.MouseEvent<HTMLButtonElement,MouseEvent>
) => {
e.stopPropagation();
setIsLiked(!isLiked);

if(isLiked){

unlikePost({ postId: post.id });
if(!(totalLikes == undefined)) setTotalLikes(totalLikes - 1);

}else{

likePost({ postId: post.id });
if(!(totalLikes == undefined)) setTotalLikes(totalLikes + 1);

}


Expand All @@ -64,40 +75,54 @@ const PostStats = ({ post }: PostCardProps) => {
? "w-full"
: "";

// hidden icon comment and send stats for explore page
const postDetailsPage = location.pathname.split("/")[1] === "posts"

const hiddentIconStats = location.pathname.startsWith("/explore") || postDetailsPage ? "hidden" : "";


// disable full width for explore page
const fullWidthExplore = location.pathname.startsWith("/explore") || postDetailsPage ? "" : "w-full";


return (
<div
className={`flex justify-between items-center z-20 ${containerStyles}`}>
<div className="flex gap-2 mr-5">
<img
src={`${
isLiked
? "/assets/icons/liked.svg"
: "/assets/icons/like.svg"
}`}
alt="like"
width={20}
height={20}
<div className={`flex ${fullWidthExplore} ${hiddentIconStats ==='hidden' ? "justify-end " : "justify-between"}`}>

<div className="flex gap-4 ">
<Button
variant="ghost"
size="icon"
onClick={(e) => handleLikePost(e)}
className="cursor-pointer"
/>
<p className="small-medium lg:base-medium">{post.totalLikes}</p>
</div>

<div className="flex gap-2">
{
isDeletingSaved || isSavingPost ? (<Loader/>) : (
<img
src={isSaved ? "/assets/icons/saved.svg" : "/assets/icons/save.svg"}
alt="share"
width={20}
height={20}
className="cursor-pointer"
onClick={(e) => handleSavePost(e)}
/>
)
}
className="hover:bg-transparent focus:bg-transparent group"
>
<Heart className={`h-6 w-6 ${isLiked ? 'fill-red text-red' : ''} transition-opacity
text-primary-500 group-hover:opacity-70
`}
/>
</Button>
<Button
variant="ghost"
size="icon"
className={`group ${hiddentIconStats} hover:bg-transparent focus:bg-transparent`}
>
<MessageCircle className="w-6 h-6 transition-opacity text-primary-500 group-hover:opacity-70" />
</Button>
<Button
variant="ghost"
size="icon"
className={`group ${hiddentIconStats} hover:bg-transparent focus:bg-transparent`}
>
<Send className="w-6 h-6 transition-opacity text-primary-500 group-hover:opacity-70" />
</Button>
</div>
</div>
<Button
variant="ghost"
size="icon"
className="group hover:bg-transparent focus:bg-transparent"
>
<Bookmark className="w-6 h-6 transition-opacity text-primary-500 group-hover:opacity-70" />
</Button>
</div>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { default as PostStats } from "./PostStats";
export { default as GridPostList } from "./GridPostList";
export { default as FileUploader } from "./FileUploader";
export { default as ProfileUploader } from "./ProfileUploader";

export { default as PostComment } from "./PostComment";
// export { default as UserCard } from "./UserCard";


Expand Down
Loading

0 comments on commit eae5bfe

Please sign in to comment.