Skip to content

Commit

Permalink
implement last suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPopovych committed Oct 27, 2023
1 parent eeba654 commit 499b2c9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const App: React.FC = () => {
setFilterParam(param);
};

const souldRenderList = Boolean(state.length);

return (
<div className="todoapp">
<Header />

{state.length > 0
{souldRenderList
&& (
<>
<section className="main">
Expand Down
2 changes: 1 addition & 1 deletion src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface Props {
}

export const TodoItem: React.FC<Props> = ({ todo, toggleStatus }) => {
// const state = useContext(TodosContext);
const { dispatch } = useContext(TodosContext);
const [titleCurrent, setTitleCurrent] = useState('');
const [updatedTitle, setUpdatedTitle] = useState('');
Expand Down Expand Up @@ -71,6 +70,7 @@ export const TodoItem: React.FC<Props> = ({ todo, toggleStatus }) => {
setTitleCurrent(title);
setUpdatedTitle(title);
setStatus(completed);

if (completed && checkbox.current) {
checkbox.current.checked = true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/TodosFilter/TodosFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const TodoFilter: React.FC<Props> = ({
const { state } = useContext(TodosContext);
const { dispatch } = useContext(TodosContext);

const handleDeleteComplited = () => {
dispatch({ type: Dispatchers.DeleteComplited });
};

const count = state.filter(todo => !todo.completed).length;

const shouldClearButtonRender = shouldRenderButton(count, state.length);
Expand Down Expand Up @@ -77,7 +81,7 @@ export const TodoFilter: React.FC<Props> = ({
className={cn(
'clear-completed',
)}
onClick={() => dispatch({ type: Dispatchers.DeleteComplited })}
onClick={handleDeleteComplited}
>
Clear completed
</button>
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useLocalStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ export function useLocalStorage<T>(
};

return { value, save };
// return [value, save];
}
26 changes: 13 additions & 13 deletions src/store/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,43 @@ const reducer = (state: Todo[], action: Actions) => {
];

case Dispatchers.DeleteComplited:
return [...state].filter(elem => !elem.completed);
return [...state].filter(todo => !todo.completed);

case Dispatchers.DeleteWithId:
return [...state].filter(elem => elem.id !== action.payload);
return [...state].filter(todo => todo.id !== action.payload);

case Dispatchers.ChangeStatus: {
return [...state].map(elem => {
if (elem.id === action.payload) {
return [...state].map(todo => {
if (todo.id === action.payload) {
return {
...elem,
completed: !elem.completed,
...todo,
completed: !todo.completed,
};
}

return elem;
return todo;
});
}

case Dispatchers.UpdateTitle: {
const { title, id } = action.payload;

return [...state].map(elem => {
if (elem.id === id) {
return [...state].map(todo => {
if (todo.id === id) {
return {
...elem,
...todo,
title,
};
}

return elem;
return todo;
});
}

case Dispatchers.ChangeAllStatuses: {
return [...state].map(elem => {
return [...state].map(todo => {
return {
...elem,
...todo,
completed: action.payload,
};
});
Expand Down

0 comments on commit 499b2c9

Please sign in to comment.