diff --git a/Part_7/routed-anecdotes/src/App.js b/Part_7/routed-anecdotes/src/App.js index 07437ae..172a608 100644 --- a/Part_7/routed-anecdotes/src/App.js +++ b/Part_7/routed-anecdotes/src/App.js @@ -5,9 +5,11 @@ import Menu from './components/Menu' import About from './components/About' import Footer from './components/Footer' import CreateNew from './components/CreateNew' -import { Navigate, Route, Routes, useMatch } from 'react-router-dom' +import { useNavigate, Navigate, Route, Routes, useMatch } from 'react-router-dom' +import Notification from './components/Notification' const App = () => { + const [notification, setNotification] = useState('') const [anecdotes, setAnecdotes] = useState([ { content: 'If it hurts, do it more often', @@ -25,14 +27,18 @@ const App = () => { } ]) + const navigate = useNavigate() const match = useMatch('/anecdotes/:id') const anecdote = match ? anecdotes.find(a => a.id === Number(match.params.id)) : null - const [notification, setNotification] = useState('') - const addNew = (anecdote) => { anecdote.id = Math.round(Math.random() * 10000) setAnecdotes(anecdotes.concat(anecdote)) + setNotification(`Added ${anecdote.content}`) + setTimeout(() => { + setNotification('') + }, 5000) + navigate('/') } const anecdoteById = (id) => @@ -54,6 +60,7 @@ const App = () => {

Software Anecdotes

+
diff --git a/Part_7/routed-anecdotes/src/components/Notification.js b/Part_7/routed-anecdotes/src/components/Notification.js new file mode 100644 index 0000000..a659ca5 --- /dev/null +++ b/Part_7/routed-anecdotes/src/components/Notification.js @@ -0,0 +1,19 @@ +const Notification = ({ notification }) => { + const style = { + padding: 10, + margin: 10, + border: 'solid', + borderRadius: 5, + borderWidth: 3 + } + + if (notification === '') return null + + return ( +
+ {notification} +
+ ) +} + +export default Notification \ No newline at end of file