|
| 1 | +import { Link } from '@inertiajs/react' |
| 2 | + |
| 3 | +interface Article { |
| 4 | + id: number |
| 5 | + title: string |
| 6 | + slug: string |
| 7 | + excerpt: string |
| 8 | + author: { |
| 9 | + name: string |
| 10 | + username: string |
| 11 | + } |
| 12 | + publishedAt: string |
| 13 | +} |
| 14 | + |
| 15 | +interface ArticlesListProps { |
| 16 | + articles: { |
| 17 | + data: Article[] |
| 18 | + meta: { |
| 19 | + total: number |
| 20 | + per_page: number |
| 21 | + current_page: number |
| 22 | + last_page: number |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export default function ArticlesList({ articles }: ArticlesListProps) { |
| 28 | + return ( |
| 29 | + <div className="mt-8 space-y-8"> |
| 30 | + {articles.data.map((article) => ( |
| 31 | + <article key={article.id} className="relative bg-white p-8 rounded-lg shadow-sm"> |
| 32 | + <div className="flex items-center gap-x-4 text-xs"> |
| 33 | + <time dateTime={article.publishedAt} className="text-gray-500"> |
| 34 | + {new Date(article.publishedAt).toLocaleDateString('fr-FR', { |
| 35 | + year: 'numeric', |
| 36 | + month: 'long', |
| 37 | + day: 'numeric', |
| 38 | + })} |
| 39 | + </time> |
| 40 | + </div> |
| 41 | + <div className="group relative"> |
| 42 | + <h3 className="mt-3 text-lg font-semibold leading-6 text-gray-900 group-hover:text-gray-600"> |
| 43 | + <Link href={`/articles/${article.slug}`}> |
| 44 | + {/* <span className="absolute inset-0" /> */} |
| 45 | + {article.title} |
| 46 | + </Link> |
| 47 | + </h3> |
| 48 | + <p className="mt-5 line-clamp-3 text-sm leading-6 text-gray-600">{article.excerpt}</p> |
| 49 | + </div> |
| 50 | + <div className="mt-6 flex items-center gap-x-4"> |
| 51 | + <div className="flex items-center gap-x-4"> |
| 52 | + <img |
| 53 | + src={`https://ui-avatars.com/api/?name=${article.author.name}`} |
| 54 | + alt="" |
| 55 | + className="h-10 w-10 rounded-full bg-gray-100" |
| 56 | + /> |
| 57 | + <div className="text-sm leading-6"> |
| 58 | + <p className="font-semibold text-gray-900"> |
| 59 | + <Link href={`/@${article.author.username}`}> |
| 60 | + {/* <span className="absolute inset-0" /> */} |
| 61 | + {article.author.name} |
| 62 | + </Link> |
| 63 | + </p> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </article> |
| 68 | + ))} |
| 69 | + |
| 70 | + {/* Pagination */} |
| 71 | + {articles.meta.last_page > 1 && ( |
| 72 | + <nav className="flex items-center justify-between border-t border-gray-200 px-4 sm:px-0 mt-8 pt-8"> |
| 73 | + <div className="-mt-px flex w-0 flex-1"> |
| 74 | + {articles.meta.current_page > 1 && ( |
| 75 | + <Link |
| 76 | + href={`/articles?page=${articles.meta.current_page - 1}`} |
| 77 | + className="inline-flex items-center border-t-2 border-transparent pr-1 pt-4 text-sm font-medium text-gray-500 hover:border-gray-300 hover:text-gray-700" |
| 78 | + > |
| 79 | + <span>Previous</span> |
| 80 | + </Link> |
| 81 | + )} |
| 82 | + </div> |
| 83 | + <div className="hidden md:-mt-px md:flex"> |
| 84 | + {[...Array(articles.meta.last_page)].map((_, i) => ( |
| 85 | + <Link |
| 86 | + key={i} |
| 87 | + href={`/articles?page=${i + 1}`} |
| 88 | + className={`inline-flex items-center border-t-2 px-4 pt-4 text-sm font-medium ${ |
| 89 | + articles.meta.current_page === i + 1 |
| 90 | + ? 'border-indigo-500 text-indigo-600' |
| 91 | + : 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700' |
| 92 | + }`} |
| 93 | + > |
| 94 | + {i + 1} |
| 95 | + </Link> |
| 96 | + ))} |
| 97 | + </div> |
| 98 | + <div className="-mt-px flex w-0 flex-1 justify-end"> |
| 99 | + {articles.meta.current_page < articles.meta.last_page && ( |
| 100 | + <Link |
| 101 | + href={`/articles?page=${articles.meta.current_page + 1}`} |
| 102 | + className="inline-flex items-center border-t-2 border-transparent pl-1 pt-4 text-sm font-medium text-gray-500 hover:border-gray-300 hover:text-gray-700" |
| 103 | + > |
| 104 | + <span>Next</span> |
| 105 | + </Link> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + </nav> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + ) |
| 112 | +} |
0 commit comments