-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,75 @@ | ||
import React from 'react'; | ||
import logo from './logo.svg'; | ||
import './App.css'; | ||
import React, { useState, useEffect } from 'react' | ||
import axios from 'axios' | ||
|
||
|
||
const useField = (type) => { | ||
const [value, setValue] = useState('') | ||
|
||
const onChange = (event) => { | ||
setValue(event.target.value) | ||
} | ||
|
||
return { | ||
type, | ||
value, | ||
onChange | ||
} | ||
} | ||
|
||
const useResource = (baseUrl) => { | ||
const [resources, setResources] = useState([]) | ||
|
||
// ... | ||
|
||
const create = (resource) => { | ||
// ... | ||
} | ||
|
||
const service = { | ||
create | ||
} | ||
|
||
return [ | ||
resources, service | ||
] | ||
} | ||
|
||
const App = () => { | ||
const content = useField('text') | ||
const name = useField('text') | ||
const number = useField('text') | ||
|
||
const [notes, noteService] = useResource('http://localhost:3005/notes') | ||
const [persons, personService] = useResource('http://localhost:3005/persons') | ||
|
||
const handleNoteSubmit = (event) => { | ||
event.preventDefault() | ||
noteService.create({ content: content.value }) | ||
} | ||
|
||
const handlePersonSubmit = (event) => { | ||
event.preventDefault() | ||
personService.create({ name: name.value, number: number.value}) | ||
} | ||
|
||
function App() { | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<p> | ||
Edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<a | ||
className="App-link" | ||
href="https://reactjs.org" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn React | ||
</a> | ||
</header> | ||
<div> | ||
<h2>notes</h2> | ||
<form onSubmit={handleNoteSubmit}> | ||
<input {...content} /> | ||
<button>create</button> | ||
</form> | ||
{notes.map(n => <p key={n.id}>{n.content}</p>)} | ||
|
||
<h2>persons</h2> | ||
<form onSubmit={handlePersonSubmit}> | ||
name <input {...name} /> <br/> | ||
number <input {...number} /> | ||
<button>create</button> | ||
</form> | ||
{persons.map(n => <p key={n.id}>{n.name} {n.number}</p>)} | ||
</div> | ||
); | ||
) | ||
} | ||
|
||
export default App; | ||
export default App |