Skip to content

Commit

Permalink
day 19 has been published
Browse files Browse the repository at this point in the history
  • Loading branch information
Asabeneh committed Oct 18, 2020
1 parent b4e2374 commit 556e47a
Show file tree
Hide file tree
Showing 30 changed files with 13,720 additions and 318 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions 18_Fetch_And_Axios/18_fetch_and_axios_boilerplate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 30 Days of React App: Day 19

In the project directory, you can run to start the project

### `npm start`
File renamed without changes.
File renamed without changes.
84 changes: 84 additions & 0 deletions 18_Fetch_And_Axios/18_fetch_and_axios_boilerplate/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import axios from 'axios'

const Country = ({
country: { name, capital, flag, languages, population, currency },
}) => {
const formatedCapital =
capital.length > 0 ? (
<>
<span>Capital: </span>
{capital}
</>
) : (
''
)
const formatLanguage = languages.length > 1 ? `Languages` : `Language`
console.log(languages)
return (
<div className='country'>
<div className='country_flag'>
<img src={flag} alt={name} />
</div>
<h3 className='country_name'>{name.toUpperCase()}</h3>
<div class='country_text'>
<p>{formatedCapital}</p>
<p>
<span>{formatLanguage}: </span>
{languages.map((language) => language.name).join(', ')}
</p>
<p>
<span>Population: </span>
{population.toLocaleString()}
</p>
<p>
<span>Currency: </span>
{currency}
</p>
</div>
</div>
)
}

class App extends Component {
state = {
data: [],
}

componentDidMount() {
this.fetchCountryData()
}
fetchCountryData = async () => {
const url = 'https://restcountries.eu/rest/v2/all'
try {
const response = await axios.get(url)
const data = await response.data
this.setState({
data,
})
} catch (error) {
console.log(error)
}
}

render() {
return (
<div className='App'>
<h1>React Component Life Cycle</h1>
<h1>Calling API</h1>
<div>
<p>There are {this.state.data.length} countries in the api</p>
<div className='countries-wrapper'>
{this.state.data.map((country) => (
<Country country={country} />
))}
</div>
</div>
</div>
)
}
}

const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)
File renamed without changes.
Loading

0 comments on commit 556e47a

Please sign in to comment.