Upload 7.6
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import { useField } from "./hooks/hooks";
|
||||||
import { Route, Routes, Link, useMatch, useNavigate } from "react-router-dom";
|
import { Route, Routes, Link, useMatch, useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
const Menu = () => {
|
const Menu = () => {
|
||||||
@@ -37,7 +38,7 @@ const Anecdote = ({ anecdote }) => (
|
|||||||
</h2>
|
</h2>
|
||||||
<p>has {anecdote.votes} votes</p>
|
<p>has {anecdote.votes} votes</p>
|
||||||
<p>
|
<p>
|
||||||
for more info see <a href={anecdote.info}>{anecdote.info}</a>
|
for more info see <a href={anecdote.info}>here</a>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -90,51 +91,46 @@ const Footer = () => (
|
|||||||
|
|
||||||
const CreateNew = (props) => {
|
const CreateNew = (props) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [content, setContent] = useState("");
|
const { reset: contentReset, ...content } = useField("content");
|
||||||
const [author, setAuthor] = useState("");
|
const { reset: authorReset, ...author } = useField("author");
|
||||||
const [info, setInfo] = useState("");
|
const { reset: infoReset, ...info } = useField("info");
|
||||||
|
|
||||||
const handleSubmit = (e) => {
|
const handleSubmit = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
props.addNew({
|
props.addNew({
|
||||||
content,
|
content: content.value,
|
||||||
author,
|
author: author.value,
|
||||||
info,
|
info: info.value,
|
||||||
votes: 0,
|
votes: 0,
|
||||||
});
|
});
|
||||||
navigate("/");
|
navigate("/");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleReset = () => {
|
||||||
|
contentReset();
|
||||||
|
authorReset();
|
||||||
|
infoReset();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2>create a new anecdote</h2>
|
<h2>create a new anecdote</h2>
|
||||||
<form onSubmit={handleSubmit}>
|
<form onSubmit={handleSubmit}>
|
||||||
<div>
|
<div>
|
||||||
content
|
content
|
||||||
<input
|
<input {...content} />
|
||||||
name="content"
|
|
||||||
value={content}
|
|
||||||
onChange={(e) => setContent(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
author
|
author
|
||||||
<input
|
<input {...author} />
|
||||||
name="author"
|
|
||||||
value={author}
|
|
||||||
onChange={(e) => setAuthor(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
url for more info
|
url for more info
|
||||||
<input
|
<input {...info} />
|
||||||
name="info"
|
|
||||||
value={info}
|
|
||||||
onChange={(e) => setInfo(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<button>create</button>
|
<button>create</button>
|
||||||
</form>
|
</form>
|
||||||
|
<button onClick={handleReset}>reset</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
20
part7/routed-anecdotes/src/hooks/hooks.js
Normal file
20
part7/routed-anecdotes/src/hooks/hooks.js
Normal file
@@ -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,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user