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

Enhanced Add to favourite feature #570

Merged
merged 3 commits into from
Jun 26, 2023
Merged
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
25 changes: 17 additions & 8 deletions src/components/Post/PostNav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
import { IconButton, Typography } from "@mui/material";
import React, { useState } from "react";

import { FaSave } from "react-icons/fa";
import BookmarkBorderIcon from "@mui/icons-material/BookmarkBorder";
import BookmarksIcon from "@mui/icons-material/Bookmarks";
import Flexbetween from "../../reusableComponents/Flexbetween";
import { useSnackbar } from "notistack";

Expand All @@ -25,6 +26,8 @@ const PostNav = ({
}) => {
const { enqueueSnackbar } = useSnackbar();
const [Open, setOpen] = useState(false);
const [isSaved, setisSaved] = useState(false);

const save = async () => {
const localStoragePosts = JSON.parse(localStorage.getItem("posts")) || [];
const postIdExists = localStoragePosts.includes(postId);
Expand All @@ -42,6 +45,18 @@ const PostNav = ({
}
};

const handleToggleFavorite = () => {
setisSaved(!isSaved);
};

const renderFavoriteIcon = () => {
if (isSaved) {
return <BookmarksIcon onClick={handleToggleFavorite} />;
} else {
return <BookmarkBorderIcon onClick={handleToggleFavorite} />;
}
};

return (
<Flexbetween gap={!fullScreen && "1.6rem"}>
<Flexbetween sx={{ cursor: "pointer" }} onClick={likesHandler}>
Expand Down Expand Up @@ -82,13 +97,7 @@ const PostNav = ({
</Flexbetween>

<Flexbetween sx={{ cursor: "pointer" }} onClick={save}>
<IconButton>
<FaSave
onClick={save}
style={{ cursor: "pointer", fontSize: "22px" }}
className="post_button"
/>
</IconButton>
<IconButton>{renderFavoriteIcon()}</IconButton>
<Typography fontSize={14}>Save</Typography>
</Flexbetween>
</Flexbetween>
Expand Down