Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BER - Jana & Roberta #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions labs/lab-react-ironcontacts/starter-code/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,90 @@ import React from 'react';
import ReactDOM from 'react-dom';

import './index.css';
import contacts from "./contacts.json";


class App extends React.Component {
constructor(props) {
super(props);
this.state = {
contacts : contacts,
subContacts : contacts.slice(0,5)
}
}

handleClick(){
let newSubContacts = this.state.subContacts.slice();
newSubContacts.push(contacts[Math.floor(Math.random()*(contacts.length-1))]);
this.setState({
subContacts : newSubContacts
})
}

sortName(){
let sortedContacts = this.state.subContacts.slice()
sortedContacts.sort(function(a,b) {return (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0);} );
console.log(sortedContacts)
this.setState({
subContacts : sortedContacts
})
}

sortPopularity(){
let sortedPopContacts = this.state.subContacts.slice()
sortedPopContacts.sort(function(b,a) {return (a.popularity > b.popularity) ? 1 : ((b.popularity> a.popularity) ? -1 : 0);} );
console.log(sortedPopContacts)
this.setState({
subContacts : sortedPopContacts
})
}

deleteContact(i){
let reducedContacts = this.state.subContacts.slice()
// console.log()
reducedContacts.splice(i,1) //change 0 to the key value of the button // or table element
this.setState({
subContacts : reducedContacts
})
}

render() {
return (
<div>
<h1>IronContacts</h1>

<button onClick={()=> this.handleClick()}>Add random Contact</button>
<button onClick={()=> this.sortName()}>Sort by Name</button>
<button onClick={()=> this.sortPopularity()}>Sort by Popularity</button>


<table>
<thead>
<tr>
<th>Picture</th>
<th>Name</th>
<th>Popularity</th>
<th>Action</th>

</tr>
</thead>
<tbody>

{/* für jeden actor fügen wir picture name popularity ein */}
{this.state.subContacts.map((actor,i)=>
<tr key={i}>
<td><img src={actor.pictureUrl} alt=""/> </td>
<td>{actor.name}</td>
<td>{actor.popularity.toFixed(2)}</td>
<td><button onClick={()=> this.deleteContact(i)}>Delete</button></td>
</tr>

)}
</tbody>

</table>


</div>
);
}
Expand Down