Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #794

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

cardinal1991
Copy link

Copy link

@choeqq choeqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PLEASE MAKE SURE TO REMOVE EVERY COMMENT BEFORE PUSHING YOUR CODE

also we want to use comments only we unexpected behaviour occurs 😉

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file should be in different directory

Comment on lines 72 to 75
// const addTodo = (title: string) => {
// // Dodaj zadanie do stanu zadań
// setTodos([...todos: any, { title, completed: false }]);
// };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comments

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to type your context corretly, then you won't have to type it manually every time you use it

src/App.tsx Outdated
Comment on lines 24 to 26
// isToggledAll,
setIsToggledAll,
// isGroupDeleting,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup

// isGroupDeleting,
setIsGroupDeleting,
titleInputRef,
} = useTodoContext() as TContext;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check comments in TodoContext.tsx

Comment on lines 67 to 82
// const handleDelete = (todoId: number) => {
// setIsDeleting(true);

// return deleteTodo(todoId)
// .then(() => getTodos(USER_ID))
// .then((res) => {
// setTodos(res);
// setIsDeleting(false);
// })
// .catch(() => {
// handleError('Unable to delete a todo');
// })
// .finally(() => {
// titleInputRef.current?.focus();
// });
// };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup

@@ -0,0 +1,75 @@
import React from 'react';
// import cn from 'classnames';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup

Comment on lines 13 to 17
// todos,
// setTodos,
// handleError,
tempTodos,
// idTemp,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup

// handleError,
tempTodos,
// idTemp,
} = useTodoContext() as TContext;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check comments in TodoContext.tsx

