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

Designed post card #228

Merged
merged 9 commits into from
Nov 18, 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/assets/icons/comment.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/icons/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/pages/blog/BlogPage.styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Box, styled } from "@mui/material";

export const BoxStyled = styled(Box)({
marginTop: "100px",
backgroundColor: "#F8EBE1",
});
8 changes: 5 additions & 3 deletions src/pages/blog/BlogPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useSearchParams } from "react-router-dom";

import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";

import AppHelmet from "@/components/app-helmet/AppHelmet";
import BlogTabLayout from "@/layouts/blog-tab-layout/BlogTabLayout";
import { BoxStyled } from "@/pages/blog/BlogPage.styled";
import PostCard from "@/pages/blog/components/post-card/PostCard";

export default function BlogPage() {
const [searchParams] = useSearchParams();
Expand All @@ -15,11 +16,12 @@ export default function BlogPage() {
title="Blog"
description="Blog with posts from experts in our field"
>
<Box sx={{ marginTop: "100px" }}>
<BoxStyled>
<BlogTabLayout>
<Typography>Hello {searchParams.get("category")}</Typography>
</BlogTabLayout>
</Box>
<PostCard />
</BoxStyled>
</AppHelmet>
);
}
51 changes: 51 additions & 0 deletions src/pages/blog/components/post-card-skeleton/PostCardSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Box from "@mui/material/Box";
import Skeleton from "@mui/material/Skeleton";

import imagePlaceholder from "@/assets/icons/image-placeholder.svg?url";
import ShareIcon from "@/assets/icons/three-dots-vertical.svg";

import {
BoxImageStyled,
BoxStyled,
ContentBox,
ImgStyled,
PostContentBox,
PostInfoBox,
ShareButtonStyled,
StatsBox,
} from "@/pages/blog/components/post-card/PostCard.styled";
import LikeWidgetSkeleton from "@/pages/post/components/like-widget/LikeWidgetSkeleton";

export default function PostCardSkeleton() {
return (
<BoxStyled>
<BoxImageStyled isLoading={true} shouldShowImagePlaceholder={true}>
<ImgStyled src={imagePlaceholder} alt="Post image placeholder" />
</BoxImageStyled>
<ContentBox>
<PostInfoBox>
<Box display="flex" gap={1}>
<Skeleton variant="circular" width={40} height={40} />
<Box>
<Skeleton width={100} />
<Skeleton width={250} />
</Box>
</Box>
<ShareButtonStyled disableRipple area-label="share">
<ShareIcon />
</ShareButtonStyled>
</PostInfoBox>
<PostContentBox>
<Skeleton width="100%" height={200} />
</PostContentBox>
<StatsBox>
<Box display="flex" alignItems="center" gap={2}>
<Skeleton width={50} />
<Skeleton width={50} />
</Box>
<LikeWidgetSkeleton />
</StatsBox>
</ContentBox>
</BoxStyled>
);
}
56 changes: 56 additions & 0 deletions src/pages/blog/components/post-card/PostCard.styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Box, IconButton, styled } from "@mui/material";

type BoxImageStyledProps = {
isLoading: boolean;
shouldShowImagePlaceholder: boolean;
};

