@@ -6,26 +6,29 @@ import { useParams } from 'next/navigation'; |
| 6 | 6 | import { getConflicts, getPeopleByConflict, Conflict, PersonDetail } from '@/lib/api'; |
| 7 | 7 | import Header from '@/components/Header'; |
| 8 | 8 | import DocumentIcon from '@/components/DocumentIcon'; |
| 9 | +import Pagination from '@/components/Pagination'; |
| 9 | 10 | |
| 10 | 11 | export default function ConflictPage() { |
| 11 | 12 | const params = useParams(); |
| 12 | 13 | const conflictId = parseInt(params.id as string); |
| 13 | | - |
| 14 | + |
| 14 | 15 | const [conflict, setConflict] = useState<Conflict | null>(null); |
| 15 | 16 | const [people, setPeople] = useState<PersonDetail[]>([]); |
| 16 | 17 | const [loading, setLoading] = useState(true); |
| 17 | 18 | const [error, setError] = useState<string | null>(null); |
| 19 | + const [currentPage, setCurrentPage] = useState(1); |
| 20 | + const [itemsPerPage, setItemsPerPage] = useState(30); |
| 18 | 21 | |
| 19 | | - useEffect(() => { |
| 22 | +useEffect(() => { |
| 20 | 23 | async function fetchData() { |
| 21 | 24 | try { |
| 22 | 25 | const conflicts = await getConflicts(); |
| 23 | 26 | const currentConflict = conflicts.find(c => c.id === conflictId); |
| 24 | | - |
| 27 | + |
| 25 | 28 | if (!currentConflict) { |
| 26 | 29 | throw new Error('Conflict not found'); |
| 27 | 30 | } |
| 28 | | - |
| 31 | + |
| 29 | 32 | setConflict(currentConflict); |
| 30 | 33 | const peopleData = await getPeopleByConflict(conflictId); |
| 31 | 34 | setPeople(peopleData); |
@@ -36,10 +39,29 @@ export default function ConflictPage() { |
| 36 | 39 | setLoading(false); |
| 37 | 40 | } |
| 38 | 41 | } |
| 39 | | - |
| 42 | + |
| 40 | 43 | fetchData(); |
| 41 | 44 | }, [conflictId]); |
| 42 | 45 | |
| 46 | + // Calculate paginated data |
| 47 | + const totalItems = people.length; |
| 48 | + const totalPages = Math.ceil(totalItems / itemsPerPage); |
| 49 | + const startIndex = (currentPage - 1) * itemsPerPage; |
| 50 | + const endIndex = startIndex + itemsPerPage; |
| 51 | + const paginatedPeople = people.slice(startIndex, endIndex); |
| 52 | + |
| 53 | + // Handlers for pagination |
| 54 | + const handlePageChange = (page: number) => { |
| 55 | + setCurrentPage(page); |
| 56 | + // Scroll to top of results |
| 57 | + window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleItemsPerPageChange = (newItemsPerPage: number) => { |
| 61 | + setItemsPerPage(newItemsPerPage); |
| 62 | + setCurrentPage(1); // Reset to first page when changing items per page |
| 63 | + }; |
| 64 | + |
| 43 | 65 | if (loading) { |
| 44 | 66 | return ( |
| 45 | 67 | <div className="min-h-screen bg-vmi-cream flex items-center justify-center"> |
@@ -97,37 +119,58 @@ export default function ConflictPage() { |
| 97 | 119 | <h2 className="text-3xl font-bold mb-8 text-center text-vmi-red"> |
| 98 | 120 | Honor Roll |
| 99 | 121 | </h2> |
| 100 | | - |
| 122 | + |
| 101 | 123 | {people.length === 0 ? ( |
| 102 | 124 | <p className="text-center text-gray-600 text-lg">No casualties recorded yet.</p> |
| 103 | 125 | ) : ( |
| 104 | | - <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> |
| 105 | | - {people.map((person) => ( |
| 106 | | - <Link |
| 107 | | - key={person.id} |
| 108 | | - href={`/memorial/person/${person.id}`} |
| 109 | | - className="block p-6 border-2 border-gray-200 rounded-lg hover:border-vmi-gold hover:bg-vmi-light-gold transition-all duration-200 group" |
| 110 | | - > |
| 111 | | - <h3 className="text-xl font-bold text-gray-800 group-hover:text-vmi-red transition-colors mb-2 flex items-center gap-2"> |
| 112 | | - {person.full_display_name ? |
| 113 | | - person.full_display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '') |
| 114 | | - : person.display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '')} |
| 115 | | - {person.pdf_key && <DocumentIcon className="flex-shrink-0" />} |
| 116 | | - </h3> |
| 117 | | - {person.rank && ( |
| 118 | | - <p className="text-gray-700 font-semibold">{person.rank}</p> |
| 119 | | - )} |
| 120 | | - {person.unit && ( |
| 121 | | - <p className="text-gray-600 text-sm italic">{person.unit}</p> |
| 122 | | - )} |
| 123 | | - {person.death_description && ( |
| 124 | | - <p className="text-gray-600 text-sm italic mt-3 line-clamp-3"> |
| 125 | | - {person.death_description} |
| 126 | | - </p> |
| 127 | | - )} |
| 128 | | - </Link> |
| 129 | | - ))} |
| 130 | | - </div> |
| 126 | + <> |
| 127 | + {/* Pagination Controls - Top */} |
| 128 | + <Pagination |
| 129 | + currentPage={currentPage} |
| 130 | + totalItems={totalItems} |
| 131 | + itemsPerPage={itemsPerPage} |
| 132 | + onPageChange={handlePageChange} |
| 133 | + onItemsPerPageChange={handleItemsPerPageChange} |
| 134 | + /> |
| 135 | + |
| 136 | + {/* People Grid */} |
| 137 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> |
| 138 | + {paginatedPeople.map((person) => ( |
| 139 | + <Link |
| 140 | + key={person.id} |
| 141 | + href={`/memorial/person/${person.id}`} |
| 142 | + className="block p-6 border-2 border-gray-200 rounded-lg hover:border-vmi-gold hover:bg-vmi-light-gold transition-all duration-200 group" |
| 143 | + > |
| 144 | + <h3 className="text-xl font-bold text-gray-800 group-hover:text-vmi-red transition-colors mb-2 flex items-center gap-2"> |
| 145 | + {person.full_display_name ? |
| 146 | + person.full_display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '') |
| 147 | + : person.display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '')} |
| 148 | + {person.pdf_key && <DocumentIcon className="flex-shrink-0" />} |
| 149 | + </h3> |
| 150 | + {person.rank && ( |
| 151 | + <p className="text-gray-700 font-semibold">{person.rank}</p> |
| 152 | + )} |
| 153 | + {person.unit && ( |
| 154 | + <p className="text-gray-600 text-sm italic">{person.unit}</p> |
| 155 | + )} |
| 156 | + {person.death_description && ( |
| 157 | + <p className="text-gray-600 text-sm italic mt-3 line-clamp-3"> |
| 158 | + {person.death_description} |
| 159 | + </p> |
| 160 | + )} |
| 161 | + </Link> |
| 162 | + ))} |
| 163 | + </div> |
| 164 | + |
| 165 | + {/* Pagination Controls - Bottom */} |
| 166 | + <Pagination |
| 167 | + currentPage={currentPage} |
| 168 | + totalItems={totalItems} |
| 169 | + itemsPerPage={itemsPerPage} |
| 170 | + onPageChange={handlePageChange} |
| 171 | + onItemsPerPageChange={handleItemsPerPageChange} |
| 172 | + /> |
| 173 | + </> |
| 131 | 174 | )} |
| 132 | 175 | </div> |
| 133 | 176 | </main> |