From 4941c8fbbdd79850955bcec0d9cd9823139e03b0 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Wed, 3 May 2023 23:10:07 -0400 Subject: ex2.18 country info part1 --- part2/countrydata/src/App.js | 83 +++++++++++++++++++++++++++++++++++++++++ part2/countrydata/src/index.css | 7 ++++ part2/countrydata/src/index.js | 9 +++++ 3 files changed, 99 insertions(+) create mode 100644 part2/countrydata/src/App.js create mode 100644 part2/countrydata/src/index.css create mode 100644 part2/countrydata/src/index.js (limited to 'part2/countrydata/src') diff --git a/part2/countrydata/src/App.js b/part2/countrydata/src/App.js new file mode 100644 index 0000000..b3168b1 --- /dev/null +++ b/part2/countrydata/src/App.js @@ -0,0 +1,83 @@ +import axios from 'axios' +import {useEffect, useState} from 'react' + +const Notification = (props) => { + const namesOfCountries = props.countries.map(country => { + return
  • {country.name.official}
  • + }) + + if (props.countries.length <= 10 && props.countries.length > 1) { + return ( +
    +         
    +        
    + ) + + } else if (props.countries.length > 10) { + return

    Too many matches, specify another filter

    + } else if (props.countries.length === 1) { + // retrieve object pertaining to country + const country = props.countries[0] + console.log(props.countries[0]) + + // extract languages from + let languages = [] + for (let value in country.languages) { + languages = languages.concat(
  • {country.languages[value]}
  • ) + } + return ( +
    +

    {country.name.official}

    +

    capital {country.capital}

    +

    area {country.area}

    +

    languages:

    + + {country.flags.alt} +
    + ) + } + } + +const App = () => { + + const [country, setCountry] = useState('') + const [countries, setCountries] = useState([]) + + const handleChange = (event) => { + console.log(event.target.value) + setCountry(event.target.value) + } + + useEffect( () => { + axios + .get('https://restcountries.com/v3.1/all') + .then(response => { + if(country) { + const filtered = response.data.filter(value => + value.name.official + .includes(country)) + setCountries(filtered) + console.log(countries, countries.length) + + } + }) + //.catch(response => { + //return null + //}) + + }, [country, countries] + ) + + return ( +
    + find countries + +
    + ) +} + +export default App; diff --git a/part2/countrydata/src/index.css b/part2/countrydata/src/index.css new file mode 100644 index 0000000..07d0f25 --- /dev/null +++ b/part2/countrydata/src/index.css @@ -0,0 +1,7 @@ +li { + list-style-type:none; + margin-left: 0px; + font-size: 20px; + padding: 0px; + color: green; +} diff --git a/part2/countrydata/src/index.js b/part2/countrydata/src/index.js new file mode 100644 index 0000000..e36818f --- /dev/null +++ b/part2/countrydata/src/index.js @@ -0,0 +1,9 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App' +import './index.css' + +const root = ReactDOM.createRoot(document.getElementById('root')) +root.render( + +) -- cgit v1.2.3