export const BoxStyled = styled("li")({
backgroundColor: "#fff",
maxWidth: "27rem",
listStyleType: "none",
"& a:active": {
color: "#000",
},
});
export const ImgStyled = styled("img")({
width: "100%",
});
export const ContentBox = styled(Box)({
display: "flex",
flexDirection: "column",
gap: "1rem",
padding: "1.7rem",
});
export const PostInfoBox = styled(Box)({
display: "flex",
justifyContent: "space-between",
});
export const PostContentBox = styled(Box)({
paddingBottom: "1rem",
borderBottom: "1px solid rgba(3,3,3,0.2)",
});
export const StatsBox = styled(Box)({
display: "flex",
justifyContent: "space-between",
});
export const ShareButtonStyled = styled(IconButton)({
"&:hover": { backgroundColor: "transparent" },
"&:active": { backgroundColor: "transparent" },
});
export const BoxImageStyled = styled(Box, {
shouldForwardProp: prop =>
prop !== "isLoading" && prop !== "shouldShowImagePlaceholder",
})<BoxImageStyledProps>(({ theme, isLoading, shouldShowImagePlaceholder }) => ({
minWidth: "170px",
minHeight: "170px",
backgroundColor: theme.palette.primary.main,
boxShadow: "0 4px 8px rgba(0, 0, 0, 0.1)",
display: "flex",
justifyContent: "center",
alignItems: "center",
opacity: isLoading ? 0.3 : 1,
"& img": {
width: shouldShowImagePlaceholder ? "100px" : "100%",
},
}));
155 changes: 155 additions & 0 deletions src/pages/blog/components/post-card/PostCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { MouseEvent } from "react";
import { Link } from "react-router-dom";

import Avatar from "@mui/material/Avatar";
import Box from "@mui/material/Box";
import Icon from "@mui/material/Icon";
import Typography from "@mui/material/Typography";
import useMediaQuery from "@mui/material/useMediaQuery";
import { format } from "date-fns";

import CommentIcon from "@/assets/icons/comment.svg";
import EyeIcon from "@/assets/icons/eye.svg";
import imagePlaceholder from "@/assets/icons/image-placeholder.svg?url";
import ShareIcon from "@/assets/icons/three-dots-vertical.svg";

import { routePaths } from "@/constants/routePaths";
import useLazyImage from "@/hooks/use-lazy-mage/useLazyImage";
import PostCardSkeleton from "@/pages/blog/components/post-card-skeleton/PostCardSkeleton";
import {
BoxImageStyled,
BoxStyled,
ContentBox,
ImgStyled,
PostContentBox,
PostInfoBox,
ShareButtonStyled,
StatsBox,
} from "@/pages/blog/components/post-card/PostCard.styled";
import LikeWidget from "@/pages/post/components/like-widget/LikeWidget";
import theme from "@/theme/theme";

type Post =
| {
id: number;
author: {
id: number;
username: string;
role: string;
};
createdAt: string;
updatedAt: string;
estimatedReadTime: number;
image: string;
title: string;
summary: string;
viewsCount: number;
commentsCount: number;
}
| undefined;

// temporary post for ui
const post: Post = {
id: 1,
author: {
id: 1,
username: "Admin",
role: "ADMIN",
},
createdAt: "2024-11-14T07:35:34.842Z",
updatedAt: "2024-11-14T07:35:34.842Z",
estimatedReadTime: 1,
image:
"https://static.wixstatic.com/media/84770f_170242c7269d4ba2ba8ad50591e1a1e8~mv2_d_4500_2992_s_4_2.jpg/v1/fill/w_925,h_615,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/84770f_170242c7269d4ba2ba8ad50591e1a1e8~mv2_d_4500_2992_s_4_2.jpg",
title: "Why are Facials a Must for the Modern Woman",
summary:
"Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading...",
viewsCount: 0,
commentsCount: 0,
};
// let post: Post = undefined;

export default function PostCard() {
const isSmallScreen = useMediaQuery(theme.breakpoints.down("sm"));

if (post === undefined) {
return <PostCardSkeleton />;
}

const [image, { hasError }] = useLazyImage({
src: post.image,
placeholderSrc: imagePlaceholder,
});

function handleSharePost(event: MouseEvent<HTMLButtonElement>) {
event.preventDefault();
// TODO: add share functionality
}

return (
<BoxStyled>
<Link to={`${routePaths.posts.path}/${post.id}`}>
<BoxImageStyled isLoading={false} shouldShowImagePlaceholder={hasError}>
<ImgStyled src={image} alt={`'${post?.title}' post image`} />
</BoxImageStyled>
<ContentBox>
<PostInfoBox>
<Box display="flex" gap={1}>
mhevyk marked this conversation as resolved.
Show resolved Hide resolved
<Link to={`/members/${post.author.id}`}>
<Avatar />
</Link>
<Box>
<Link to={`/members/${post.author.id}`}>
<Typography>{post.author.username}</Typography>
</Link>
<Typography>
{format(post.createdAt, "MMM d, yyyy")} &middot;{" "}
{post.estimatedReadTime} min read
</Typography>
</Box>
</Box>
<ShareButtonStyled
onClick={handleSharePost}
disableRipple
area-label="share"
>
<ShareIcon />
</ShareButtonStyled>
</PostInfoBox>
<PostContentBox>
<Typography variant="h2" fontSize={26} fontWeight={800}>
{post.title}
</Typography>
<Typography
component={"p"}
variant={"paragraph"}
marginTop={"1rem"}
>
{post.summary}
</Typography>
</PostContentBox>
<StatsBox>
<Box display="flex" alignItems="center" gap={2}>
{isSmallScreen ? (
<>
<Icon component={EyeIcon} />
<Typography fontSize={20}>{post.viewsCount}</Typography>
<Icon component={CommentIcon} />
<Typography fontSize={20}>{post.commentsCount}</Typography>
</>
) : (
<>
<Typography fontSize={12}>{post.viewsCount} views</Typography>
<Typography fontSize={12}>
{post.commentsCount} comments
</Typography>
</>
)}
</Box>
<LikeWidget postId={post.id} />
</StatsBox>
</ContentBox>
</Link>
</BoxStyled>
);
}
8 changes: 7 additions & 1 deletion src/pages/post/components/like-button/LikeButton.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export const likeCircleAnimation = keyframes`
type LikeIconProps = {
isLiked?: boolean;
isDisabled?: boolean;
isSmallScreen?: boolean;
};

export const LikeIcon = styled(HeartIcon, {
shouldForwardProp: prop => prop !== "isLiked" && prop !== "isDisabled",
shouldForwardProp: prop =>
prop !== "isLiked" && prop !== "isDisabled" && prop !== "isSmallScreen",
})<LikeIconProps>(props => {
let fillColor = "none";
let strokeColor = theme.palette.error.light;
Expand All @@ -72,6 +74,10 @@ export const LikeIcon = styled(HeartIcon, {
...(props.isLiked && {
animation: `${likeAnimation} .5s;`,
}),
...(props.isSmallScreen && {
width: "26px",
height: "26px",
}),
};
});

Expand Down
11 changes: 8 additions & 3 deletions src/pages/post/components/like-button/LikeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from "react";
import { MouseEvent, useRef, useState } from "react";

import IconButton from "@mui/material/IconButton";

Expand All @@ -12,6 +12,8 @@ import {
} from "@/pages/post/components/like-button/LikeButton.styled";
import { useUserStore } from "@/store/user/userStore";
import showSnackbar from "@/utils/show-snackbar/showSnackbar";
import { useMediaQuery } from "@mui/material";
import theme from "@/theme/theme";

type LikeButtonProps = {
postId: number;
Expand Down Expand Up @@ -50,7 +52,8 @@ export default function LikeButton({
},
});

const handlePostLike = () => {
const handlePostLike = (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
if (!isAuthenticated) {
showSnackbar({
message: "Please sign in to like the post",
Expand Down Expand Up @@ -97,6 +100,8 @@ export default function LikeButton({
return handleRequestAbort;
}, [debouncedIsLiked]);

const isSmallScreen = useMediaQuery(theme.breakpoints.down("sm"));

return (
<LikeButtonContainer>
{likesCount > 0 && (
Expand All @@ -107,7 +112,7 @@ export default function LikeButton({
onClick={handlePostLike}
data-testid="heart-button"
>
<LikeIcon isLiked={isLiked} />
<LikeIcon isSmallScreen={isSmallScreen} isLiked={isLiked} />
</IconButton>
</LikeButtonContainer>
);
Expand Down
Loading