Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import contacts from './data/contacts.json';
import CardsList from './components/CardsList';



class App extends Component {
render() {
state = {
list: contacts.slice(0,5)
};
onAddItem = () => {
this.setState(state => {
const randomValue = Math.floor(contacts.length * Math.random())
const list = state.list.concat(contacts[randomValue]);
return {
list,
value: '',
};
});
};
sortByName = () => {
const list=this.state.list.sort((a,b) => (a.name > b.name) ? 1 : ((b.name> a.name) ? -1 : 0));
this.setState({list: list})
};
sortByPopularity = () => {
const list=this.state.list.sort((a,b) => (a.popularity < b.popularity) ? 1 : ((b.popularity< a.popularity) ? -1 : 0));
this.setState({list: list})
};
onDeleteItem = (index) => {
let {list} = this.state
list.splice(index,1)
this.setState({
list
})

};
render () {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<div>
{ <button
type="button"
onClick={this.onAddItem}
>
Add Random Contact
</button> }
{ <button
type="button"
onClick={this.sortByName}
>
Sort By Name
</button> }
{ <button
type="button"
onClick={this.sortByPopularity}
>
Sort By Popularity
</button> }

<CardsList contacts={this.state.list} onDelete={this.onDeleteItem}/>


</div>
);

)
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/components/Card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { Component } from 'react';

class Card extends Component {

render () {
console.log(this.props)
const {pictureUrl, name, popularity} = this.props.contact;
return (
<div >
<img src={pictureUrl}/>
<h2>{name}</h2>
<h3>{popularity}</h3>

</div>

)
}
}
export default Card;
36 changes: 36 additions & 0 deletions src/components/CardsList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component } from 'react';
import Card from './Card';



class CardsList extends Component {



renderList () {
return this.props.contacts.map((contact, index) => {
return <div>
<Card key={`id-${index}`} contact={contact}/>
<button
type="button"
onClick={() => this.props.onDelete(index)}
>
Delete
</button>
</div>

})
}



render () {
return (
<div >
{this.renderList()}
</div>

)
}
}
export default CardsList;
2 changes: 1 addition & 1 deletion src/data/contacts.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
[
{
"name": "Idris Elba",
"pictureUrl": "https://image.tmdb.org/t/p/w500/d9NkfCwczP0TjgrjpF94jF67SK8.jpg",
Expand Down