@@ -6,26 +6,29 @@ import { useParams } from 'next/navigation'; |
| 6 | import { getConflicts, getPeopleByConflict, Conflict, PersonDetail } from '@/lib/api'; | 6 | import { getConflicts, getPeopleByConflict, Conflict, PersonDetail } from '@/lib/api'; |
| 7 | import Header from '@/components/Header'; | 7 | import Header from '@/components/Header'; |
| 8 | import DocumentIcon from '@/components/DocumentIcon'; | 8 | import DocumentIcon from '@/components/DocumentIcon'; |
| | 9 | +import Pagination from '@/components/Pagination'; |
| 9 | | 10 | |
| 10 | export default function ConflictPage() { | 11 | export default function ConflictPage() { |
| 11 | const params = useParams(); | 12 | const params = useParams(); |
| 12 | const conflictId = parseInt(params.id as string); | 13 | const conflictId = parseInt(params.id as string); |
| 13 | - | 14 | + |
| 14 | const [conflict, setConflict] = useState<Conflict | null>(null); | 15 | const [conflict, setConflict] = useState<Conflict | null>(null); |
| 15 | const [people, setPeople] = useState<PersonDetail[]>([]); | 16 | const [people, setPeople] = useState<PersonDetail[]>([]); |
| 16 | const [loading, setLoading] = useState(true); | 17 | const [loading, setLoading] = useState(true); |
| 17 | const [error, setError] = useState<string | null>(null); | 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 | async function fetchData() { | 23 | async function fetchData() { |
| 21 | try { | 24 | try { |
| 22 | const conflicts = await getConflicts(); | 25 | const conflicts = await getConflicts(); |
| 23 | const currentConflict = conflicts.find(c => c.id === conflictId); | 26 | const currentConflict = conflicts.find(c => c.id === conflictId); |
| 24 | - | 27 | + |
| 25 | if (!currentConflict) { | 28 | if (!currentConflict) { |
| 26 | throw new Error('Conflict not found'); | 29 | throw new Error('Conflict not found'); |
| 27 | } | 30 | } |
| 28 | - | 31 | + |
| 29 | setConflict(currentConflict); | 32 | setConflict(currentConflict); |
| 30 | const peopleData = await getPeopleByConflict(conflictId); | 33 | const peopleData = await getPeopleByConflict(conflictId); |
| 31 | setPeople(peopleData); | 34 | setPeople(peopleData); |
@@ -36,10 +39,29 @@ export default function ConflictPage() { |
| 36 | setLoading(false); | 39 | setLoading(false); |
| 37 | } | 40 | } |
| 38 | } | 41 | } |
| 39 | - | 42 | + |
| 40 | fetchData(); | 43 | fetchData(); |
| 41 | }, [conflictId]); | 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 | if (loading) { | 65 | if (loading) { |
| 44 | return ( | 66 | return ( |
| 45 | <div className="min-h-screen bg-vmi-cream flex items-center justify-center"> | 67 | <div className="min-h-screen bg-vmi-cream flex items-center justify-center"> |
@@ -97,37 +119,58 @@ export default function ConflictPage() { |
| 97 | <h2 className="text-3xl font-bold mb-8 text-center text-vmi-red"> | 119 | <h2 className="text-3xl font-bold mb-8 text-center text-vmi-red"> |
| 98 | Honor Roll | 120 | Honor Roll |
| 99 | </h2> | 121 | </h2> |
| 100 | - | 122 | + |
| 101 | {people.length === 0 ? ( | 123 | {people.length === 0 ? ( |
| 102 | <p className="text-center text-gray-600 text-lg">No casualties recorded yet.</p> | 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"> | 126 | + <> |
| 105 | - {people.map((person) => ( | 127 | + {/* Pagination Controls - Top */} |
| 106 | - <Link | 128 | + <Pagination |
| 107 | - key={person.id} | 129 | + currentPage={currentPage} |
| 108 | - href={`/memorial/person/${person.id}`} | 130 | + totalItems={totalItems} |
| 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" | 131 | + itemsPerPage={itemsPerPage} |
| 110 | - > | 132 | + onPageChange={handlePageChange} |
| 111 | - <h3 className="text-xl font-bold text-gray-800 group-hover:text-vmi-red transition-colors mb-2 flex items-center gap-2"> | 133 | + onItemsPerPageChange={handleItemsPerPageChange} |
| 112 | - {person.full_display_name ? | 134 | + /> |
| 113 | - person.full_display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '') | 135 | + |
| 114 | - : person.display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '')} | 136 | + {/* People Grid */} |
| 115 | - {person.pdf_key && <DocumentIcon className="flex-shrink-0" />} | 137 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> |
| 116 | - </h3> | 138 | + {paginatedPeople.map((person) => ( |
| 117 | - {person.rank && ( | 139 | + <Link |
| 118 | - <p className="text-gray-700 font-semibold">{person.rank}</p> | 140 | + key={person.id} |
| 119 | - )} | 141 | + href={`/memorial/person/${person.id}`} |
| 120 | - {person.unit && ( | 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" |
| 121 | - <p className="text-gray-600 text-sm italic">{person.unit}</p> | 143 | + > |
| 122 | - )} | 144 | + <h3 className="text-xl font-bold text-gray-800 group-hover:text-vmi-red transition-colors mb-2 flex items-center gap-2"> |
| 123 | - {person.death_description && ( | 145 | + {person.full_display_name ? |
| 124 | - <p className="text-gray-600 text-sm italic mt-3 line-clamp-3"> | 146 | + person.full_display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '') |
| 125 | - {person.death_description} | 147 | + : person.display_name.replace(person.rank + ' ', '').replace(person.rank + ', ', '')} |
| 126 | - </p> | 148 | + {person.pdf_key && <DocumentIcon className="flex-shrink-0" />} |
| 127 | - )} | 149 | + </h3> |
| 128 | - </Link> | 150 | + {person.rank && ( |
| 129 | - ))} | 151 | + <p className="text-gray-700 font-semibold">{person.rank}</p> |
| 130 | - </div> | 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 | </div> | 175 | </div> |
| 133 | </main> | 176 | </main> |