Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import React, { useState, useEffect } from "react"
  2. import axios from "axios"
  3.  
  4. const App = () => {
  5. const [countries, setCountries] = useState([])
  6. const [weather, setWeather] = useState([])
  7. const [countriesToShow, setCountriesToShow] = useState([])
  8.  
  9. useEffect(() => {
  10. axios.get("https://restcountries.eu/rest/v2/all").then(response => {
  11. setCountries(response.data)
  12. return axios
  13. .get(
  14. "https://api.apixu.com/v1/current.json?key=f6889e7e19f340728af140801192301&q=Paris"
  15. )
  16. .then(response => {
  17. setWeather(response.data)
  18. })
  19. })
  20. }, [])
  21. console.log("sää", weather)
  22.  
  23. const getWeather = ({ capital }) => {
  24. axios
  25. .get(
  26. `https://api.apixu.com/v1/current.json?key=f6889e7e19f340728af140801192301&q=${capital}`
  27. )
  28. .then(response => {
  29. setWeather(response.data)
  30. })
  31. }
  32.  
  33. const handleCountryChange = event => {
  34. event.preventDefault()
  35. const checker = countries.filter(country =>
  36. country.name.toLowerCase().includes(event.target.value.toLowerCase())
  37. )
  38. setCountriesToShow(checker)
  39.  
  40. console.log("countriestoshow: ", countriesToShow)
  41. }
  42.  
  43. const findForm = () => {
  44. return (
  45. <form>
  46. find countries: <input onChange={handleCountryChange} />
  47. </form>
  48. )
  49. }
  50.  
  51. const showCountry = () => {
  52. if (countriesToShow.length === 1) {
  53. const capital = countriesToShow[0].capital
  54.  
  55. getWeather({ capital })
  56. return (
  57. <div key={countriesToShow[0].name}>
  58. <h1>{countriesToShow[0].name}</h1>
  59. <p>capital {countriesToShow[0].capital}</p>
  60. <p>population {countriesToShow[0].population}</p>
  61. <h3>Languages:</h3>
  62. <ul>
  63. {countriesToShow[0].languages.map(language => (
  64. <li key={language.name}>{language.name}</li>
  65. ))}
  66. </ul>
  67. <p>
  68. <img
  69. src={countriesToShow[0].flag}
  70. alt="Flag"
  71. height="100"
  72. width="150"
  73. />
  74. </p>
  75. <h3>Weather in {countriesToShow[0].capital}</h3>
  76. <b>temperature:</b> {weather.current.temp_c} Celsius
  77. <p>
  78. <img
  79. src={weather.current.condition.icon}
  80. alt="Weather"
  81. height="70"
  82. width="70"
  83. />
  84. </p>
  85. <b>wind:</b> {weather.current.wind_kph} kph direction{" "}
  86. {weather.current.wind_dir}
  87. </div>
  88. )
  89. } else if (countriesToShow.length < 10) {
  90. return (
  91. <>
  92. {countriesToShow.map(country => (
  93. <p key={country.name}>
  94. {country.name}{" "}
  95. <button
  96. onClick={() => {
  97. setCountriesToShow([country])
  98. console.log(
  99. "countries to show after button press",
  100. countriesToShow
  101. )
  102. }}
  103. >
  104. show
  105. </button>
  106. </p>
  107. ))}
  108. </>
  109. )
  110. } else {
  111. return <p>Too many matches, specify another filter</p>
  112. }
  113. }
  114.  
  115. return (
  116. <div>
  117. {findForm()}
  118. {showCountry()}
  119. </div>
  120. )
  121. }
  122.  
  123. export default App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement