Skip to content

Commit

Permalink
feat: store todo on localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
oyatplum committed Mar 23, 2023
1 parent ef373ff commit ac0f9ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import Todo from './components/Todo';
import Done from './components/Done';
import GlobalStyle from './styles/GloalStyle';

const initialTodoData = localStorage.getItem('list')
? JSON.parse(localStorage.getItem('list'))
: [];

function App() {
const [list, setList] = useState([]);
const [list, setList] = useState(initialTodoData);
const [value, setValue] = useState('');

const getTodo = (todo) => {
Expand All @@ -16,18 +20,21 @@ function App() {
completed: false,
};
setList([...list, newTodo]);
localStorage.setItem('list', JSON.stringify([...list, newTodo]));
};

const toggleTodo = (id) => {
setList(
list.map((data) =>
data.id === id ? { ...data, completed: !data.completed } : data
)
let toggledTodo = list.map((data) =>
data.id === id ? { ...data, completed: !data.completed } : data
);
setList(toggledTodo);
localStorage.setItem('list', JSON.stringify(toggledTodo));
};

const deleteTodo = (id) => {
setList(list.filter((data) => data.id !== id));
let deletedTodo = list.filter((data) => data.id !== id);
setList(deletedTodo);
localStorage.setItem('list', JSON.stringify(deletedTodo));
};

let countTodo = 0;
Expand Down
1 change: 1 addition & 0 deletions src/components/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Todo = ({ Todo, toggleTodo, deleteTodo, title, list, setList }) => {

setList(editedTodo);
setIsEditing(false);
localStorage.setItem('list', JSON.stringify(editedTodo));
};

if (isEditing) {
Expand Down

0 comments on commit ac0f9ef

Please sign in to comment.