Created
July 22, 2018 15:10
-
-
Save saurabhpati/f908bd76fd7d20a2e14efbd5e0053b30 to your computer and use it in GitHub Desktop.
Github card addition using react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Card = props => { | |
return ( | |
<div style={{marginLeft:'1em'}}> | |
<img width="75" src={props.avatar_url}/> | |
<div style={{display: 'inline-block', marginLeft: 10}}> | |
<div style={{fontSize:'1.25em', fontWeight:'bold'}}> | |
<div>Name: {props.name}</div> | |
<div>Company: {props.company}</div> | |
</div> | |
</div> | |
</div> | |
); | |
} | |
let data = [{ | |
name: 'Saurabh Pati', | |
company: 'ThinkSys Inc.', | |
avatar_url: 'https://avatars3.githubusercontent.com/u/20544414?v=4' | |
}, { | |
name: 'Honey Dev P', | |
company: 'ThinkSys Inc.', | |
avatar_url: 'https://avatars0.githubusercontent.com/u/13077411?v=4' | |
}]; | |
const CardList = (props) => { | |
return ( | |
<div> | |
{props.cards.map(card => <Card {...card}/>)} | |
</div> | |
); | |
} | |
class Form extends React.Component { | |
constructor() { | |
super(); | |
this.state = {username: ''}; | |
} | |
handleSubmit = (event) => { | |
event.preventDefault(); | |
axios.get(`https://api.github.com/users/${this.state.username}`) | |
.then(res => { | |
this.props.onSubmit(res.data); | |
}) | |
.catch(reason => {console.log(reason);}) | |
} | |
handleChange = (event) => { | |
this.setState({username: event.target.value}); | |
} | |
render() { | |
return ( | |
<form onSubmit = {this.handleSubmit}> | |
<input type="text" placeholder="Github username" onChange={this.handleChange}/> | |
<button type="submit">Add card</button> | |
</form> | |
); | |
} | |
} | |
class App extends React.Component { | |
constructor () { | |
super(); | |
this.state = { | |
data: [{ | |
name: 'Saurabh Pati', | |
company: 'ThinkSys Inc.', | |
avatar_url: 'https://avatars3.githubusercontent.com/u/20544414?v=4' | |
}, { | |
name: 'Honey Dev P', | |
company: 'ThinkSys Inc.', | |
avatar_url: 'https://avatars0.githubusercontent.com/u/13077411?v=4' | |
}]}; | |
} | |
addCard = (data) => { | |
this.setState((prevState) => { | |
prevState.data.push(data); | |
return {data: prevState.data}; | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<Form onSubmit={this.addCard}/> | |
<CardList cards = {this.state.data}/> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render(<App/>, mountNode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment