mirror of
https://github.com/LucasVbr/todo-list.git
synced 2026-07-09 07:07:49 +00:00
@@ -2,7 +2,8 @@ import TodoState from '../../models/TodoState.ts';
|
|||||||
import {createSlice} from '@reduxjs/toolkit';
|
import {createSlice} from '@reduxjs/toolkit';
|
||||||
import todoState from '../../models/TodoState.ts';
|
import todoState from '../../models/TodoState.ts';
|
||||||
|
|
||||||
const initialState: TodoState[] = [];
|
const savedTodos: string | null = localStorage.getItem('todos');
|
||||||
|
const initialState: TodoState[] = savedTodos ? JSON.parse(savedTodos) : [];
|
||||||
|
|
||||||
export const todoSlice = createSlice({
|
export const todoSlice = createSlice({
|
||||||
name: 'todo',
|
name: 'todo',
|
||||||
@@ -16,6 +17,7 @@ export const todoSlice = createSlice({
|
|||||||
};
|
};
|
||||||
|
|
||||||
state.push(newTodo);
|
state.push(newTodo);
|
||||||
|
localStorage.setItem('todos', JSON.stringify(state));
|
||||||
},
|
},
|
||||||
checkTodo: (state, action) => {
|
checkTodo: (state, action) => {
|
||||||
const id: number = action.payload;
|
const id: number = action.payload;
|
||||||
@@ -26,9 +28,12 @@ export const todoSlice = createSlice({
|
|||||||
if (todo === undefined) return;
|
if (todo === undefined) return;
|
||||||
|
|
||||||
todo.checked = !todo.checked;
|
todo.checked = !todo.checked;
|
||||||
|
localStorage.setItem('todos', JSON.stringify(state));
|
||||||
},
|
},
|
||||||
deleteSelectedTodos: (state) => {
|
deleteSelectedTodos: (state) => {
|
||||||
return state.filter((todo: TodoState) => !todo.checked);
|
const newState = state.filter((todo: TodoState) => !todo.checked);
|
||||||
|
localStorage.setItem('todos', JSON.stringify(state));
|
||||||
|
return newState;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user