Skip to content

Commit f26be71

Browse files
committed
refactor single post page
1 parent 8b64d38 commit f26be71

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

client/src/components/PostActions.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ interface PostActionsProps {
88
postId: string;
99
userLike: boolean;
1010
addCommentRef: React.RefObject<HTMLInputElement>;
11+
className?: string;
1112
}
1213

1314
const PostActions: React.FC<PostActionsProps> = ({
1415
postId,
1516
userLike,
1617
addCommentRef,
18+
className,
1719
}) => {
1820
const [liked, setLiked] = useState(false);
1921

@@ -24,7 +26,7 @@ const PostActions: React.FC<PostActionsProps> = ({
2426
const history = useHistory();
2527
const location = useLocation();
2628
return (
27-
<div className='flex items-center pb-2'>
29+
<div className={`flex items-center pb-2 ${className}`}>
2830
<LikeButton postId={postId} liked={liked} setLiked={setLiked}>
2931
{liked ? (
3032
<FaHeart

client/src/routes/SinglePost.tsx

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
import dayjs from 'dayjs';
2-
import React, { useEffect, useRef, useState } from 'react';
3-
import { FaHeart, FaRegHeart } from 'react-icons/fa';
4-
import { MdMoreHoriz } from 'react-icons/md';
5-
import { RiChat1Line } from 'react-icons/ri';
2+
import React, { useEffect, useRef } from 'react';
63
import { Link, useParams } from 'react-router-dom';
74
import Avatar from '../components-ui/Avatar';
85
import Card from '../components-ui/Card';
96
import Container from '../components-ui/Container';
7+
import Spinner from '../components-ui/Spinner';
108
import AddComment from '../components/AddComment';
11-
import LikeButton from '../components/LikeButton';
12-
import { useGetSinglePostQuery } from '../generated/graphql';
9+
import PostActions from '../components/PostActions';
10+
import PostOptions from '../components/PostOptions';
11+
import { Post, useGetSinglePostQuery } from '../generated/graphql';
1312

1413
const SinglePost = () => {
15-
const [liked, setLiked] = useState(false);
1614
const { postId }: any = useParams();
1715
const { data, loading } = useGetSinglePostQuery({ variables: { postId } });
1816

1917
// const { me } = apolloClient.readQuery({ query: MeDocument });
2018
const addCommentRef = useRef<HTMLInputElement>(null);
2119

22-
useEffect(() => {
23-
setLiked(data?.getSinglePost?.userLike || false);
24-
}, [setLiked, data]);
25-
2620
useEffect(() => {
2721
window.scrollTo({ top: 0 });
2822
}, []);
2923

30-
if (loading) return <div>Loading...</div>;
24+
if (loading) return <Spinner />;
3125
if (data && data.getSinglePost) {
3226
const {
3327
id,
@@ -55,9 +49,7 @@ const SinglePost = () => {
5549
</Link>
5650
</div>
5751
{/* TODO Icon Button */}
58-
<button>
59-
<MdMoreHoriz size='1.5em' />
60-
</button>
52+
<PostOptions post={data.getSinglePost as Post} />
6153
</header>
6254
{/* Media */}
6355
<div className='md:h-full md:mr-80'>
@@ -130,30 +122,16 @@ const SinglePost = () => {
130122
</div>
131123
{/* Like And Comment Button */}
132124
<div>
133-
<div className='flex items-center px-3 py-2 border-t border-gray-300'>
134-
<LikeButton postId={id} liked={liked} setLiked={setLiked}>
135-
{userLike ? (
136-
<FaHeart
137-
size='1.5em'
138-
className='mr-2 text-red-600 duration-150 transform cursor-pointer active:scale-110'
139-
/>
140-
) : (
141-
<FaRegHeart
142-
size='1.5em'
143-
className='mr-2 duration-150 transform cursor-pointer active:scale-110'
144-
/>
145-
)}
146-
</LikeButton>
147-
<RiChat1Line
148-
size='1.6em'
149-
onClick={() => {
150-
addCommentRef.current?.focus();
151-
}}
152-
className='cursor-pointer'
153-
/>
154-
</div>
125+
<PostActions
126+
className='p-3'
127+
postId={id}
128+
addCommentRef={addCommentRef}
129+
userLike={userLike}
130+
/>
155131
{/* Like Count */}
156-
<p className='px-3 font-semibold'>{likeCount} likes</p>
132+
<p className='px-3 font-semibold'>
133+
{likeCount} like{likeCount === 1 ? '' : 's'}
134+
</p>
157135
{/* TimeStamp */}
158136
<Link
159137
to={`/p/${id}`}

0 commit comments

Comments
 (0)