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
78 changes: 63 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,73 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import contacts from './data/contacts.json'
import Card from './Components/Card'
import AddRandomButon from './Components/AddRandomButton'


class App extends Component {

state = {
contacts: contacts.slice(0, 5)
}

handleClickRandom = () => {
const index = Math.floor(Math.random() * contacts.length);
const randomContact = contacts[index]
this.setState({
contacts: [...this.state.contacts, randomContact]
})
}

handleClickSortByName = () => {
const nameArray = this.state.contacts.sort((a, b) => {
return a.name.localeCompare(b.name);
})
this.setState({
contacts: nameArray,
})
}

handleClickSortByPopularity = () => {
const popuArray = this.state.contacts.sort((a, b) => {
return b.popularity - a.popularity;
})
this.setState({
contacts: popuArray,
})
}

handleClickDeleted = (index) => {
const { contacts } = this.state;
contacts.splice(index, 1);
this.setState({
contacts
})
}

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>
<h1>IronContacts</h1>
<AddRandomButon
addContact={this.handleClickRandom}
/>
<button onClick={this.handleClickSortByName}>Sort by Name</button>
<button onClick={this.handleClickSortByPopularity}>Sort by Popularity</button>
<section className="row">
{this.state.contacts.map((item, index) => {
return <div>
<Card
key={`id-${index}`}
pictureUrl={item.pictureUrl}
name={item.name}
popularity={item.popularity}
/>
<button onClick={()=>this.handleClickDeleted(index)}>Delete</button>
</div>
})
}
</section>
</div>
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Components/AddRandomButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';

class AddRandomButon extends Component {
render() {
return (
<div>
<button onClick={this.props.addContact} >Add Random Contact</button>
</div>
);
}
}

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


class Card extends Component {
render() {
const { pictureUrl, name, popularity } = this.props;
return (
<div className="card">
<article>
<img src={pictureUrl} alt="" />
<p>{name}</p>
<p>{popularity}</p>

</article>
</div>
);
}
}

export default Card;