diff --git a/part7/routed-anecdotes/src/App.js b/part7/routed-anecdotes/src/App.js index f648cbf..0dac343 100644 --- a/part7/routed-anecdotes/src/App.js +++ b/part7/routed-anecdotes/src/App.js @@ -1,4 +1,5 @@ import { useState } from "react"; +import { useField } from "./hooks/hooks"; import { Route, Routes, Link, useMatch, useNavigate } from "react-router-dom"; const Menu = () => { @@ -37,7 +38,7 @@ const Anecdote = ({ anecdote }) => (

has {anecdote.votes} votes

- for more info see {anecdote.info} + for more info see here

); @@ -90,51 +91,46 @@ const Footer = () => ( const CreateNew = (props) => { const navigate = useNavigate(); - const [content, setContent] = useState(""); - const [author, setAuthor] = useState(""); - const [info, setInfo] = useState(""); + const { reset: contentReset, ...content } = useField("content"); + const { reset: authorReset, ...author } = useField("author"); + const { reset: infoReset, ...info } = useField("info"); const handleSubmit = (e) => { e.preventDefault(); props.addNew({ - content, - author, - info, + content: content.value, + author: author.value, + info: info.value, votes: 0, }); navigate("/"); }; + const handleReset = () => { + contentReset(); + authorReset(); + infoReset(); + }; + return (

create a new anecdote

content - setContent(e.target.value)} - /> +
author - setAuthor(e.target.value)} - /> +
url for more info - setInfo(e.target.value)} - /> +
+
); }; diff --git a/part7/routed-anecdotes/src/hooks/hooks.js b/part7/routed-anecdotes/src/hooks/hooks.js new file mode 100644 index 0000000..8f1c21e --- /dev/null +++ b/part7/routed-anecdotes/src/hooks/hooks.js @@ -0,0 +1,20 @@ +import { useState } from "react"; + +export const useField = (name) => { + const [value, setValue] = useState(""); + + const onChange = (event) => { + setValue(event.target.value); + }; + + const reset = () => { + setValue(""); + }; + + return { + name, + value, + onChange, + reset, + }; +};