-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.jsx
148 lines (129 loc) · 4.29 KB
/
Card.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React from 'react';
// let data =[
// { name : "Tanmay Gujar",
// avatar_url : "https://avatars0.githubusercontent.com/u/19?v=4",
// company : "Nitor Infotech"},
// { name : "Aziz Nagpurwala",
// avatar_url : "https://avatars2.githubusercontent.com/u/37330246?v=4",
// company : "Nitor Infotech"},
// ];
const Card = (props) =>{
return(
<div style={{margin:'1em'}}>
<img width="75" src={props.avatar_url} />
<div style={{display:'inline-block',marginLeft:10}}>
<div style={{fontSize:'1.2em',fontWeight:'bold'}}> {props.name} </div>
<div> {props.company} </div>
</div>
</div>
);
};
const CardList = (props) =>{
return(
<div>
{/* <Card name = "Aziz Nagpurwala"
avatar_url = "https://avatars2.githubusercontent.com/u/37330246?v=4"
company = "Nitor Infotech"/> */}
{props.cards.map(card => <Card key={card.id} {...card} />)} {/* spreadoperator */}
</div>
);
};
class Form extends React.Component{
constructor(props){
super(props);
this.handleSubmit=this.handleSubmit.bind(this);
// this.clearData =this.clearData.bind(this);
//this.onSubmit = this.onSubmit.bind(this);
this.state = {userName : ''};
}
handleSubmit(event){
var globalThis = this;
event.preventDefault();
//console.log('Event submitted',this.userNameInput.value);
console.log('Event submitted',this.state.userName);
fetch(`https://api.github.com/users/${this.state.userName}`)
.then( function(response) {
return response.json();
})
// .then( function(response) {
// setTimeout( function() {
// this.setState({
// infoStatus: 'loaded'
// });
// }, 300);
// return response.json();
// })
.then( function(data) {
//debugger;
globalThis.props.onSubmit(data);
//globalThis.setState({userName : ''});
document.getElementById('nametextbox').value = '';
// clearData();
//console.log(data.avatar_url);
});
// })
// .then(function(data){
// console.log(data.avatar_url);
// // console.log(response.)
// })
}
render(){
return(
<form onSubmit={this.handleSubmit}>
<input type="text" id="nametextbox"
onChange={(event)=>this.setState({userName:event.target.value})}
// ref={(input)=>this.userNameInput = input}
placeholder = "Github username" required />
<button type="submit">Add Card </button>
</form>
);
}
}
class App extends React.Component{
constructor(props){
super(props);
this.addNewCard=this.addNewCard.bind(this);
this.state = {
cards :[
// { name : "Tanmay Gujar",
// avatar_url : "https://avatars0.githubusercontent.com/u/19?v=4",
// company : "Nitor Infotech"},
// { name : "Aziz Nagpurwala",
// avatar_url : "https://avatars2.githubusercontent.com/u/37330246?v=4",
// company : "Nitor Infotech"},
]
};
}
addNewCard(cardInfo){
//console.log(cardInfo);
this.setState(prevState => ({
cards: prevState.cards.concat(cardInfo)
}));
}
// state = {
// cards :[
// { name : "Tanmay Gujar",
// avatar_url : "https://avatars0.githubusercontent.com/u/19?v=4",
// company : "Nitor Infotech"},
// { name : "Aziz Nagpurwala",
// avatar_url : "https://avatars2.githubusercontent.com/u/37330246?v=4",
// company : "Nitor Infotech"},
// ]
// };
// clearData(){
// this.setState({
// userName: ''
// })
// }
render(){
return(
<div>
<Form onSubmit={this.addNewCard}/>
<br/>
{/* <CardList cards= {data} /> */}
<CardList cards= {this.state.cards} />
</div>
);
}
}
export default App;