-
Notifications
You must be signed in to change notification settings - Fork 73
unified saved hub and bookmark refactor (Closes #132) #138
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| "use client"; | ||
|
|
||
| import React, { useEffect, useState } from "react"; | ||
| import Link from "next/link"; | ||
| import CourseCard from "@/components/molecules/dashboard/cards/courseCard"; | ||
| import LibraryBookCard from "@/components/molecules/dashboard/cards/libraryCard"; | ||
| import CourseCardSkeleton from "@/components/atoms/skeletons/CourseCardSkeleton"; | ||
| import LibraryBookSkeleton from "@/components/atoms/skeletons/LibraryBookSkeleton"; | ||
| import NetworkErrorComp from "@/components/molecules/errors/NetworkError"; | ||
| import Button from "@/components/atoms/form/Button"; | ||
| import { getBookmarkedCourses } from "@/lib/actions/courses/bookmark-course"; | ||
| import { getBookmarkedBooks } from "@/lib/actions/library/bookmark-book"; | ||
| import { Bookmark, LaptopMinimal, Book } from "lucide-react"; | ||
|
|
||
| export default function SavedPage() { | ||
| const [activeTab, setActiveTab] = useState("courses"); // "courses" | "books" | ||
| const [courses, setCourses] = useState([]); | ||
| const [books, setBooks] = useState([]); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(false); | ||
|
|
||
| const fetchSavedItems = async () => { | ||
| setLoading(true); | ||
| setError(false); | ||
| try { | ||
| const [coursesRes, booksRes] = await Promise.allSettled([ | ||
| getBookmarkedCourses(), | ||
| getBookmarkedBooks(), | ||
| ]); | ||
|
|
||
| if (coursesRes.status === "fulfilled") { | ||
| const courseData = coursesRes.value; | ||
| setCourses(courseData?.bookmarks || (Array.isArray(courseData) ? courseData : [])); | ||
| } | ||
|
|
||
| if (booksRes.status === "fulfilled") { | ||
| const bookData = booksRes.value; | ||
| setBooks(bookData?.bookmarks || (Array.isArray(bookData) ? bookData : [])); | ||
| } | ||
|
|
||
| if (coursesRes.status === "rejected" && booksRes.status === "rejected") { | ||
| setError(true); | ||
| } | ||
| } catch (_err) { | ||
| setError(true); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| fetchSavedItems(); | ||
| }, []); | ||
|
|
||
| const handleCourseBookmarkChange = (isBookmarked, courseId) => { | ||
| if (!isBookmarked) { | ||
| setCourses((prev) => prev.filter((c) => c._id !== courseId)); | ||
| } | ||
| }; | ||
|
|
||
| const handleBookBookmarkChange = (isBookmarked, bookId) => { | ||
| if (!isBookmarked) { | ||
| setBooks((prev) => prev.filter((b) => b._id !== bookId)); | ||
| } | ||
| }; | ||
|
|
||
| if (error) { | ||
| return ( | ||
| <NetworkErrorComp | ||
| errMsg="Failed to load saved items. Please try again." | ||
| reset={() => fetchSavedItems()} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="bg-muted min-h-full w-full p-5"> | ||
| {/* Header */} | ||
| <div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-6"> | ||
| <div> | ||
| <h2 className="text-2xl font-bold flex items-center gap-2"> | ||
| <Bookmark className="w-6 h-6 text-accent fill-accent" /> | ||
| My Saved Hub | ||
| </h2> | ||
| <p className="text-muted-foreground text-sm mt-1"> | ||
| Access all your bookmarked courses and books in one place | ||
| </p> | ||
| </div> | ||
|
|
||
| {/* Tabs Toggle */} | ||
| <div className="flex gap-2 bg-background/60 p-1.5 rounded-full border shadow-sm"> | ||
| <button | ||
| type="button" | ||
| onClick={() => setActiveTab("courses")} | ||
| className={`flex items-center gap-2 px-4 py-2 text-sm font-semibold rounded-full transition-all cursor-pointer ${ | ||
| activeTab === "courses" | ||
| ? "bg-accent text-white shadow" | ||
| : "text-muted-foreground hover:text-foreground" | ||
| }`} | ||
| > | ||
| <LaptopMinimal className="w-4 h-4" /> | ||
| Courses ({courses.length}) | ||
| </button> | ||
|
|
||
| <button | ||
| type="button" | ||
| onClick={() => setActiveTab("books")} | ||
| className={`flex items-center gap-2 px-4 py-2 text-sm font-semibold rounded-full transition-all cursor-pointer ${ | ||
| activeTab === "books" | ||
| ? "bg-accent text-white shadow" | ||
| : "text-muted-foreground hover:text-foreground" | ||
| }`} | ||
| > | ||
| <Book className="w-4 h-4" /> | ||
| Books ({books.length}) | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Content Section */} | ||
| <div className="mt-6"> | ||
| {loading ? ( | ||
| <div className="grid gap-4 grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3"> | ||
| {[...Array(6)].map((_, idx) => | ||
| activeTab === "courses" ? ( | ||
| <CourseCardSkeleton key={`skeleton-course-${idx}`} /> | ||
| ) : ( | ||
| <LibraryBookSkeleton key={`skeleton-book-${idx}`} /> | ||
| ) | ||
| )} | ||
| </div> | ||
| ) : activeTab === "courses" ? ( | ||
| courses.length === 0 ? ( | ||
| <div className="flex flex-col items-center justify-center text-center py-20 bg-background/40 rounded-2xl border border-dashed p-8"> | ||
| <LaptopMinimal className="w-12 h-12 text-muted-foreground/50 mb-3" /> | ||
| <h3 className="text-lg font-semibold mb-1">No saved courses yet</h3> | ||
| <p className="text-muted-foreground text-sm max-w-md mb-6"> | ||
| Explore our catalog and bookmark courses you are interested in to view them here later. | ||
| </p> | ||
| <Button to="/dashboard/courses" round className="bg-accent text-white"> | ||
| Browse Courses | ||
| </Button> | ||
| </div> | ||
| ) : ( | ||
| <div className="grid gap-4 grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3"> | ||
| {courses.map((course) => ( | ||
| <CourseCard | ||
| key={course._id} | ||
| course={course} | ||
| initialIsBookmarked={true} | ||
| onBookmarkChange={(isBookmarked) => | ||
| handleCourseBookmarkChange(isBookmarked, course._id) | ||
| } | ||
| /> | ||
| ))} | ||
| </div> | ||
| ) | ||
| ) : books.length === 0 ? ( | ||
| <div className="flex flex-col items-center justify-center text-center py-20 bg-background/40 rounded-2xl border border-dashed p-8"> | ||
| <Book className="w-12 h-12 text-muted-foreground/50 mb-3" /> | ||
| <h3 className="text-lg font-semibold mb-1">No saved books yet</h3> | ||
| <p className="text-muted-foreground text-sm max-w-md mb-6"> | ||
| Browse our Islamic library and save books to build your personal reading list. | ||
| </p> | ||
| <Button to="/dashboard/library" round className="bg-accent text-white"> | ||
| Browse Library | ||
| </Button> | ||
| </div> | ||
| ) : ( | ||
| <div className="grid gap-4 grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3"> | ||
| {books.map((book) => ( | ||
| <LibraryBookCard | ||
| key={book._id} | ||
| book={book} | ||
| initialIsBookmarked={true} | ||
| onBookmarkChange={(isBookmarked) => | ||
| handleBookBookmarkChange(isBookmarked, book._id) | ||
| } | ||
| /> | ||
| ))} | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import React from "react"; | ||
| import { Bookmark, BookmarkCheck, CirclePlus, Loader2 } from "lucide-react"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| /** | ||
| * Reusable BookmarkButton component with consistent filled/outlined and loading states. | ||
| * | ||
| * @param {Object} props | ||
| * @param {boolean} props.isBookmarked - Bookmark active state | ||
| * @param {boolean} [props.loading] - Loading state during API request | ||
| * @param {function} props.onClick - Click handler | ||
| * @param {string} [props.variant="course"] - "course" | "book" icon variant | ||
| * @param {string} [props.title] - Hover title | ||
| * @param {string} [props.ariaLabel] - Accessible label | ||
| * @param {string} [props.className] - Additional class names | ||
| */ | ||
| const BookmarkButton = ({ | ||
| isBookmarked, | ||
| loading = false, | ||
| onClick, | ||
| variant = "course", | ||
| title, | ||
| ariaLabel, | ||
| className, | ||
| }) => { | ||
| const defaultTitle = isBookmarked ? "Remove bookmark" : "Add bookmark"; | ||
| const buttonTitle = title || defaultTitle; | ||
| const buttonAriaLabel = ariaLabel || defaultTitle; | ||
|
|
||
| if (variant === "book") { | ||
| return ( | ||
| <button | ||
| type="button" | ||
| onClick={onClick} | ||
| disabled={loading} | ||
| title={buttonTitle} | ||
| aria-label={buttonAriaLabel} | ||
| className={cn( | ||
| "flex items-center justify-center rounded-full p-1.5 transition-all", | ||
| "hover:bg-accent hover:text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring", | ||
| loading ? "opacity-60 cursor-not-allowed" : "cursor-pointer", | ||
| isBookmarked ? "bg-accent text-white" : "text-accent", | ||
| className | ||
| )} | ||
| > | ||
| {loading ? ( | ||
| <Loader2 className="w-6 h-6 animate-spin" /> | ||
| ) : ( | ||
| <CirclePlus | ||
| className="w-6 h-6" | ||
| strokeWidth={1.75} | ||
| fill={isBookmarked ? "currentColor" : "none"} | ||
| /> | ||
| )} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <button | ||
| type="button" | ||
| onClick={onClick} | ||
| disabled={loading} | ||
| title={buttonTitle} | ||
| aria-label={buttonAriaLabel} | ||
| className={cn( | ||
| "transition-all hover:scale-110 cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded-md p-1", | ||
| loading ? "opacity-60 cursor-not-allowed" : "", | ||
| className | ||
| )} | ||
| > | ||
| {loading ? ( | ||
| <Loader2 className="w-6 h-6 text-accent animate-spin" /> | ||
| ) : isBookmarked ? ( | ||
| <BookmarkCheck className="w-6 h-6 text-accent fill-accent" /> | ||
| ) : ( | ||
| <Bookmark className="w-6 h-6 text-accent hover:fill-accent/20" /> | ||
| )} | ||
| </button> | ||
| ); | ||
| }; | ||
|
|
||
| export default BookmarkButton; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.