Upload 6.19

This commit is contained in:
Andrew Trieu
2023-06-24 15:38:19 +03:00
parent 0f6e5c1991
commit 417e182d52
7 changed files with 61 additions and 15 deletions

View File

@@ -3,12 +3,12 @@
{ {
"content": "If it hurts, do it more often", "content": "If it hurts, do it more often",
"id": "47145", "id": "47145",
"votes": 0 "votes": 1
}, },
{ {
"content": "Adding manpower to a late software project makes it later!", "content": "Adding manpower to a late software project makes it later!",
"id": "21149", "id": "21149",
"votes": 0 "votes": 2
}, },
{ {
"content": "The first 90 percent of the code accounts for the first 10 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.", "content": "The first 90 percent of the code accounts for the first 10 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.",
@@ -29,6 +29,11 @@
"content": "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.", "content": "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.",
"id": "98312", "id": "98312",
"votes": 0 "votes": 0
},
{
"content": "New",
"votes": 0,
"id": "mPUXy_Q"
} }
] ]
} }

View File

@@ -1,10 +1,17 @@
import React from "react"; import React from "react";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import List from "./components/List"; import List from "./components/List";
import Form from "./components/Form"; import Form from "./components/Form";
import Filter from "./components/Filter"; import Filter from "./components/Filter";
import Notification from "./components/Notification"; import Notification from "./components/Notification";
import { initializeAnecdotes } from "./reducers/anecdoteReducer";
const App = () => { const App = () => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(initializeAnecdotes());
}, [dispatch]);
return ( return (
<div> <div>
<h1>Anecdotes</h1> <h1>Anecdotes</h1>

View File

@@ -1,8 +1,7 @@
import React from "react"; import React from "react";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
import { add } from "../reducers/anecdoteReducer";
import { setNotification } from "../reducers/notificationReducer"; import { setNotification } from "../reducers/notificationReducer";
import anecdoteService from "../services/anecdotes"; import { addAnecdote } from "../reducers/anecdoteReducer";
const Form = () => { const Form = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@@ -11,9 +10,8 @@ const Form = () => {
event.preventDefault(); event.preventDefault();
const content = event.target.anecdote.value; const content = event.target.anecdote.value;
event.target.anecdote.value = ""; event.target.anecdote.value = "";
const newAnecdote = await anecdoteService.createNew(content); dispatch(addAnecdote(content));
dispatch(add(newAnecdote)); dispatch(setNotification(`You added "${content}"`, 5));
dispatch(setNotification(`You added "${content}"`));
}; };
return ( return (

View File

@@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { vote } from "../reducers/anecdoteReducer"; import { voteAnecdote } from "../reducers/anecdoteReducer";
import { setNotification } from "../reducers/notificationReducer"; import { setNotification } from "../reducers/notificationReducer";
const List = () => { const List = () => {
@@ -12,12 +12,13 @@ const List = () => {
}); });
const handleVote = (id) => { const handleVote = (id) => {
dispatch(vote(id)); dispatch(voteAnecdote(id));
dispatch( dispatch(
setNotification( setNotification(
`You voted for "${ `You voted for "${
anecdotes.find((anecdote) => anecdote.id === id).content anecdotes.find((anecdote) => anecdote.id === id).content
}"` }"`,
5
) )
); );
}; };

View File

@@ -1,4 +1,5 @@
import { createSlice } from "@reduxjs/toolkit"; import { createSlice } from "@reduxjs/toolkit";
import anecdoteService from "../services/anecdotes";
const anecdoteSlice = createSlice({ const anecdoteSlice = createSlice({
name: "anecdotes", name: "anecdotes",
@@ -13,8 +14,33 @@ const anecdoteSlice = createSlice({
const anecdote = action.payload; const anecdote = action.payload;
state.push(anecdote); state.push(anecdote);
}, },
set(state, action) {
return action.payload;
},
}, },
}); });
export const { vote, add } = anecdoteSlice.actions; export const { vote, add, set } = anecdoteSlice.actions;
export const initializeAnecdotes = () => {
return async (dispatch) => {
const anecdotes = await anecdoteService.getAll();
dispatch(set(anecdotes));
};
};
export const addAnecdote = (content) => {
return async (dispatch) => {
const anecdote = await anecdoteService.createNew(content);
dispatch(add(anecdote));
};
};
export const voteAnecdote = (id) => {
return async (dispatch) => {
await anecdoteService.addVote(id);
dispatch(vote(id));
};
};
export default anecdoteSlice.reducer; export default anecdoteSlice.reducer;

View File

@@ -15,12 +15,12 @@ const notificationSlice = createSlice({
export const { showNotification, hideNotification } = notificationSlice.actions; export const { showNotification, hideNotification } = notificationSlice.actions;
export const setNotification = (text) => { export const setNotification = (text, time) => {
return (dispatch) => { return (dispatch) => {
dispatch(showNotification(text)); dispatch(showNotification(text));
setTimeout(() => { setTimeout(() => {
dispatch(hideNotification()); dispatch(hideNotification());
}, 5000); }, time * 1000);
}; };
}; };
export default notificationSlice.reducer; export default notificationSlice.reducer;

View File

@@ -15,5 +15,14 @@ const createNew = async (content) => {
return response.data; return response.data;
}; };
const addVote = async (id) => {
const anecdotes = await getAll();
const anecdote = anecdotes.find((a) => a.id === id);
console.log(anecdote);
const voted = { ...anecdote, votes: anecdote.votes + 1 };
const response = await axios.put(`${baseUrl}/${id}`, voted);
return response.data;
};
// eslint-disable-next-line import/no-anonymous-default-export // eslint-disable-next-line import/no-anonymous-default-export
export default { getAll, createNew }; export default { getAll, createNew, addVote };