|
| 1 | +import { |
| 2 | + ChevronLeft, |
| 3 | + ChevronRight, |
| 4 | + ChevronsLeft, |
| 5 | + ChevronsRight, |
| 6 | + Search, |
| 7 | +} from "lucide-react"; |
| 8 | +import { useState } from "react"; |
| 9 | + |
| 10 | +import { ContentCard } from "@/components/content-card"; |
| 11 | +import { getResources, Resource } from "@/hooks/apiService"; |
| 12 | + |
| 13 | +type Direction = "double_left" | "left" | "double_right" | "right"; |
| 14 | + |
| 15 | +function ArrowButton({ direction }: { direction: Direction }) { |
| 16 | + const buttonClass = |
| 17 | + "flex items-center justify-center w-8 h-8 bg-white border border-[#F1F1F1] rounded-lg"; |
| 18 | + |
| 19 | + const iconClass = "h-4 w-4 text-[#CCCCCC]"; |
| 20 | + |
| 21 | + const icons = { |
| 22 | + double_left: <ChevronsLeft className={iconClass} />, |
| 23 | + left: <ChevronLeft className={iconClass} />, |
| 24 | + double_right: <ChevronsRight className={iconClass} />, |
| 25 | + right: <ChevronRight className={iconClass} />, |
| 26 | + }; |
| 27 | + |
| 28 | + return <button className={buttonClass}> {icons[direction]} </button>; |
| 29 | +} |
| 30 | + |
| 31 | +function ResourceCard({ item }: { item: Resource }) { |
| 32 | + const href = `/resources/${item.slug}`; |
| 33 | + |
| 34 | + // TODO: Use the horizontal card component for the mobile layout once it is available. |
| 35 | + return ( |
| 36 | + <ContentCard |
| 37 | + title={item.name} |
| 38 | + description={item.summary} |
| 39 | + author={item.author} |
| 40 | + dateTime={item.date_made} |
| 41 | + href={href} |
| 42 | + imageSrc={item.image || "/default-resource-img.jpg"} |
| 43 | + imageAlt={item.name} |
| 44 | + buttonLabel="View" |
| 45 | + className="min-h-[28rem] w-full max-w-none [&_h3]:text-xl [&_p]:text-sm" |
| 46 | + /> |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +function ResourcesCards({ searchTerm }: { searchTerm: string }) { |
| 51 | + const { data, isLoading } = getResources(); |
| 52 | + |
| 53 | + if (isLoading || data === undefined) return <p>Loading...</p>; |
| 54 | + |
| 55 | + const query = searchTerm.trim().toLowerCase(); |
| 56 | + |
| 57 | + const filteredResources = data.filter((item) => { |
| 58 | + return item.name.toLowerCase().includes(query); |
| 59 | + }); |
| 60 | + |
| 61 | + if (filteredResources.length === 0) { |
| 62 | + return <p>No resources match your search.</p>; |
| 63 | + } |
| 64 | + |
| 65 | + return filteredResources.map((item) => ( |
| 66 | + <ResourceCard key={item.id} item={item} /> |
| 67 | + )); |
| 68 | +} |
| 69 | + |
| 70 | +export default function Resources() { |
| 71 | + const [searchTerm, setSearchTerm] = useState(""); |
| 72 | + |
| 73 | + return ( |
| 74 | + <main> |
| 75 | + <div className="mx-auto flex w-full max-w-[90rem] flex-col gap-8 px-4 pt-6 sm:px-8 lg:px-16 lg:pt-8"> |
| 76 | + {/* Title + Search */} |
| 77 | + <div className="flex flex-col gap-4 md:flex-row md:items-center md:justify-between"> |
| 78 | + <h1 className="text-2xl font-bold">Resources</h1> |
| 79 | + |
| 80 | + {/* Search Box */} |
| 81 | + <div className="relative w-full md:w-auto"> |
| 82 | + <Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-gray-400" /> |
| 83 | + <input |
| 84 | + type="text" |
| 85 | + placeholder="Search..." |
| 86 | + value={searchTerm} |
| 87 | + onChange={(event) => setSearchTerm(event.target.value)} |
| 88 | + className="w-full rounded-lg border border-[#F1F1F1] py-2 pl-9 pr-4 md:w-72" |
| 89 | + /> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + {/* Cards */} |
| 93 | + <div className="md grid w-full justify-items-center gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"> |
| 94 | + <ResourcesCards searchTerm={searchTerm} /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </main> |
| 98 | + ); |
| 99 | +} |
0 commit comments