Skip to content

Commit

Permalink
App component
Browse files Browse the repository at this point in the history
  • Loading branch information
mluukkai committed Jun 13, 2019
1 parent e423b20 commit c8f2eb8
Showing 1 changed file with 70 additions and 21 deletions.
91 changes: 70 additions & 21 deletions src/App.js
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

0 comments on commit c8f2eb8

Please sign in to comment.