Comment on lines 40 to 67
{/* {(tempTodos !== null) && (
<div data-cy="Todo" className={`${tempTodos?.completed ? 'todo completed' : 'todo'}`} key={tempTodos?.id}>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={tempTodos?.completed}
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
{tempTodos?.title}
</span>
<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
>
×
</button>

<div
data-cy="TodoLoader"
className={cn('modal overlay')}
// { 'is-active': tempTodos.id === 0 }

>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup

@cardinal1991 cardinal1991 requested a review from choeqq September 27, 2023 14:44
Copy link

@Matylda-Lewandowska Matylda-Lewandowska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have nothing to add, code looks nice to me :)

Copy link

@pawel-peksa pawel-peksa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there ;)

src/index.tsx Outdated
@@ -1,10 +1,16 @@
import { createRoot } from 'react-dom/client';
// import React from 'react';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup needed

Comment on lines 72 to 73
// eslint-disable-next-line max-len
const updatedTodoWithNewStatus = { ...updatedTodo, completed: !updatedTodo.completed };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use proper formatting and remove // eslint-disable-next-line max-len


export function TodoProvider({ children }: { children: ReactNode }) {
const [todos, setTodos] = useState<Todo[]>([]);
const [hasError, setHasError] = useState<string | null>(null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's narrow down the type of this state to union of strings which we are actually using there + null

todos: Todo[];
setTodos: React.Dispatch<SetStateAction<Todo[]>>;
hasError: null | string;
handleError: (error: string) => void;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comment related to types in const [hasError, setHasError].
Instead of string let's use a string union

className="todo__title-field"
placeholder="Empty todo will be deleted"
value={newTitle}
// defaultValue={todo.title}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup needed

src/App.tsx Outdated
// }
};

const arrayCompleted = [...todos].filter((todo) => todo.completed === true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const arrayCompleted = [...todos].filter((todo) => todo.completed === true);
const arrayCompleted = [...todos].filter((todo) => todo.completed);

src/App.tsx Outdated
if (!USER_ID) {
return <UserWarning />;
}

const isAllCompleted = todos.every((todo) => todo.completed === true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const isAllCompleted = todos.every((todo) => todo.completed === true);
const isAllCompleted = todos.every((todo) => todo.completed);

src/App.tsx Outdated
// }
};

const arrayCompleted = [...todos].filter((todo) => todo.completed === true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same logic as in sortedTodos.completed. Try to not repeat your code.

src/App.tsx Outdated
Comment on lines 98 to 99
// eslint-disable-next-line quote-props
'active': (isAllCompleted),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for quotes here

src/App.tsx Outdated
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
onClick={deleteCompleted}
disabled={!(todos.some(todo => todo.completed === true))}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
disabled={!(todos.some(todo => todo.completed === true))}
disabled={!(todos.some(todo => todo.completed))}

Copy link

@pawel-peksa pawel-peksa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job 🔥 This task is really complicated and the code looks much better. There is, however, still a possiblity to fix many of the failing tests. Try to focus on setting 'is-active' class basing on specific states. Currently it looks like there is too much going on. First, try to write down on paper all the cases when this class needs to be applied and then reflect it in isActiveConditions

src/App.tsx Outdated
}
};

// const arrayCompleted = [...todos].filter((todo) => todo.completed);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup needed

Comment on lines 39 to 43
const isActiveConditions = (isDeleting === true)
|| (todo.id === 0) || (isToggled && todo.id === toggledId)
|| (isToggledAll) || (isLoading)
|| ((todo.completed) && isGroupDeleting);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that you extracted it outside JSX, however this logic still causes some of the tests to fail. Try to fix it a bit. It should not be dependent on so many cases.

src/App.tsx Outdated
type="button"
className={cn('todoapp__toggle-all',
{
active: (isAllCompleted || isAllNotCompleted),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want this class to appear only when isAllCompleted is true.

Copy link

@mbruhler mbruhler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, you are almost there!

Just a few fixes in your API handling.
Remember that:

  • In PATCH request, you don't have to (and you shouldn't) pass whole object to update, just partial data that needs the update
  • Always read the backend API documentation and align your API handling to it. For example, you don't have to pass ID to the POST request. If there are typing issues, think of using Partial, Pick, Omit typescript utility types which helps to have one type and it's transformations.

In your case I would go with creating types like that:

  • Todo - which contains the response of Todo from API
  • PatchTodo - which is a Partial type of Todo - Partial<Todo>. This makes all the properties optional,
  • AddTodo - which has everything but id. So you can use Omit here like Omit<Todo, 'id'>
  • others accordingly if needed

More on this here: https://www.typescriptlang.org/docs/handbook/utility-types.html

Of course you don't have to use those directives, you can create just another types.

My suggestion may be an overkill for now, but I can see that you do really good and you have much potential so why not trying something extra! If that's over the top, next person propably will accept this PR :)

titleInputRef,
} = useTodoContext() as TContext;

let counter: number;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one resolves to NaN

const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

const newTodo: Todo = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
    "userId": 11550,
    "id": NaN,
    "title": "a",
    "completed": false
}

.finally(() => {
setIsSubmitting(false);
setTempTodos(null);
counter += 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In overall this counter has no effect because is always null. If you want to get next ID then you should check it against the last item in the todos collection.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually as I can see you don't have to pass ID to the POST

Please refer to this documentation link: https://mate-academy.github.io/fe-students-api/todos.html


setIsToggled(true);

editTodo(todoId, updatedTodoWithNewStatus)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to pass the whole Todo to the PATCH.
It's always good to transfer at least amount of data between backend and frontend.

Here we are only interested to set the completed status to the opposite, so the payload should be

{
    completed: !updatedTodo.completed
}

Please refer to the API documentation for further information: https://mate-academy.github.io/fe-students-api/todos.html

…leDelete moved to Context and editTodo with partial argument
Copy link

@mbruhler mbruhler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup the code and I'll accept it. Check also if there are no not needed console.log and // comments

@@ -43,6 +40,8 @@ export const TodoForm: React.FC = () => {
addTodo(newTodo)
.then((res) => {
setTodos([...todos, res]);
// eslint-disable-next-line no-console

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear console.log

src/App.tsx Outdated
@@ -7,23 +7,21 @@ import { TodoList } from './components/TodoList';
import { TodoForm } from './components/TodoForm';
import { TContext, useTodoContext } from './context/TodoContext';
import { SortTypes, Todo } from './types/Todo';
import { deleteTodo } from './api/todos';
// import { deleteTodo } from './api/todos';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear comment

Copy link

@choeqq choeqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants