parent
63782b8197
commit
dbe1689e8c
@ -0,0 +1 @@
|
|||||||
|
REACT_APP_API_URL=https://afternoon-escarpment-71350.herokuapp.com/api/contacts
|
||||||
@ -0,0 +1 @@
|
|||||||
|
REACT_APP_API_URL=PutYourAPIUrlHere
|
||||||
@ -1,12 +1,72 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import axios from 'axios';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
|
|
||||||
function App() {
|
import Form from './components/Form';
|
||||||
return (
|
|
||||||
<div className="App">
|
class App extends React.Component {
|
||||||
hello world
|
state = {
|
||||||
|
contacts: []
|
||||||
|
}
|
||||||
|
|
||||||
|
getContacts = () => {
|
||||||
|
// ** don't forget to create an env file with your api url! **
|
||||||
|
axios.get(process.env.REACT_APP_API_URL)
|
||||||
|
.then(res => {
|
||||||
|
this.setState({
|
||||||
|
contacts: res.data
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteContact = (e) => {
|
||||||
|
axios.delete(`${process.env.REACT_APP_API_URL}/${e.target.value}`).then(res => {
|
||||||
|
this.getContacts();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
createContact = (contactInfo) => {
|
||||||
|
axios.post(process.env.REACT_APP_API_URL, contactInfo).then(res => {
|
||||||
|
this.getContacts();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
updateContact = (updateInfo, id) => {
|
||||||
|
axios.put(`${process.env.REACT_APP_API_URL}/${id}`, updateInfo).then(res => {
|
||||||
|
this.getContacts();
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount = () => {
|
||||||
|
this.getContacts();
|
||||||
|
}
|
||||||
|
|
||||||
|
render = () => {
|
||||||
|
return <div>
|
||||||
|
<h2>Contacts</h2>
|
||||||
|
<ul>
|
||||||
|
{
|
||||||
|
this.state.contacts.length > 0 ? this.state.contacts.map(
|
||||||
|
(contact, index) => {
|
||||||
|
return (
|
||||||
|
<li key={contact.id}>
|
||||||
|
{contact.name}: {contact.age}
|
||||||
|
<button value={contact.id} onClick={this.deleteContact}>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
<details><summary>Edit</summary>
|
||||||
|
<Form contact={contact} updateContact={this.updateContact}/>
|
||||||
|
</details>
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
) : null
|
||||||
|
}
|
||||||
|
</ul>
|
||||||
|
<h3>Add a Contact</h3>
|
||||||
|
<Form createContact={this.createContact} />
|
||||||
</div>
|
</div>
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
class Form extends React.Component {
|
||||||
|
state = {
|
||||||
|
name: '',
|
||||||
|
age: null
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSubmit = (e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
if(this.props.createContact) {
|
||||||
|
this.props.createContact({
|
||||||
|
name: this.state.name,
|
||||||
|
age: parseInt(this.state.age)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
this.props.updateContact({
|
||||||
|
name: this.state.name,
|
||||||
|
age: parseInt(this.state.age)
|
||||||
|
}, this.state.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleChange = (e) => {
|
||||||
|
this.setState({
|
||||||
|
[e.target.id]: e.target.value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount = () => {
|
||||||
|
if(this.props.contact) {
|
||||||
|
this.setState(this.props.contact)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<form onSubmit ={this.handleSubmit}>
|
||||||
|
<input type="text" id="name" placeholder="name" onChange={this.handleChange} defaultValue={this.state.name}></input>
|
||||||
|
<input type="number" id="age" placeholder="age" onChange={this.handleChange} defaultValue={this.state.age}></input>
|
||||||
|
<input type="submit" value="Submit"/>
|
||||||
|
</form>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Form;
|
||||||
Loading…
Reference in new issue