diff --git a/country-explorer/src/components/App.tsx b/country-explorer/src/components/App.tsx index 9ecc689..a0b019f 100644 --- a/country-explorer/src/components/App.tsx +++ b/country-explorer/src/components/App.tsx @@ -4,6 +4,7 @@ import { useCountries } from '../hooks/useCountries'; import CountryCard from './CountryCard'; import Loading from './Loading'; import ErrorMessage from './ErrorMessage'; +import NavigationControls from './NavigationControls'; function App() { const { countries, isLoading, isError, error, refetch } = useCountries(); @@ -11,6 +12,16 @@ function App() { const selectedCountry = countries.find((country) => country.code === selectedCountryCode) ?? countries[0]; const activeCountryCode = selectedCountry?.code ?? null; + const activeCountryIndex = countries.findIndex((country) => country.code === activeCountryCode); + + function navigateCountry(direction: -1 | 1) { + setSelectedCountryCode((currentCode) => { + const currentIndex = countries.findIndex((country) => country.code === currentCode); + if (currentIndex === -1) return countries[0]?.code ?? null; + + return countries[currentIndex + direction]?.code ?? currentCode; + }); + } // Keep the selection valid when data arrives or the available countries change. // Store the fallback so a removed country is not reselected if it returns later. @@ -57,11 +68,19 @@ function App() { onRetry={() => void refetch()} /> ) : selectedCountry ? ( - + <> + + navigateCountry(-1)} + onNext={() => navigateCountry(1)} + /> + ) : (

No countries available.

)} - {/* FavoriteButton and NavigationControls go here */} + {/* FavoriteButton goes here */}
diff --git a/country-explorer/src/components/NavigationControls.css b/country-explorer/src/components/NavigationControls.css new file mode 100644 index 0000000..5bbb23b --- /dev/null +++ b/country-explorer/src/components/NavigationControls.css @@ -0,0 +1,34 @@ +.country-navigation { + display: flex; + flex-wrap: wrap; + justify-content: center; + align-items: center; + gap: 1rem; + margin: 1rem; +} + +.country-navigation button { + min-height: 44px; + padding: 0.5rem 1rem; + font: inherit; + color: var(--text-h); + background: var(--bg); + border: 1px solid var(--border); + border-radius: 4px; + cursor: pointer; +} + +.country-navigation button:hover:not(:disabled) { + background: var(--accent-bg); + border-color: var(--accent); +} + +.country-navigation button:focus-visible { + outline: 2px solid var(--accent); + outline-offset: 3px; +} + +.country-navigation button:disabled { + opacity: 0.5; + cursor: default; +} diff --git a/country-explorer/src/components/NavigationControls.tsx b/country-explorer/src/components/NavigationControls.tsx new file mode 100644 index 0000000..de7a9d1 --- /dev/null +++ b/country-explorer/src/components/NavigationControls.tsx @@ -0,0 +1,44 @@ +import './NavigationControls.css'; + +interface NavigationControlsProps { + currentIndex: number; + totalCountries: number; + onPrevious: () => void; + onNext: () => void; +} + +function NavigationControls({ + currentIndex, + totalCountries, + onPrevious, + onNext, +}: NavigationControlsProps) { + const hasSelection = + Number.isInteger(currentIndex) && currentIndex >= 0 && currentIndex < totalCountries; + + return ( + + ); +} + +export default NavigationControls;