Skip to content

Commit

Permalink
refactor:refactor useTodos,add useLocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ab3MN committed Nov 15, 2024
1 parent 491c1d3 commit 905c871
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
25 changes: 25 additions & 0 deletions src/hooks/useLocaLStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
interface IUseLocalStorage {
setItem: (value: unknown) => void;
getItem: () => unknown;
removeItem: () => void;
}

const useLocaLStorage = (key: string): IUseLocalStorage => {
const setItem = (value: unknown): void => {
typeof value === 'string'

Check failure on line 9 in src/hooks/useLocaLStorage.ts

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Expected an assignment or function call and instead saw an expression
? localStorage.setItem(key, value)
: localStorage.setItem(key, JSON.stringify(value));
};

const getItem = (): unknown => {
const item = localStorage.getItem(key);

return item ? JSON.parse(item) : null;
};

const removeItem = (): void => localStorage.removeItem(key);

return { setItem, getItem, removeItem };
};

export default useLocaLStorage;
46 changes: 21 additions & 25 deletions src/hooks/useTodos.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { useLayoutEffect, useState } from 'react';
import { Todo } from '../types/Todo';
import {
getCompletedTodos,
getInCompletedTodos,
hasInCompletedTodos,
} from '../utils/todos/getTodos';
import { validUpdatedTodos } from '../utils/todos/validationTodo';
import { updateTodosCompleted } from '../utils/todos/updateTodo';
import { getRandomId } from '../utils/getRandomId';
import useLocaLStorage from './useLocaLStorage';

export const useTodos = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const { getItem, setItem } = useLocaLStorage('todos');

const fetchTodos = () => {
const localTodos = localStorage.getItem('todos');
const localTodos = <Todo[]>getItem();

setTodos(localTodos ? JSON.parse(localTodos) : []);
setTodos(localTodos ? localTodos : []);
};

const addTodo = (title: string): Todo | void => {
const todo = {
title,
completed: false,
id: getRandomId(),
};

localStorage.setItem('todos', JSON.stringify([...todos, todo]));
setItem([...todos, todo]);

setTodos(prevState => [...prevState, todo]);
};
Expand All @@ -34,15 +35,15 @@ export const useTodos = () => {

setTodos(newTodos);

localStorage.setItem('todos', JSON.stringify(newTodos));
setItem(newTodos);
};

const deleteCompletedTodos = async () => {
const inCompletedTodos = getInCompletedTodos(todos);

setTodos(inCompletedTodos);

localStorage.setItem('todos', JSON.stringify(inCompletedTodos));
setItem(inCompletedTodos);
};

const updateTodo = (todo: Todo): Todo | void => {
Expand All @@ -52,33 +53,28 @@ export const useTodos = () => {

setTodos(updatedTodos);

setItem(updatedTodos);

return todo;
};

const updatedAllTodo = (): void => {
const isIncompletedTodo = hasInCompletedTodos(todos);

const newTodos = isIncompletedTodo
? updateTodosCompleted(getInCompletedTodos(todos))
: updateTodosCompleted(todos);

const res = newTodos.map(todo => updateTodo(todo));

const updatedTodos = validUpdatedTodos(res);
let newTodos: Todo[] = [];

if (!!updatedTodos.length) {
setTodos(prevState => {
const updatedState = prevState.map(todo => {
const updatedTodo = updatedTodos.find(
updated => updated.id === todo.id,
);
if (isIncompletedTodo) {
newTodos = [
...updateTodosCompleted(getInCompletedTodos(todos)),
...getCompletedTodos(todos),
];
} else {
newTodos = updateTodosCompleted(todos);
}

return updatedTodo ? updatedTodo : todo;
});
setTodos(newTodos);

return updatedState;
});
}
setItem(newTodos);
};

useLayoutEffect(() => {
Expand Down
4 changes: 0 additions & 4 deletions src/utils/todos/removeTodos.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/utils/todos/validationTodo.ts

This file was deleted.

0 comments on commit 905c871

Please sign in to comment.