Skip to content

Commit

Permalink
store, slice
Browse files Browse the repository at this point in the history
  • Loading branch information
KorolenkoDaria committed Jan 21, 2024
1 parent 6c7e3ea commit 6040a20
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 16 deletions.
72 changes: 66 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-icons": "^4.4.0",
"react-redux": "^9.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.3"
},
Expand Down
20 changes: 11 additions & 9 deletions src/components/App/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { nanoid } from 'nanoid';
import { useState, useEffect } from 'react';
import { useEffect } from 'react';
import { addTodo, getTodos } from "store/todoSlice";


import { useDispatch, useSelector } from 'react-redux';

import {
Container,
Expand All @@ -13,29 +17,27 @@ import {
} from 'components';

export const App = () => {
const [todos, setTodos] = useState(
() => JSON.parse(localStorage.getItem('todos')) ?? []
);
const dispatch = useDispatch();
const todos = useSelector(getTodos);

useEffect(() => {
localStorage.setItem('todos', JSON.stringify(todos));
}, [todos]);

const addTodo = text => {
const addTodos = text => {
const todo = {
id: nanoid(),
text,
};

setTodos(todos => [...todos, todo]);
dispatch(addTodo(todo))
};

const handleSubmit = data => {
addTodo(data);
addTodos(data);
};

const deleteTodo = id => {
setTodos(prevTodos => prevTodos.filter(todo => todo.id !== id));
/* setTodos(prevTodos => prevTodos.filter(todo => todo.id !== id)); */
};

return (
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import { Global, ThemeProvider } from '@emotion/react';
import 'modern-normalize';
import { Provider } from "react-redux";
import store from "store/store";

import { App } from 'components';
import { GlobalStyles, theme } from 'styles';

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Provider store={store}>
<ThemeProvider theme={theme}>
<Global styles={GlobalStyles} />
<App />
</ThemeProvider>
</ThemeProvider>
</Provider>
</React.StrictMode>
);
11 changes: 11 additions & 0 deletions src/store/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { configureStore } from "@reduxjs/toolkit";
import todoSlice from "./todoSlice";

const store = configureStore({
reducer: {
todos: todoSlice,
}
})

export default store;

21 changes: 21 additions & 0 deletions src/store/todoSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createSlice } from "@reduxjs/toolkit";

const todoSlice = createSlice({
name: 'todos',
initialState: {
todos: [],
},
reducers: {
addTodo: (state, { payload }) => {
state.todos = [...state.todos, payload]
}
},
selectors: {
getTodos: (state) => state.todos,
}
});

export default todoSlice.reducer;

export const { addTodo } = todoSlice.actions;
export const { getTodos } = todoSlice.selectors;

0 comments on commit 6040a20

Please sign in to comment.