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

feat: Add bookmark feature to posts #61

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Style Share is a collaborative platform designed to streamline the process of cr
2. Run the following commands in the backend folder

```sh
npm install
npm install
npm run build
npm run dev
```
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ import AuthenticatedRoute from "./components/AuthenticatedRoute";
import Profile from "./pages/Profile";
import React from "react";
import Loader from "./components/Loader";
import BookmarkList from './components/BookmarkList';
// import axios from "axios";
// axios.defaults.baseURL = "http://localhost:3001/";

function App() {
const [bookmarks, setBookmarks] = useState<string[]>([]);

const handleBookmark = (item: string) => {
setBookmarks((prevBookmarks) =>
prevBookmarks.includes(item)
? prevBookmarks.filter((bookmark) => bookmark !== item)
: [...prevBookmarks, item]
);
};
return (
<BrowserRouter>
<RecoilRoot>
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/components/BookmarkButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { useState } from 'react';

interface BookmarkButtonProps {
item: string;
onBookmark: (item: string) => void;
isBookmarked: boolean;
}

const BookmarkButton: React.FC<BookmarkButtonProps> = ({ item, onBookmark, isBookmarked }) => {
return (
<button onClick={() => onBookmark(item)}>
{isBookmarked ? 'Remove Bookmark' : 'Add Bookmark'}
</button>
);
};

export default BookmarkButton;
23 changes: 23 additions & 0 deletions frontend/src/components/BookmarkList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';

interface BookmarkListProps {
bookmarks: string[];
onRemove: (item: string) => void;
}

const BookmarkList: React.FC<BookmarkListProps> = ({ bookmarks, onRemove }) => {
return (
<div>
<h2>Bookmarked Items</h2>
<ul>
{bookmarks.map((bookmark, index) => (
<li key={index}>
{bookmark} <button onClick={() => onRemove(bookmark)}>Remove</button>
</li>
))}
</ul>
</div>
);
};

export default BookmarkList;
19 changes: 16 additions & 3 deletions frontend/src/pages/Posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import axios from 'axios';
import { IPost } from '../types';
import Loader from '../components/Loader';
import PostCard from '../components/PostCard';
import BookmarkButton from '../components/BookmarkButton';
import React from 'react';

const Posts = () => {
interface PostsProps {
onBookmark: (item: string) => void;
bookmarks: string[];
}

const Posts: React.FC<PostsProps> = ({ onBookmark, bookmarks }) => {
const [posts, setPosts] = useState<IPost[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
Expand Down Expand Up @@ -68,7 +75,7 @@ const Posts = () => {
);

if (loading) {
return <Loader/>;
return <Loader />;
}

if (error) {
Expand Down Expand Up @@ -129,7 +136,13 @@ const Posts = () => {
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 w-full">
{filteredPosts.map((post) => (
<PostCard post={post} />
<PostCard key={post.id} post={post}>
<BookmarkButton
item={post.id}
onBookmark={onBookmark}
isBookmarked={bookmarks.includes(post.id)}
/>
</PostCard>
))}
</div>
</div>
Expand Down