-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
initial commit #794
Conversation
There was a problem hiding this 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 😉
src/components/TodoContext.tsx
Outdated
There was a problem hiding this comment.
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
src/components/TodoContext.tsx
Outdated
// const addTodo = (title: string) => { | ||
// // Dodaj zadanie do stanu zadań | ||
// setTodos([...todos: any, { title, completed: false }]); | ||
// }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comments
src/components/TodoContext.tsx
Outdated
There was a problem hiding this comment.
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
// isToggledAll, | ||
setIsToggledAll, | ||
// isGroupDeleting, |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
src/components/TodoItem.tsx
Outdated
// 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(); | ||
// }); | ||
// }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup
src/components/TodoList.tsx
Outdated
@@ -0,0 +1,75 @@ | |||
import React from 'react'; | |||
// import cn from 'classnames'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup
src/components/TodoList.tsx
Outdated
// todos, | ||
// setTodos, | ||
// handleError, | ||
tempTodos, | ||
// idTemp, |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
src/components/TodoList.tsx
Outdated
{/* {(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 } | ||
|
||
> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup
There was a problem hiding this 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 :)
There was a problem hiding this 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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup needed
src/context/TodoContext.tsx
Outdated
// eslint-disable-next-line max-len | ||
const updatedTodoWithNewStatus = { ...updatedTodo, completed: !updatedTodo.completed }; |
There was a problem hiding this comment.
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
src/context/TodoContext.tsx
Outdated
|
||
export function TodoProvider({ children }: { children: ReactNode }) { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [hasError, setHasError] = useState<string | null>(null); |
There was a problem hiding this comment.
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
src/context/TodoContext.tsx
Outdated
todos: Todo[]; | ||
setTodos: React.Dispatch<SetStateAction<Todo[]>>; | ||
hasError: null | string; | ||
handleError: (error: string) => void; |
There was a problem hiding this comment.
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
src/components/TodoItem.tsx
Outdated
className="todo__title-field" | ||
placeholder="Empty todo will be deleted" | ||
value={newTitle} | ||
// defaultValue={todo.title} |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
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
// eslint-disable-next-line quote-props | ||
'active': (isAllCompleted), |
There was a problem hiding this comment.
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))} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disabled={!(todos.some(todo => todo.completed === true))} | |
disabled={!(todos.some(todo => todo.completed))} |
There was a problem hiding this 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup needed
src/components/TodoItem.tsx
Outdated
const isActiveConditions = (isDeleting === true) | ||
|| (todo.id === 0) || (isToggled && todo.id === toggledId) | ||
|| (isToggledAll) || (isLoading) | ||
|| ((todo.completed) && isGroupDeleting); | ||
|
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 usingPartial
,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 useOmit
here likeOmit<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 :)
src/components/TodoForm.tsx
Outdated
titleInputRef, | ||
} = useTodoContext() as TContext; | ||
|
||
let counter: number; |
There was a problem hiding this comment.
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
src/components/TodoForm.tsx
Outdated
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
const newTodo: Todo = { |
There was a problem hiding this comment.
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
}
src/components/TodoForm.tsx
Outdated
.finally(() => { | ||
setIsSubmitting(false); | ||
setTempTodos(null); | ||
counter += 1; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
src/context/TodoContext.tsx
Outdated
|
||
setIsToggled(true); | ||
|
||
editTodo(todoId, updatedTodoWithNewStatus) |
There was a problem hiding this comment.
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
There was a problem hiding this 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
src/components/TodoForm.tsx
Outdated
@@ -43,6 +40,8 @@ export const TodoForm: React.FC = () => { | |||
addTodo(newTodo) | |||
.then((res) => { | |||
setTodos([...todos, res]); | |||
// eslint-disable-next-line no-console |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clear comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ✅
DEMO LINK