Advertisement
Guest User

fullstackopen 2.13

a guest
Mar 25th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from "react";
  2. import axios from "axios";
  3.  
  4. const App = () => {
  5.     const [countries, setCountries] = useState([]);
  6.     const [foundNames, setFoundNames] = useState([]);
  7.     const [nameFilter, setNameFilter] = useState("");
  8.     const [viewCountry, setViewCountry] = useState(-1);
  9.  
  10.     useEffect(() => {
  11.         console.log("effect");
  12.         axios.get("https://restcountries.eu/rest/v2/all").then((response) => {
  13.             console.log("promise fulfilled");
  14.             setCountries(response.data);
  15.             console.log(response.data);
  16.         });
  17.     }, []);
  18.  
  19.     const handleFilterChange = (event) => {
  20.         setNameFilter(event.target.value);
  21.     };
  22.  
  23.     const filterNames = (event) => {
  24.         event.preventDefault();
  25.  
  26.         const objectNames = countries.filter(
  27.             (country) => country.name.toLowerCase().search(nameFilter) > -1
  28.         );
  29.         setViewCountry(-1); //NOTE remember rules of hooks
  30.         setFoundNames(objectNames);
  31.     };
  32.  
  33.     const handleSearches = () => {
  34.         if (foundNames.length >= 10) {
  35.             return <div>Too many matches, specify another query</div>;
  36.         }
  37.  
  38.         if (foundNames.length === 1) {
  39.             console.log(foundNames[0].name);
  40.             return (
  41.                 <div>
  42.                     <h2>{foundNames[0].name}</h2>
  43.  
  44.                     <h2>capital: {foundNames[0].capital}</h2>
  45.                     <h2>population: {foundNames[0].population}</h2>
  46.                     <ul>
  47.                         {foundNames[0].languages.map((langs, index) => (
  48.                             <li key={index}> {langs.name}</li>
  49.                         ))}
  50.                     </ul>
  51.                     <img
  52.                         src={foundNames[0].flag}
  53.                         alt=""
  54.                         width="100"
  55.                         height="100"
  56.                     ></img>
  57.                 </div>
  58.             );
  59.         }
  60.  
  61.         if (viewCountry >= 0) {
  62.             const temp = viewCountry;
  63.  
  64.             return (
  65.                 <div>
  66.                     <h2>{foundNames[temp].name}</h2>
  67.                     <h2>capital: {foundNames[temp].capital}</h2>
  68.                     <h2>population: {foundNames[temp].population}</h2>
  69.                     <ul>
  70.                         {foundNames[temp].languages.map((langs, index) => (
  71.                             <li key={index}> {langs.name}</li>
  72.                         ))}
  73.                     </ul>
  74.                     <img
  75.                         src={foundNames[temp].flag}
  76.                         alt=""
  77.                         width="100"
  78.                         height="100"
  79.                     ></img>
  80.                 </div>
  81.             );
  82.         }
  83.  
  84.         return (
  85.             <div>
  86.                 <ul>
  87.                     {foundNames.map((country, index) => [
  88.                         <li key={country.name}>{country.name}</li>,
  89.                         <button
  90.                             key={country.name + index}
  91.                             onClick={() => setViewCountry(index)}
  92.                         >
  93.                             view
  94.                         </button>,
  95.                     ])}
  96.                 </ul>
  97.             </div>
  98.         );
  99.     };
  100.  
  101.     return (
  102.         <div>
  103.             <h2>find Countries:</h2>
  104.             <form onChange={filterNames}>
  105.                 <input value={nameFilter} onChange={handleFilterChange}></input>
  106.             </form>
  107.             <div>{handleSearches()}</div>
  108.         </div>
  109.     );
  110. };
  111.  
  112. export default App;
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement