Upload 6.9

This commit is contained in:
Andrew Trieu
2023-06-23 14:49:57 +03:00
parent 485c56f8e4
commit e9de84103d
8 changed files with 127 additions and 17 deletions

View File

@@ -1,12 +1,14 @@
import React from 'react'
import List from './components/List'
import Form from './components/Form'
import Filter from './components/Filter'
const App = () => {
return (
<div>
<h1>Anecdotes</h1>
<Filter />
<List />
<Form />
</div>

View File

@@ -0,0 +1,23 @@
import React from "react";
import { useDispatch } from "react-redux";
import { setFilter } from "../reducers/filterReducer";
const Filter = () => {
const dispatch = useDispatch();
const handleChange = (event) => {
dispatch(setFilter(event.target.value));
};
const style = {
marginBottom: 10,
};
return (
<div style={style}>
Filter <input onChange={handleChange} />
</div>
);
}
export default Filter;

View File

@@ -4,7 +4,9 @@ import { addVote } from '../reducers/anecdoteReducer'
const List = () => {
const dispatch = useDispatch();
const anecdotes = useSelector((state) => state);
const anecdotes = useSelector(({ anecdotes, filter }) => {
return anecdotes.filter((anecdote) => anecdote.content.toLowerCase().includes(filter.toLowerCase()));
});
const handleVote = (id) => {
dispatch(addVote(id));

View File

@@ -1,9 +1,15 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { createStore } from 'redux'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import App from './App'
import reducer from './reducers/anecdoteReducer'
import anecdoteReducer from './reducers/anecdoteReducer'
import filterReducer from './reducers/filterReducer'
const reducer = combineReducers({
anecdotes: anecdoteReducer,
filter: filterReducer,
})
const store = createStore(reducer)

View File

@@ -37,7 +37,7 @@ export const addAnecdote = (newAnecdote) => {
};
};
const reducer = (state = initialState, action) => {
const anecdoteReducer = (state = initialState, action) => {
switch (action.type) {
case "VOTE": {
const { id } = action.data;
@@ -59,4 +59,4 @@ const reducer = (state = initialState, action) => {
}
};
export default reducer
export default anecdoteReducer

View File

@@ -0,0 +1,17 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = "";
const filterSlice = createSlice({
name: "filter",
initialState,
reducers: {
setFilter(state, action) {
state = action.payload;
return state;
},
},
});
export const { setFilter } = filterSlice.actions;
export default filterSlice.reducer;