Day_16 has been added

pull/72/head
asabeneh 4 years ago
parent ad6b501b61
commit 0fd4349292

@ -1,133 +1,95 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import axios from '../../../17_React_Router/15_react_router_boilerplate/src/node_modules/axios'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import moment from '../../../17_React_Router/15_react_router_boilerplate/src/node_modules/moment'
import styled from 'styled-components'
import {
TiSocialLinkedinCircular,
TiSocialGithubCircular,
TiSocialTwitterCircular,
} from '../../../17_React_Router/15_react_router_boilerplate/src/node_modules/react-icons/ti'
const Title = styled.h1` const Button = ({ onClick, text, style }) => {
font-size: 70px; return (
font-weight: 300; <button onClick={onClick} style={style}>
` {text}
const SubTitle = styled.h2` </button>
font-weight: 300; )
`
const Header = styled.header`
background-color: #61dbfb;
padding: 25;
padding: 10px;
margin: 0;
`
class App extends Component {
constructor(props) {
super(props)
console.log('I am the constructor and I will be the first to run.')
this.state = {
firstName: 'John',
data: [],
day: 1,
congratulate: '',
}
} }
componentDidMount() { const buttonWithStyles = (CompParam, bgColor = 'default') => {
const API_URL = 'https://restcountries.eu/rest/v2/all' const colors = [
axios {
.get(API_URL) name: 'default',
.then((response) => { backgroundColor: '#e7e7e7',
this.setState({ color: '#000000',
data: response.data, },
}) {
}) name: 'react',
.catch((error) => { backgroundColor: '#61dbfb',
console.log(error) color: '#ffffff',
}) },
{
name: 'success',
backgroundColor: '#4CAF50',
color: '#ffffff',
},
{
name: 'info',
backgroundColor: '#2196F3',
color: '#ffffff',
},
{
name: 'warning',
backgroundColor: '#ff9800',
color: '#ffffff',
},
{
name: 'danger',
backgroundColor: '#f44336',
color: '#ffffff',
},
]
const { backgroundColor, color } = colors.find((c) => c.name === bgColor)
const buttonStyles = {
backgroundColor,
padding: '10px 45px',
border: 'none',
borderRadius: 3,
margin: 3,
cursor: 'pointer',
fontSize: '1.25rem',
color,
} }
static getDerivedStateFromProps(props, state) { return (props) => {
return { firstName: props.firstName } return <CompParam {...props} style={buttonStyles} />
}
shouldComponentUpdate(nextProps, nextState) {
console.log(nextProps, nextState)
console.log(nextState.day)
if (nextState.day > 31) {
return false
} else {
return true
} }
} }
doChallenge = () => { const NewButton = buttonWithStyles(Button)
this.setState({ const ReactButton = buttonWithStyles(Button, 'react')
day: this.state.day + 1, const InfoButton = buttonWithStyles(Button, 'info')
}) const WarningButton = buttonWithStyles(Button, 'warning')
} const DangerButton = buttonWithStyles(Button, 'danger')
renderCountries = () => { const SuccessButton = buttonWithStyles(Button, 'success')
return this.state.data.map((country) => {
const languageOrLanguages =
country.languages.length > 1 ? 'Langauges' : 'Language'
const formatLanguages = country.languages
.map(({ name }) => name)
.join(', ')
return (
<div>
<div>
{' '}
<img src={country.flag} alt={country.name} />{' '}
</div>
<div>
<h1>{country.name}</h1>
<p>Capital: {country.capital}</p>
<p>
{languageOrLanguages}: {formatLanguages}
</p>
<p>Population: {country.population}</p>
</div>
</div>
)
})
}
componentDidUpdate(prevProps, prevState) {
if (prevState.day == 30) {
this.setState({
congratulate: 'Congratulations,Challenge has been completed',
})
}
console.log(prevState, prevProps)
}
class App extends Component {
render() { render() {
return ( return (
<div className='App'> <div className='App'>
<Header> <h1>Higher Order Components</h1>
<Title>30 Days Of React</Title> <Button text='No Style' onClick={() => alert('I am not styled yet')} />
<h2>Getting Started React</h2> <NewButton
<h3>JavaScript Library</h3> text='Styled Button'
<p>Instructor: Asabeneh Yetayey</p> onClick={() => alert('I am the default style')}
<small>Oct 15, 2020</small> />
</Header> <ReactButton text='React' onClick={() => alert('I have react color')} />
<p>This challenge was started {moment('2020-10-01').fromNow()}</p> <InfoButton
<p>The challenge will be over in {moment('2020-10-30').fromNow()}</p> text='Info'
<p>Today is {moment(new Date()).format('MMMM DD, YYYY HH:mm')}</p> onClick={() => alert('I am styled with info color')}
<h1>React Component Life Cycle</h1> />
<h1>Calling API</h1> <SuccessButton text='Success' onClick={() => alert('I am succesful')} />
<TiSocialLinkedinCircular /> <WarningButton
<TiSocialGithubCircular /> text='Warning'
<TiSocialTwitterCircular /> onClick={() => alert('I warn you many times')}
/>
<button onClick={this.doChallenge}>Do Challenge</button> <DangerButton
<p>Challenge: Day {this.state.day}</p> text='Danger'
{this.state.congratulate && <h2>{this.state.congratulate}</h2>} onClick={() => alert('Oh no, you can not restore it')}
<div> />
<p>There are {this.state.data.length} countries in the api</p>
<div className='countries-wrapper'>{this.renderCountries()}</div>
</div>
</div> </div>
) )
} }

Loading…
Cancel
Save