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

59 get likes posts in likes tab in profile page #60

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "NODE_ENV=development vite",
"build": "NODE_ENV=production tsc && vite build",
"dev": "cross-env NODE_ENV=development vite",
"build": "cross-env NODE_ENV=development && vite",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint:fix": "eslint src --ext ts,tsx --fix",
"preview": "vite preview",
Expand Down
114 changes: 72 additions & 42 deletions src/components/MyProfile/Posts/SinglePost/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React from 'react';
import { Avatar } from '@mui/material';

import { RiBookmarkLine, RiChat1Line, RiHeart3Line, RiShareLine } from 'react-icons/ri';
import { toast } from 'react-toastify';
import { useMutation } from 'react-query';
import {
PostContainer,
PostContentWithImg,
PostInteraction,
PostUserInfo,
UserNameWithPublish,
} from './style';
import { likesRoutes } from '../../../../services';
import { ReqError } from '../../../../utils';

interface IPost {
id: number;
alt: string;
src: string;
username: string;
Expand All @@ -19,9 +25,11 @@ interface IPost {
postImgAlt: string;
likesCount: string;
commentsCount: string;
isLiked: boolean;
}

const SinglePost = ({
id,
alt,
src,
username,
Expand All @@ -31,48 +39,70 @@ const SinglePost = ({
postImgAlt,
likesCount,
commentsCount,
}: IPost) => (
<PostContainer>
<PostUserInfo>
<Avatar alt={alt} src={src} sx={{ width: 50, height: 50 }} />
<UserNameWithPublish>
<p>{username}</p>
<p>{publishDate}</p>
</UserNameWithPublish>
</PostUserInfo>
<PostContentWithImg>
<p>{postContent.length > 100 ? postContent : <strong>{postContent}</strong>}</p>
{postImg ? <img src={postImg} alt={postImgAlt} /> : null}
<PostInteraction>
<span>
{likesCount}
{' '}
<span>Likes</span>
</span>
<span>
{commentsCount}
{' '}
<span>Comments</span>
</span>
</PostInteraction>
<PostInteraction>
<div>
<button type="button">
<RiHeart3Line />
</button>
<button type="button">
<RiChat1Line />
</button>
isLiked,
}: IPost) => {
const [isLike, setIsLike] = React.useState(isLiked);
const [likes, setLikes] = React.useState(likesCount);
const { mutate: like } = useMutation((postId: number) => likesRoutes.send(postId), {
onSuccess: (data) => {
if (data.status === 200) {
setLikes((prev) => `${Number(prev) - 1}`);
setIsLike(false);
} else if (data.status === 201) {
setLikes((prev) => `${Number(prev) + 1}`);
setIsLike(true);
}
},
onError: (err: ReqError) => {
toast.error(err.response.data.data.message);
},
});

const handleLike = (postId: number) => {
like(postId);
};
return (
<PostContainer>
<PostUserInfo>
<Avatar alt={alt} src={src} sx={{ width: 50, height: 50 }} />
<UserNameWithPublish>
<p>{username}</p>
<p>{publishDate}</p>
</UserNameWithPublish>
</PostUserInfo>
<PostContentWithImg>
<p>{postContent.length > 100 ? postContent : <strong>{postContent}</strong>}</p>
{postImg ? <img src={postImg} alt={postImgAlt} /> : null}
<PostInteraction>
<span>
{likes}
{' '}
<span>Likes</span>
</span>
<span>
{commentsCount}
{' '}
<span>Comments</span>
</span>
</PostInteraction>
<PostInteraction>
<div>
<button type="button" onClick={() => handleLike(id)}>
<RiHeart3Line style={{ color: isLike ? 'red' : '#8d8d8d' }} />
</button>
<button type="button">
<RiChat1Line />
</button>
<button type="button">
<RiShareLine />
</button>
</div>
<button type="button">
<RiShareLine />
<RiBookmarkLine />
</button>
</div>
<button type="button">
<RiBookmarkLine />
</button>
</PostInteraction>
</PostContentWithImg>
</PostContainer>
);

</PostInteraction>
</PostContentWithImg>
</PostContainer>
);
};
export default SinglePost;
6 changes: 6 additions & 0 deletions src/components/MyProfile/Posts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const PostsWrapper = styled.div`
`;

interface IPost {
id: number;
alt: string;
src: string;
username: string;
publishDate: string;
isLiked:boolean;
postContent: string;
postImg: string;
postImgAlt: string;
Expand All @@ -19,6 +21,7 @@ interface IPost {
}

const Posts = ({
id,
alt,
src,
username,
Expand All @@ -28,9 +31,11 @@ const Posts = ({
postImgAlt,
likesCount,
commentsCount,
isLiked,
}: IPost) => (
<PostsWrapper>
<SinglePost
id={id}
alt={alt}
src={src}
username={username}
Expand All @@ -40,6 +45,7 @@ const Posts = ({
postImgAlt={postImgAlt}
likesCount={likesCount}
commentsCount={commentsCount}
isLiked={isLiked}
/>
</PostsWrapper>
);
Expand Down
76 changes: 43 additions & 33 deletions src/components/MyProfile/Tabs/ProfileDetailsWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { TabPanel } from '@mui/lab';
import { Grid, styled } from '@mui/material';
import { Grid } from '@mui/material';
import { useQuery } from 'react-query';
import Posts from '../Posts';
import ProfileDetails from '../ProfileDetails';
import { CohortCard } from '../..';
import { profileRoutes } from '../../../services';
import { cohortsRoutes, profileRoutes } from '../../../services';
import convertDate from '../../../utils/helpers/convertDate';

interface ICohorts {
id: number;
thumbnail: string;
cohort_name: string;
end_date: string;
start_date: string;
members: number;
}

interface IPost {
avatar: string;
commentsCount: string;
Expand All @@ -19,10 +28,15 @@ interface IPost {
updatedAt: string;
userId: number;
username: string;
isLiked: boolean;
}

const ProfileDetailsWrapper = () => {
const { data } = useQuery({
const { data: myCohorts } = useQuery({
queryKey: ['myCohorts'],
queryFn: async () => cohortsRoutes.my(),
});
const { data: posts } = useQuery({
queryKey: ['my-posts'],
queryFn: async () => profileRoutes.posts(),
});
Expand All @@ -40,10 +54,11 @@ const ProfileDetailsWrapper = () => {
sx={{ paddingTop: '3%' }}
>
<Grid item xs={11} sm={11} md={11} lg={11}>
<TabPanelStyle value="1">
{data?.data.data.posts.map((post: IPost) => (
<TabPanel value="1">
{posts?.data.data.posts.map((post: IPost) => (
<Posts
alt={post.avatar}
key={post.id}
alt={post.username}
src={post.avatar}
username={post.username}
publishDate={convertDate(post.createdAt)}
Expand All @@ -52,31 +67,28 @@ const ProfileDetailsWrapper = () => {
postImgAlt={post.image}
likesCount={post.likesCount}
commentsCount={post.commentsCount}
isLiked={post.isLiked}
id={post.id}
/>
))}
</TabPanelStyle>
<TabPanelStyle value="2">
</TabPanel>
<TabPanel value="2">
<ProfileDetails />
</TabPanelStyle>
<TabPanelStyle value="3">
<CohortCard
alt="G13"
thumbnail="https://intranet.cera-theme.com/wp-content/themes/cera/assets/images/avatars/user-group.png"
cohortName="G13"
cohortMembers={20}
startDate="20 May"
endDate="30 May"
/>
<CohortCard
alt="G13"
thumbnail="https://intranet.cera-theme.com/wp-content/themes/cera/assets/images/avatars/user-group.png"
cohortName="G13"
cohortMembers={20}
startDate="20 May"
endDate="30 May"
/>
</TabPanelStyle>
<TabPanelStyle value="4">
</TabPanel>
<TabPanel value="3">
{myCohorts?.data.data.cohorts.map((cohort: ICohorts) => (
<CohortCard
key={cohort.id}
alt={cohort.cohort_name}
thumbnail={cohort.thumbnail}
cohortName={cohort.cohort_name}
cohortMembers={cohort.members}
startDate={cohort.start_date}
endDate={cohort.end_date}
/>
))}
</TabPanel>
<TabPanel value="4">
<Posts
alt="K"
src="K"
Expand All @@ -87,8 +99,10 @@ const ProfileDetailsWrapper = () => {
postImgAlt="K"
likesCount="10"
commentsCount="10"
id={0}
isLiked={false}
/>
</TabPanelStyle>
</TabPanel>
</Grid>
<Grid item xs={4} sm={4} md={4} lg={4}>
<h2>Members Bar Hereeee</h2>
Expand All @@ -97,8 +111,4 @@ const ProfileDetailsWrapper = () => {
);
};

const TabPanelStyle = styled(TabPanel)`
padding: 0;
`;

export default ProfileDetailsWrapper;
8 changes: 8 additions & 0 deletions src/services/httpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class HttpService {
return apiClient.post(this.endpoint, body);
}

async send(id: number) {
return apiClient.post(`${this.endpoint}/${id}`);
}

async update<T extends Global>(body: T) {
return apiClient.put(`${this.endpoint}/${body.id}`, body);
}
Expand All @@ -73,6 +77,10 @@ class HttpService {
async posts() {
return apiClient.get(`${this.endpoint}/posts`);
}

async my() {
return apiClient.get(`${this.endpoint}/my`);
}
}

const create = (endpoint: string) => new HttpService(endpoint);
Expand Down