Upload 6.13

This commit is contained in:
Andrew Trieu
2023-06-24 14:07:17 +03:00
parent e9de84103d
commit ebb739d6a7
10 changed files with 179 additions and 147 deletions

View File

@@ -1,18 +1,19 @@
import React from 'react' import React from "react";
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";
const App = () => { const App = () => {
return ( return (
<div> <div>
<h1>Anecdotes</h1> <h1>Anecdotes</h1>
<Notification />
<Filter /> <Filter />
<List /> <List />
<Form /> <Form />
</div> </div>
) );
} };
export default App export default App;

View File

@@ -18,6 +18,6 @@ const Filter = () => {
Filter <input onChange={handleChange} /> Filter <input onChange={handleChange} />
</div> </div>
); );
} };
export default Filter; export default Filter;

View File

@@ -1,6 +1,7 @@
import React from 'react' import React from "react";
import { useDispatch } from 'react-redux' import { useDispatch } from "react-redux";
import { addAnecdote } from '../reducers/anecdoteReducer' import { add } from "../reducers/anecdoteReducer";
import { setNotification } from "../reducers/notificationReducer";
const Form = () => { const Form = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@@ -8,8 +9,9 @@ const Form = () => {
const handleNew = (event) => { const handleNew = (event) => {
event.preventDefault(); event.preventDefault();
const content = event.target.anecdote.value; const content = event.target.anecdote.value;
event.target.anecdote.value = ''; event.target.anecdote.value = "";
dispatch(addAnecdote(content)); dispatch(add(content));
dispatch(setNotification(`You added "${content}"`));
}; };
return ( return (

View File

@@ -1,24 +1,38 @@
import React from 'react' import React from "react";
import { useDispatch, useSelector } from 'react-redux' import { useDispatch, useSelector } from "react-redux";
import { addVote } from '../reducers/anecdoteReducer' import { vote } from "../reducers/anecdoteReducer";
import { setNotification } from "../reducers/notificationReducer";
const List = () => { const List = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const anecdotes = useSelector(({ anecdotes, filter }) => { const anecdotes = useSelector(({ anecdotes, filter }) => {
return anecdotes.filter((anecdote) => anecdote.content.toLowerCase().includes(filter.toLowerCase())); return anecdotes.filter((anecdote) =>
anecdote.content.toLowerCase().includes(filter.toLowerCase())
);
}); });
const handleVote = (id) => { const handleVote = (id) => {
dispatch(addVote(id)); dispatch(vote(id));
} dispatch(
setNotification(
`You voted for "${
anecdotes.find((anecdote) => anecdote.id === id).content
}"`
)
);
};
return ( return (
<div> <div>
<h2>List of anecdotes</h2> <h2>List of anecdotes</h2>
{anecdotes.map((anecdote) => ( {anecdotes
.sort((a, b) => b.votes - a.votes)
.map((anecdote) => (
<div key={anecdote.id}> <div key={anecdote.id}>
<div> <div>
<p>"{anecdote.content}"&nbsp;has&nbsp;{anecdote.votes}&nbsp;votes<br /> <p>
"{anecdote.content}"&nbsp;has&nbsp;{anecdote.votes}&nbsp;votes
<br />
<button onClick={() => handleVote(anecdote.id)}>Vote</button> <button onClick={() => handleVote(anecdote.id)}>Vote</button>
</p> </p>
</div> </div>

View File

@@ -1,14 +1,15 @@
const Notification = () => { import React from "react";
const style = { import { useSelector } from "react-redux";
border: 'solid',
padding: 10,
borderWidth: 1
}
return (
<div style={style}>
render here notification...
</div>
)
}
export default Notification const Notification = () => {
const notification = useSelector((state) => state.notification);
const style = {
padding: 10,
borderWidth: 1,
color: "green",
};
return <div style={style}>{notification}</div>;
};
export default Notification;

View File

@@ -1,20 +1,11 @@
import React from 'react' import React from "react";
import ReactDOM from 'react-dom/client' import ReactDOM from "react-dom/client";
import { createStore, combineReducers } from 'redux' import { Provider } from "react-redux";
import { Provider } from 'react-redux' import store from "./reducers/store";
import App from './App' import App from "./App";
import anecdoteReducer from './reducers/anecdoteReducer'
import filterReducer from './reducers/filterReducer'
const reducer = combineReducers({ ReactDOM.createRoot(document.getElementById("root")).render(
anecdotes: anecdoteReducer,
filter: filterReducer,
})
const store = createStore(reducer)
ReactDOM.createRoot(document.getElementById('root')).render(
<Provider store={store}> <Provider store={store}>
<App /> <App />
</Provider> </Provider>
) );

View File

@@ -1,62 +1,45 @@
const anecdotesAtStart = [ import { createSlice } from "@reduxjs/toolkit";
'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 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) => { const asObject = (anecdote) => {
return { return {
content: anecdote, content: anecdote,
id: getId(), id: getId(),
votes: 0 votes: 0,
}
}
const initialState = anecdotesAtStart.map(asObject)
export const addVote = (id) => {
return {
type: "VOTE",
data: { id },
}; };
}; };
export const addAnecdote = (newAnecdote) => { const initialState = anecdotesAtStart.map(asObject);
return {
type: "NEW", const anecdoteSlice = createSlice({
data: { name: "anecdotes",
content: newAnecdote, initialState,
reducers: {
vote(state, action) {
const id = action.payload;
const anecdote = state.find((a) => a.id === id);
anecdote.votes += 1;
},
add(state, action) {
const anecdote = action.payload;
state.push({
content: anecdote,
id: getId(), id: getId(),
votes: 0, votes: 0,
});
}, },
}; },
}; });
const anecdoteReducer = (state = initialState, action) => { export const { vote, add } = anecdoteSlice.actions;
switch (action.type) { export default anecdoteSlice.reducer;
case "VOTE": {
const { id } = action.data;
const oldAnecdote = state.find((a) => a.id === id);
const updatedAnecdote = {
...oldAnecdote,
votes: oldAnecdote.votes + 1,
};
const mappedAnecdotes = state.map((anecdote) =>
anecdote.id !== id ? anecdote : updatedAnecdote
);
return mappedAnecdotes.sort((a, b) => b.votes - a.votes);
}
case "NEW": {
return [...state, action.data];
}
default:
return state;
}
};
export default anecdoteReducer

View File

@@ -0,0 +1,26 @@
import { createSlice } from "@reduxjs/toolkit";
const notificationSlice = createSlice({
name: "notification",
initialState: "",
reducers: {
showNotification(state, action) {
return action.payload;
},
hideNotification(state, action) {
return "";
},
},
});
export const { showNotification, hideNotification } = notificationSlice.actions;
export const setNotification = (text) => {
return (dispatch) => {
dispatch(showNotification(text));
setTimeout(() => {
dispatch(hideNotification());
}, 5000);
};
};
export default notificationSlice.reducer;

View File

@@ -0,0 +1,14 @@
import { configureStore } from "@reduxjs/toolkit";
import anecdoteReducer from "./anecdoteReducer";
import filterReducer from "./filterReducer";
import notificationReducer from "./notificationReducer";
const store = configureStore({
reducer: {
anecdotes: anecdoteReducer,
filter: filterReducer,
notification: notificationReducer,
},
});
export default store;