diff --git a/Part_6/anecdotes-redux/src/components/AnecdoteList.js b/Part_6/anecdotes-redux/src/components/AnecdoteList.js
index 10781d3..0739369 100644
--- a/Part_6/anecdotes-redux/src/components/AnecdoteList.js
+++ b/Part_6/anecdotes-redux/src/components/AnecdoteList.js
@@ -1,5 +1,5 @@
import { useSelector, useDispatch } from 'react-redux'
-import { voteAnecdote } from '../reducers/anecdoteReducer'
+import { vote } from '../reducers/anecdoteReducer'
import { notifChange } from '../reducers/notificationReducer'
const AnecdoteList = () => {
@@ -13,8 +13,8 @@ const AnecdoteList = () => {
const dispatch = useDispatch()
- const vote = id => {
- dispatch(voteAnecdote(id))
+ const voteAnec = anecdote => {
+ dispatch(vote(anecdote))
dispatch(notifChange('Voted Anecdote'))
setTimeout(() => {
dispatch(notifChange(''))
@@ -31,7 +31,7 @@ const AnecdoteList = () => {
Has {anecdote.votes} Votes
-
+
)}
diff --git a/Part_6/anecdotes-redux/src/reducers/anecdoteReducer.js b/Part_6/anecdotes-redux/src/reducers/anecdoteReducer.js
index 543d446..07d1a91 100644
--- a/Part_6/anecdotes-redux/src/reducers/anecdoteReducer.js
+++ b/Part_6/anecdotes-redux/src/reducers/anecdoteReducer.js
@@ -1,5 +1,6 @@
import { createSlice } from '@reduxjs/toolkit'
import anecdoteService from '../services/anecdote'
+import axios from 'axios'
const getId = () => (100000 * Math.random()).toFixed(0)
@@ -8,7 +9,7 @@ const anecdoteSlice = createSlice({
initialState: [],
reducers: {
voteAnecdote(state, action) {
- const id = action.payload
+ const id = action.payload.id
const anecdoteToUpdate = state.find(a => a.id === id)
const updatedAnecdote = {
...anecdoteToUpdate,
@@ -42,4 +43,12 @@ export const createAnecdote = content => {
}
}
+export const vote = content => {
+ return async dispatch => {
+ const anecToUpdate = { ...content, votes: content.votes + 1 }
+ const updatedAnecdote = await anecdoteService.update(anecToUpdate)
+ dispatch(voteAnecdote(updatedAnecdote))
+ }
+}
+
export default anecdoteSlice.reducer
\ No newline at end of file
diff --git a/Part_6/anecdotes-redux/src/services/anecdote.js b/Part_6/anecdotes-redux/src/services/anecdote.js
index c054cd8..c26a4c3 100644
--- a/Part_6/anecdotes-redux/src/services/anecdote.js
+++ b/Part_6/anecdotes-redux/src/services/anecdote.js
@@ -15,4 +15,10 @@ const createNew = async content => {
return response.data
}
-export default { getAll, createNew }
\ No newline at end of file
+const update = async object => {
+ const response = await axios.put(`${baseUrl}/${object.id}`, object)
+
+ return response.data
+}
+
+export default { getAll, createNew, update }
\ No newline at end of file