From 8fa222c479ac932f640b41360184bb555355298c Mon Sep 17 00:00:00 2001 From: Miguel Sanchez Date: Tue, 19 Mar 2019 19:06:16 +0100 Subject: [PATCH] done --- src/App.js | 77 ++++++++++++++++++++++++++++--------- src/components/Card.js | 19 +++++++++ src/components/CardsList.js | 36 +++++++++++++++++ src/data/contacts.json | 2 +- 4 files changed, 115 insertions(+), 19 deletions(-) create mode 100644 src/components/Card.js create mode 100644 src/components/CardsList.js diff --git a/src/App.js b/src/App.js index 7e261ca47..48b044c05 100755 --- a/src/App.js +++ b/src/App.js @@ -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 ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+
+ { } + { } + { } + + + +
- ); + + ) } } diff --git a/src/components/Card.js b/src/components/Card.js new file mode 100644 index 000000000..879e0a227 --- /dev/null +++ b/src/components/Card.js @@ -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 ( +
+ +

{name}

+

{popularity}

+ +
+ + ) + } +} +export default Card; \ No newline at end of file diff --git a/src/components/CardsList.js b/src/components/CardsList.js new file mode 100644 index 000000000..8bf87677d --- /dev/null +++ b/src/components/CardsList.js @@ -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
+ + +
+ + }) + } + + + + render () { + return ( +
+ {this.renderList()} +
+ + ) + } +} +export default CardsList; \ No newline at end of file diff --git a/src/data/contacts.json b/src/data/contacts.json index 270023615..3e603fe44 100644 --- a/src/data/contacts.json +++ b/src/data/contacts.json @@ -1,4 +1,4 @@ -[ + [ { "name": "Idris Elba", "pictureUrl": "https://image.tmdb.org/t/p/w500/d9NkfCwczP0TjgrjpF94jF67SK8.jpg",