diff --git a/src/App.js b/src/App.js index 7e261ca47..0bafc6ddd 100755 --- a/src/App.js +++ b/src/App.js @@ -1,25 +1,112 @@ import React, { Component } from 'react'; -import logo from './logo.svg'; import './App.css'; +import ContactCard from './components/ContactCard'; +import contacts from './data/contacts.json'; +import Button from './components/Button'; + +let firstFiveContacts = contacts.slice(0,5); +let randomContact = contacts[Math.floor(Math.random() * contacts.length)]; class App extends Component { + state = { + contacts: firstFiveContacts, + } + + clickAddRandomContact = () => { + const { contacts } = this.state; + let random = Math.floor(Math.random() * (contacts.length)); + // console.log(`obtengo el random: ${random}`); + this.setState({ + contacts: [contacts[random], ...contacts] + }) + } + + addContact = () => { + const { contacts } = this.state; + this.setState({ + contacts: [randomContact, ...contacts] + }) + } + + sortByNames = () => { + const { contacts } = this.state; + const sortedContacts = contacts.sort( + (a,b) => { + if(a.name < b.name){ + return -1; + } else if(a.name > b.name){ + return 1; + } else{ + return 0; + } + }) + + this.setState({ + contacts: sortedContacts + }) + }; + + sortByPopularity = () => { + const { contacts } = this.state; + const sortedByPop = contacts.sort((a,b) => a.popularity - b.popularity ); + + this.setState({ + contacts: sortedByPop + }) + }; + + removeContact = (contact) => { + const {contacts} = this.state; + + // remove the contact + /* exemple with arrow functions + let tasks = [ + {'name': 'write', 'duration': 60}, + {'name': 'workout', 'duration': 120}, + {'name': 'duolingo', 'duration': 170} + ]; + let dificultad = tasks.filter((task) => task.duration >= 120); + */ + + const contactFiltered = contacts.filter((contactActtual) => contactActtual.name !== contact.name); + console.log("contactos filtrados", contactFiltered); + + this.setState({ + contacts: contactFiltered + }) + }; + render() { return (
- Edit src/App.js and save to reload.
-
| Picture | +Name | +Popularity | +Action | +
|---|---|---|---|
|
+ |
+ {name} | +{popularity} | ++ + | +