import { useState } from "react";
import { Route, Routes, Link, useMatch, useNavigate } from "react-router-dom";
const Menu = () => {
const padding = {
paddingRight: 5,
};
return (
anecdotes
create new
about
);
};
const Notification = ({ notification }) => {
const style = {
border: "solid",
padding: 10,
borderWidth: 1,
color: "green",
};
return {notification}
;
};
const Anecdote = ({ anecdote }) => (
{anecdote.content} by {anecdote.author}
has {anecdote.votes} votes
for more info see{anecdote.info}
);
const AnecdoteList = ({ anecdotes }) => (
Anecdotes
{anecdotes.map((anecdote) => (
{anecdote.content}
))}
);
const About = () => (
About anecdote app
According to Wikipedia:
An anecdote is a brief, revealing account of an individual person or an
incident. Occasionally humorous, anecdotes differ from jokes because their
primary purpose is not simply to provoke laughter but to reveal a truth
more general than the brief tale itself, such as to characterize a person
by delineating a specific quirk or trait, to communicate an abstract idea
about a person, place, or thing through the concrete details of a short
narrative. An anecdote is "a story with a point."
Software engineering is full of excellent anecdotes, at this app you can
find the best and add more.
);
const Footer = () => (
);
const CreateNew = (props) => {
const navigate = useNavigate();
const [content, setContent] = useState("");
const [author, setAuthor] = useState("");
const [info, setInfo] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
props.addNew({
content,
author,
info,
votes: 0,
});
navigate("/");
};
return (
);
};
const App = () => {
const [anecdotes, setAnecdotes] = useState([
{
content: "If it hurts, do it more often",
author: "Jez Humble",
info: "https://martinfowler.com/bliki/FrequencyReducesDifficulty.html",
votes: 0,
id: 1,
},
{
content: "Premature optimization is the root of all evil",
author: "Donald Knuth",
info: "http://wiki.c2.com/?PrematureOptimization",
votes: 0,
id: 2,
},
]);
const [notification, setNotification] = useState("");
const addNew = (anecdote) => {
anecdote.id = Math.round(Math.random() * 10000);
setAnecdotes(anecdotes.concat(anecdote));
setNotification(
`A new anecdote ${anecdote.content} has been created by ${anecdote.author}!`
);
setTimeout(() => {
setNotification("");
}, 5000);
};
const anecdoteById = (id) => anecdotes.find((a) => a.id === id);
const vote = (id) => {
const anecdote = anecdoteById(id);
const voted = {
...anecdote,
votes: anecdote.votes + 1,
};
setAnecdotes(anecdotes.map((a) => (a.id === id ? voted : a)));
};
const match = useMatch("/anecdotes/:id");
const anecdote = match
? anecdotes.find((a) => a.id === Number(match.params.id))
: null;
return (
{notification !== "" && }
}
/>
} />
}
/>
} />
} />
);
};
export default App;