From c8f2eb8c12df2bcb152a50eee5f3b90ffe347fc5 Mon Sep 17 00:00:00 2001 From: Matti Luukkainen Date: Thu, 13 Jun 2019 18:11:24 +0300 Subject: [PATCH] App component --- src/App.js | 91 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/src/App.js b/src/App.js index ce9cbd2..274b18d 100644 --- a/src/App.js +++ b/src/App.js @@ -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 ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+
+

notes

+
+ + +
+ {notes.map(n =>

{n.content}

)} + +

persons

+
+ name
+ number + +
+ {persons.map(n =>

{n.name} {n.number}

)}
- ); + ) } -export default App; +export default App \ No newline at end of file