Skip to content
Snippets Groups Projects
Commit b54395fc authored by Risto Luukkonen's avatar Risto Luukkonen
Browse files

final

parent 32b57b11
Branches
No related tags found
No related merge requests found
...@@ -27,44 +27,58 @@ class App extends React.Component { ...@@ -27,44 +27,58 @@ class App extends React.Component {
}).catch(console.log('contact to the server')) }).catch(console.log('contact to the server'))
} }
// method to check whether param1 (=list) contains param2 (=comparable). // (part 2) method to check whether param1 (=list) contains param2 (=comparable).
// (part 3 update) this is obsolete due to changes in addPerson()-method
isUnique = (list,comparable) =>{ isUnique = (list,comparable) =>{
var unique = list.filter(a=> comparable===a.name) var unique = list.filter(a=> comparable===a.name)
if(unique.length>0){ if(unique.length>0){
alert(`Nimi on jo listassa`)
return false return false
} }
return true return true
} }
// add newName to the persons-list // add newName to the persons-list
addPerson = (event) =>{ addPerson = (event) =>{
event.preventDefault() event.preventDefault()
const newPerson = const newPerson =
{ {
name: this.state.newName, name: this.state.newName.trim(),
number: this.state.newNumber, number: this.state.newNumber.trim()}
id: this.state.newName.replace(/\s/g, "")}
if (this.isUnique(this.state.persons,this.state.newName) && this.state.newName !== ''){ // validating is now transformed to use .do().then().catch()-pattern
const persons = this.state.persons.concat(newPerson) if (newPerson.name === '' || newPerson.number === ''){
alert('Molemmat tiedot vaaditaan!')
}
else if (!this.isUnique(this.state.persons, newPerson.name)){
alert('Nimi on jo listassa!')
}
else{
personService.create(newPerson)
.then((response)=>{
const persons = this.state.persons.concat(response)
console.log(response)
this.setState({persons, newName: this.state.newName, newNumber:this.state.newNumber}) this.setState({persons, newName: this.state.newName, newNumber:this.state.newNumber})
personService.create(newPerson) }).catch((error)=>{
alert(error.response.data.message)
console.log(`person ${newPerson.name} created`) console.log(`person ${newPerson.name} created`)
})
} }
} }
removePerson = (id) => { removePerson = (id) => {
if(window.confirm(`Haluatko varmasti poistaa henkilön?`)){
personService.remove(id).then(response =>{ personService.remove(id).then(response =>{
const a = this.state.persons.filter(person =>person.id !== id) const a = this.state.persons.filter(person =>person.id !== id)
this.setState({persons: a}) this.setState({persons: a})
}) })
} else {
console.log(`action cancelled`)
}
} }
render() { render() {
return ( return (
...@@ -84,12 +98,4 @@ class App extends React.Component { ...@@ -84,12 +98,4 @@ class App extends React.Component {
} }
export default App export default App
/*{this.state.persons.map(person => {
<Person name={this.person.name} number = {person.number} rmvFct ={person.removeFunction(person.name)}/>
<div>{this.state.persons.map(p=>
<Person name= {p.name}
number = {p.number}
removeFunction={this.removePerson(p.name)}/>)}
\ No newline at end of file
</div>
*/
\ No newline at end of file
...@@ -3,20 +3,28 @@ import React from 'react' ...@@ -3,20 +3,28 @@ import React from 'react'
const AddPersonsForm = ({handleNameChange, handleNumberChange, addPerson})=>{ const AddPersonsForm = ({handleNameChange, handleNumberChange, addPerson})=>{
return( return(
<form onSubmit = {addPerson}>
<div> <div>
nimi: <form onSubmit = {addPerson}>
<input id="nameField" <table>
<tbody>
<tr>
<td>nimi: </td>
<td><input id="nameField"
onChange= {handleNameChange} /> onChange= {handleNameChange} />
</div> </td>
<div> </tr>
numero: <input id="numberField" <tr>
onChange= {handleNumberChange} /> <td>numero:</td>
</div> <td><input id="numberField"
onChange= {handleNumberChange} /></td>
</tr>
</tbody>
</table>
<div> <div>
<button type="submit">lisää</button> <button type="submit">lisää</button>
</div> </div>
</form> </form>
</div>
) )
} }
......
...@@ -4,11 +4,17 @@ import React from 'react' ...@@ -4,11 +4,17 @@ import React from 'react'
return( return(
<div> <div>
<table>
<tbody>
{props.persons.map(a => {props.persons.map(a =>
<li key= {a.name}> {a.name} {a.number} <tr key= {a.name}>
<button onClick = {function(){removeFunction(a.id)}}>poista</button> <td>{a.name}</td>
</li>) <td>{a.number}</td>
<td><button onClick = {function(){removeFunction(a.id)}}>poista</button></td>
</tr>)
} }
</tbody>
</table>
</div> </div>
)} )}
......
import axios from 'axios'; import axios from 'axios';
const baseUrl = 'http://localhost:3001/persons' const baseUrl = '/api/persons'
//const baseUrl = 'http://localhost:3001/api/persons'
const getAll = ()=> { const getAll = ()=> {
const request = axios.get(baseUrl) const request = axios.get(baseUrl)
...@@ -9,6 +12,7 @@ const getAll = ()=> { ...@@ -9,6 +12,7 @@ const getAll = ()=> {
const create = (newPerson) => { const create = (newPerson) => {
const request = axios.post(baseUrl, newPerson) const request = axios.post(baseUrl, newPerson)
return request.then(response => response.data) return request.then(response => response.data)
//.then(response => response.data)
} }
const remove = (id) => { const remove = (id) => {
console.log(`${baseUrl}/${id}`) console.log(`${baseUrl}/${id}`)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment