This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getAllCountries = async () => { | |
try { | |
const response = await fetch(`https://restcountries.eu/rest/v2/all`); | |
const responseCountries = await response.json(); | |
return responseCountries; | |
} catch (err) { | |
console.log(err); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [countries, setCountries] = useState([]); | |
const componentIsMounted = useRef(true); | |
useEffect(() => { | |
getAllCountries() | |
.then(response => { | |
if (componentIsMounted.current) { | |
setCountries(response); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CountriesContainer = () => { | |
const [countries, setCountries] = useState([]); | |
const componentIsMounted = useRef(true); | |
const [filterInput, setFilterInput] = useReducer( | |
(state, newState) => ({ ...state, ...newState }), | |
{ | |
name: "", | |
capital: "", | |
population: "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import "./AdvancedFilter.css"; | |
const AdvancedFilter = ({ searchValue, handleChangeValue }) => ( | |
<form className="filter-container"> | |
<input | |
data-testid="filter-input-name" | |
type="text" | |
name="name" | |
value={searchValue.name} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const filterCountries = list => { | |
return list.filter(item => { | |
return ( | |
item.name | |
.toLowerCase() | |
.includes( | |
filterInput.name.toLowerCase() | |
) && | |
item.capital | |
.toLowerCase() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return ( | |
<> | |
<AdvancedFilter | |
searchValue={filterInput} | |
handleChangeValue={handleFilterCountries} | |
/> | |
<div className="countries-container"> | |
{countriesList.map(country => ( | |
<CountryCard key={country.numericCode} country={country} /> | |
))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDOM from "react-dom"; | |
import "./index.css"; | |
import App from "./App"; | |
import * as serviceWorker from "./serviceWorker"; | |
ReactDOM.render(<App />, document.getElementById("root")); | |
serviceWorker.register(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { lazy, Suspense } from "react"; | |
import "./App.css"; | |
import CountriesContainer from "./routes/CountriesContainer"; | |
import { Switch, Route, BrowserRouter as Router } from "react-router-dom"; | |
import Navbar from "./components/Navbar"; | |
import NotFoundPage from "./routes/NotFoundPage"; | |
// pages | |
const Home = lazy(() => import("./routes/Home")); | |
const Favourites = lazy(() => import("./routes/Favourites")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const App = () => { | |
return ( | |
<Router> | |
<Navbar /> | |
<Switch> | |
<Route path="/" component={Home} exact /> | |
<Route path="/data" component={CountriesContainer} /> | |
<Route path="/favourites" component={Favourites} /> | |
<Route component={NotFoundPage} /> | |
</Switch> |