This commit is contained in:
QkoSad
2023-08-08 16:02:54 +03:00
commit 0a7a469d56
315 changed files with 426907 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
{
"anecdotes": [
{
"content": "If it hurts, do it more often",
"id": "47145",
"votes": 21
},
{
"content": "Adding manpower to a late software project makes it later!",
"id": "21149",
"votes": 0
},
{
"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.",
"id": "69581",
"votes": 0
},
{
"content": "Any fool can write code that a computer can understand. Good programmers write code that humans can understand.",
"id": "36975",
"votes": 0
},
{
"content": "Premature optimization is the root of all evil.",
"id": "25170",
"votes": 0
},
{
"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",
"votes": 0
},
{
"content": "asdasdasd",
"votes": 0,
"id": "2L_EqOP"
}
]
}
+30877
View File
File diff suppressed because it is too large Load Diff
+46
View File
@@ -0,0 +1,46 @@
{
"name": "redux-anecdotes",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.4.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.5",
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"server": "json-server -p3001 --watch db.json"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"json-server": "^0.17.3"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

+43
View File
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

+25
View File
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
+3
View File
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
+26
View File
@@ -0,0 +1,26 @@
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { initializeAnecdotes } from "./reducers/anecdoteReducer";
import AnecdoteForm from "./components/AnecdoteForm";
import AnecdoteList from "./components/AnecdoteList";
import Notification from "./components/Notification";
import Filter from "./components/Filter";
const App = () => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(initializeAnecdotes());
}, [dispatch]);
return (
<div>
<Notification />
<Filter />
<AnecdoteList />
<AnecdoteForm />
</div>
);
};
export default App;
@@ -0,0 +1,33 @@
import { useDispatch } from "react-redux";
import { createAnecdote, newAnecdote } from "../reducers/anecdoteReducer";
const AnecdoteForm = () => {
const dispatch = useDispatch();
return (
<div>
<h2>create new</h2>
<form
onSubmit={async (e) => {
e.preventDefault();
dispatch(
createAnecdote({
content: e.target.anecdote.value,
votes: 0,
})
);
e.target.anecdote.value = "";
}}
>
<div>
<input
name="anecdote"
type="text"
placeholder="Writing my new acectode..."
/>
</div>
<button type="sumbit">create</button>
</form>
</div>
);
};
export default AnecdoteForm;
@@ -0,0 +1,39 @@
import { useSelector, useDispatch } from "react-redux";
import { vote } from "../reducers/anecdoteReducer";
import {
setNotification,
} from "../reducers/notificationReducer";
const AnecdoteList = () => {
const dispatch = useDispatch();
const anecdotes = useSelector((state) => state.anecdotes);
const filter = useSelector((state) => state.filter);
return (
<>
<h2>Anecdotes</h2>
{[...anecdotes]
.sort((a, b) => b.votes - a.votes)
.filter((el) => el.content.toLowerCase().includes(filter.toLowerCase()))
.map((anecdote) => (
<div key={anecdote.id}>
<div>{anecdote.content}</div>
<div>
has {anecdote.votes}
<button
onClick={() => {
dispatch(vote(anecdote));
dispatch(
setNotification("you have clicked the vote button", 5)
);
}}
>
vote
</button>
</div>
</div>
))}
</>
);
};
export default AnecdoteList;
+16
View File
@@ -0,0 +1,16 @@
import { useDispatch } from "react-redux";
import { setFilter } from "../reducers/filterReducer";
const Filter = () => {
const dispatch = useDispatch();
const handleChange = (e) => {
e.preventDefault();
dispatch(setFilter(e.target.value));
};
return (
<>
filter <input onChange={handleChange} />
</>
);
};
export default Filter;
@@ -0,0 +1,16 @@
import { useSelector } from "react-redux";
const Notification = () => {
const notification = useSelector((state) => state.notification);
const style = {
border: "solid",
padding: 10,
borderWidth: 1,
display: notification.length ? "" : "none",
};
return <div style={style} >{notification}</div>;
};
export default Notification;
+22
View File
@@ -0,0 +1,22 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { configureStore } from "@reduxjs/toolkit";
import { Provider } from "react-redux";
import App from "./App";
import anecdoteReducer from "./reducers/anecdoteReducer";
import filterReducer from "./reducers/filterReducer";
import notificationReducer from "./reducers/notificationReducer";
const store = configureStore({
reducer: {
anecdotes: anecdoteReducer,
filter: filterReducer,
notification: notificationReducer,
},
});
ReactDOM.createRoot(document.getElementById("root")).render(
<Provider store={store}>
<App />
</Provider>
);
@@ -0,0 +1,42 @@
import { createSlice } from "@reduxjs/toolkit";
import { getAll, create, increaseVote } from "../service/anecdotes";
const anecdoteSlice = createSlice({
name: "anecdote",
initialState: [],
reducers: {
increment(state, action) {
let anecdote = state.find((el) => el.id === action.payload);
anecdote.votes += 1;
state.map((el) => (el.id !== action.payload ? el : anecdote));
},
newAnecdote(state, action) {
state.push(action.payload);
},
setAnecdotes(state, action) {
return action.payload;
},
},
});
export const { increment, newAnecdote, setAnecdotes } = anecdoteSlice.actions;
export const initializeAnecdotes = () => {
return async (dispatch) => {
const anecdotes = await getAll();
dispatch(setAnecdotes(anecdotes));
};
};
export const createAnecdote = (an) => {
return async (dispatch) => {
const anecdote = await create(an);
dispatch(newAnecdote(anecdote));
};
};
export const vote = (anecdote) => {
return async (dispatch) => {
const an= await increaseVote(anecdote);
dispatch(increment(an.id));
};
};
export default anecdoteSlice.reducer;
@@ -0,0 +1,14 @@
import { createSlice } from "@reduxjs/toolkit";
const filterSlice = createSlice({
name: "filter",
initialState: "",
reducers: {
setFilter(state, action) {
return action.payload;
},
},
});
export const { setFilter } = filterSlice.actions;
export default filterSlice.reducer;
@@ -0,0 +1,23 @@
import { createSlice } from "@reduxjs/toolkit";
const notificationSlice = createSlice({
name: "notification",
initialState: [],
reducers: {
make(state, action, time) {
state.push(action.payload);
},
remove(state, action) {
state.pop();
},
},
});
const { make, remove} = notificationSlice.actions;
export const setNotification = (message,time) => {
return (dispatch) => {
dispatch(make(message));
setTimeout(() => dispatch(remove()), time * 1000);
};
};
export default notificationSlice.reducer;
+20
View File
@@ -0,0 +1,20 @@
import axios from "axios";
const baseUrl = "http://localhost:3001/anecdotes";
const getAll = async () => {
const resp = await axios.get(baseUrl);
return resp.data;
};
const create = async (data) => {
const resp = await axios.post(baseUrl, data);
return resp.data;
};
const increaseVote = async (element) => {
const id = element.id;
const newElement = { ...element, votes: element.votes + 1 };
const resp = await axios.put(baseUrl + "/" + id, newElement);
return resp.data
};
export { getAll, create, increaseVote };