diff --git a/src/components/AnecdoteForm.js b/src/components/AnecdoteForm.js
index c0c4253..03d1221 100644
--- a/src/components/AnecdoteForm.js
+++ b/src/components/AnecdoteForm.js
@@ -1,7 +1,6 @@
import React from "react";
import { useDispatch } from "react-redux";
import { addAnecdote } from "../reducers/anecdoteReducer";
-import anecdoteService from 'services/anecdotes';
import { notificationSet } from 'reducers/notificationReducer';
const AnecdoteForm = (props) => {
@@ -10,22 +9,8 @@ const AnecdoteForm = (props) => {
const newAnecdote = (event) => {
event.preventDefault();
const anecdoteContent = event.target.content.value;
-
- //Without async action creators
- // anecdoteService.createNew(anecdoteContent)
- // .then(response => {
- // console.log('response: ', response)
- // dispatch(addAnecdote(response))
- // event.target.content.value = "";
- // })
-
- //With async action creators
dispatch(addAnecdote(anecdoteContent))
.then(()=>{
- // dispatch(notificationSet(`New anecdote ${anecdoteContent}`))
- // setTimeout(()=>{
- // dispatch(notificationRemove())
- // }, 5000)
dispatch(notificationSet(anecdoteContent, 5000))
event.target.content.value = ""
})
diff --git a/src/components/AnecdoteList.js b/src/components/AnecdoteList.js
index 256d179..27d88aa 100644
--- a/src/components/AnecdoteList.js
+++ b/src/components/AnecdoteList.js
@@ -14,7 +14,6 @@ const AnecdoteList = (props) => {
const sortedAnecdotes = anecdotes.sort((a, b) => b.votes - a.votes);
const vote = (id, votes) => {
- // console.log("vote", id);
dispatch(voteAnecdote(id, votes+1));
};
diff --git a/src/components/Filter.js b/src/components/Filter.js
index 025c970..67344c1 100644
--- a/src/components/Filter.js
+++ b/src/components/Filter.js
@@ -1,5 +1,5 @@
import React from 'react'
-import { useSelector, useDispatch } from 'react-redux'
+import { useDispatch } from 'react-redux'
const Filter = () => {
const dispatch = useDispatch()
diff --git a/src/reducers/anecdoteReducer.js b/src/reducers/anecdoteReducer.js
index f816262..b6e342a 100644
--- a/src/reducers/anecdoteReducer.js
+++ b/src/reducers/anecdoteReducer.js
@@ -1,14 +1,5 @@
import anecdoteService from 'services/anecdotes';
-const anecdotesAtStart = [
- "If it hurts, do it more often",
- "Adding manpower to a late software project makes it later!",
- "The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.",
- "Any fool can write code that a computer can understand. Good programmers write code that humans can understand.",
- "Premature optimization is the root of all evil.",
- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
-];
-
const getId = () => (100000 * Math.random()).toFixed(0);
const asObject = (anecdote) => {
@@ -19,19 +10,13 @@ const asObject = (anecdote) => {
};
};
-const initialState = anecdotesAtStart.map(asObject);
-
const reducer = (state=[], action) => {
- // console.log("state now: ", state);
- // console.log("action", action);
-
switch (action.type) {
case "VOTE_ANECDOTE":
const anecdoteToBeVoted = state.filter(
(anecdote) => anecdote.id === action.data.id
)[0];
anecdoteToBeVoted.votes += 1;
- // console.log(anecdoteToBeVoted);
return state.map((anecdote) =>
anecdote.id !== action.data.id ? anecdote : anecdoteToBeVoted
);
@@ -47,11 +32,6 @@ const reducer = (state=[], action) => {
//Action creators
export const addAnecdote = (anecdote) => {
- // const noteToAdd = asObject(content);
- // return {
- // type: "NEW_ANECDOTE",
- // data: anecdote
- // };
return async (dispatch)=>{
const response = await anecdoteService.createNew(anecdote);
dispatch({
@@ -63,10 +43,7 @@ export const addAnecdote = (anecdote) => {
};
export const voteAnecdote = (id, votes) => {
- // return {
- // type: "VOTE_ANECDOTE",
- // data: { id }
- // };
+
return async (dispatch)=>{
const response = await anecdoteService.voteAnecdote(id, votes);
dispatch({
@@ -77,10 +54,6 @@ export const voteAnecdote = (id, votes) => {
};
export const initAnecdotes = () => {
- // return {
- // type: "INIT",
- // data: anecdotes
- // };
return async (dispatch) => {
const anecdotes = await anecdoteService.getAll();
dispatch({
diff --git a/src/reducers/notificationReducer.js b/src/reducers/notificationReducer.js
index a7456ff..d36c6d9 100644
--- a/src/reducers/notificationReducer.js
+++ b/src/reducers/notificationReducer.js
@@ -17,10 +17,6 @@ const reducer = (state=notificationInitialState, action) =>{
//action creators
export const notificationSet = (message, timeout)=>{
- // return {
- // type:"SET",
- // data: message
- // }
return async (dispatch)=>{
dispatch({
type: "SET",
From 58588b7c9565766be804e55b67c5f3c189e79149 Mon Sep 17 00:00:00 2001
From: saperez17
Date: Mon, 13 Sep 2021 01:29:34 -0500
Subject: [PATCH 5/5] Connect part6 6.19-6.21 exercises completed
---
db.json | 16 +++++++++++++---
src/components/AnecdoteForm.js | 14 +++++++++++---
src/components/AnecdoteList.js | 19 +++++++++++++++----
src/components/Filter.js | 22 ++++++++++++++++------
src/components/Notification.js | 19 ++++++++++++++-----
src/reducers/notificationReducer.js | 20 ++++++++++++++------
6 files changed, 83 insertions(+), 27 deletions(-)
diff --git a/db.json b/db.json
index d190146..5a59b2d 100644
--- a/db.json
+++ b/db.json
@@ -3,7 +3,7 @@
{
"content": "If it hurts, do it more often",
"id": 1,
- "votes": 5
+ "votes": 50
},
{
"content": "Adding manpower to a late software project makes it later!",
@@ -52,7 +52,7 @@
},
{
"content": "I like to grow silently, and demonstrate when the time is right",
- "votes": 0,
+ "votes": 1,
"id": 11
},
{
@@ -67,8 +67,18 @@
},
{
"content": "Tomorrow is work day, let's go!",
- "votes": 6,
+ "votes": 14,
"id": 14
+ },
+ {
+ "content": "Building a start-up from the groun-up is hard",
+ "votes": 0,
+ "id": 15
+ },
+ {
+ "content": "becoming a proficient Frontend engineer is my goal",
+ "votes": 1,
+ "id": 16
}
]
}
\ No newline at end of file
diff --git a/src/components/AnecdoteForm.js b/src/components/AnecdoteForm.js
index 03d1221..5d42b61 100644
--- a/src/components/AnecdoteForm.js
+++ b/src/components/AnecdoteForm.js
@@ -1,5 +1,5 @@
import React from "react";
-import { useDispatch } from "react-redux";
+import { useDispatch, connect } from "react-redux";
import { addAnecdote } from "../reducers/anecdoteReducer";
import { notificationSet } from 'reducers/notificationReducer';
@@ -11,7 +11,7 @@ const AnecdoteForm = (props) => {
const anecdoteContent = event.target.content.value;
dispatch(addAnecdote(anecdoteContent))
.then(()=>{
- dispatch(notificationSet(anecdoteContent, 5000))
+ props.notificationSet(anecdoteContent, 5000)
event.target.content.value = ""
})
};
@@ -29,4 +29,12 @@ const AnecdoteForm = (props) => {
);
};
-export default AnecdoteForm;
+const mapDispatchToProps = {
+ notificationSet
+}
+
+const ConnectedAnecdoteForm = connect(
+ null,
+ mapDispatchToProps
+)(AnecdoteForm)
+export default ConnectedAnecdoteForm;
diff --git a/src/components/AnecdoteList.js b/src/components/AnecdoteList.js
index 27d88aa..2ad8488 100644
--- a/src/components/AnecdoteList.js
+++ b/src/components/AnecdoteList.js
@@ -1,6 +1,7 @@
import React from "react";
-import { useSelector, useDispatch } from "react-redux";
+import { useSelector, connect } from "react-redux";
import { voteAnecdote } from "../reducers/anecdoteReducer";
+import { notificationSet } from 'reducers/notificationReducer';
const AnecdoteList = (props) => {
const anecdotes = useSelector((state) => {
@@ -9,12 +10,13 @@ const AnecdoteList = (props) => {
}
return state.anecdotes
});
- const dispatch = useDispatch();
const sortedAnecdotes = anecdotes.sort((a, b) => b.votes - a.votes);
const vote = (id, votes) => {
- dispatch(voteAnecdote(id, votes+1));
+ let votedAnecdote = anecdotes.find(anecdote => anecdote.id === id);
+ props.voteAnecdote(id, votes+1);
+ props.notificationSet(`+1 vote for ${votedAnecdote.content}`, 5000);
};
return (
@@ -32,4 +34,13 @@ const AnecdoteList = (props) => {
);
};
-export default AnecdoteList;
+const mapDispatchToProps = {
+ notificationSet,
+ voteAnecdote
+}
+
+const ConnectedAnecdoteList = connect(
+ null,
+ mapDispatchToProps
+)(AnecdoteList)
+export default ConnectedAnecdoteList;
diff --git a/src/components/Filter.js b/src/components/Filter.js
index 67344c1..937abda 100644
--- a/src/components/Filter.js
+++ b/src/components/Filter.js
@@ -1,12 +1,10 @@
import React from 'react'
-import { useDispatch } from 'react-redux'
+import { connect } from 'react-redux'
-const Filter = () => {
- const dispatch = useDispatch()
+const Filter = (props) => {
const handleChange = (event) => {
- // input-field value is in variable event.target.value
const filterValue = event.target.value
- dispatch({type:'CHANGE', data:filterValue})
+ props.changeFilter(filterValue);
}
const style = {
marginBottom: 10
@@ -19,4 +17,16 @@ const Filter = () => {
)
}
-export default Filter
\ No newline at end of file
+const mapDispatchToProps = dispatch => {
+ return {
+ changeFilter: value => {
+ dispatch( {type:'CHANGE', data:value})
+ }
+ }
+}
+
+const ConnectedFilter = connect(
+ null,
+ mapDispatchToProps
+)(Filter)
+export default ConnectedFilter
\ No newline at end of file
diff --git a/src/components/Notification.js b/src/components/Notification.js
index 7f9ed82..e2b1d9f 100644
--- a/src/components/Notification.js
+++ b/src/components/Notification.js
@@ -1,8 +1,7 @@
import React from 'react'
-import { useSelector } from 'react-redux'
+import { connect } from 'react-redux'
-const Notification = () => {
- const notification = useSelector(state => state.notification)
+const Notification = (props) => {
const style = {
border: 'solid',
padding: 10,
@@ -10,9 +9,19 @@ const Notification = () => {
}
return (
- {notification}
+ {props.notification.message}
)
}
-export default Notification
\ No newline at end of file
+const mapStateToProps = state => {
+ return {
+ notification: state.notification
+ }
+}
+
+const ConnectedNotification = connect(
+ mapStateToProps,
+ null
+)(Notification)
+export default ConnectedNotification
\ No newline at end of file
diff --git a/src/reducers/notificationReducer.js b/src/reducers/notificationReducer.js
index d36c6d9..761098f 100644
--- a/src/reducers/notificationReducer.js
+++ b/src/reducers/notificationReducer.js
@@ -1,5 +1,8 @@
-const notificationInitialState = "Welcome"
+const notificationInitialState = {
+ message: "Welcome",
+}
+let timeoutID;
const reducer = (state=notificationInitialState, action) =>{
switch(action.type){
@@ -7,7 +10,9 @@ const reducer = (state=notificationInitialState, action) =>{
return action.data
}
case 'CLEAR':{
- return ""
+ return {
+ message: '',
+ }
}
default:{
return state
@@ -18,13 +23,16 @@ const reducer = (state=notificationInitialState, action) =>{
//action creators
export const notificationSet = (message, timeout)=>{
return async (dispatch)=>{
+ if (timeoutID !== undefined){
+ clearTimeout(timeoutID)
+ }
+ timeoutID = setTimeout(()=>{
+ dispatch({ type: "CLEAR" })
+ }, timeout)
dispatch({
type: "SET",
- data: message
+ data: {message}
})
- setTimeout(()=>{
- dispatch({ type: "CLEAR" })
- }, timeout)
}
}