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

add task solution #2909

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Implement the ability to add TODOs to the `TodoList` implemented in the **Static
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_add-todo-form/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://shymdima.github.io/react_add-todo-form/) and add it to the PR description.
129 changes: 92 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,116 @@
import './App.scss';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
import { TodoList } from './components/TodoList';
import usersFromServer from './api/users';
import todosFromServer from './api/todos';
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

The usersFromServer and todosFromServer data should be prepared in the App component and passed down as props to child components. This ensures data consistency and maintainability. Consider refactoring to pass the necessary data as props.

import React, { useState } from 'react';
import { todo } from './components/types';

export const App = () => {
const editedTodos = todosFromServer.map(todo => ({

Check failure on line 9 in src/App.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'todo' is already declared in the upper scope on line 6 column 10
...todo,
user: usersFromServer.find(user => user.id === todo.userId) || {
id: 0,
name: 'Unknown User',
username: 'unknown',
email: 'unknown@example.com',
},
}));

const [todos, setTodos] = useState<todo[]>(editedTodos)

Choose a reason for hiding this comment

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

The useState hook is used correctly to initialize the todos state with editedTodos. However, ensure that the todo type imported from ./components/types includes the user property as optional, which is correctly done in the types.ts file.

const [title, setTitle] = useState('')
const [userId, setUserId] = useState(0)
const [titleError, setTitleError] = useState(false)
const [userError, setUserError] = useState(false)

const onSubmit = (event: React.FormEvent) => {
event.preventDefault();

let isError = false;

if (title.trim() === '') {
setTitleError(true);
isError = true;
}
if (userId === 0) {
setUserError(true);
isError = true;
}

if( isError) {
return;
}

const newTodo: todo = {
id: Math.max(...todos.map(todo => todo.id)) + 1,

Choose a reason for hiding this comment

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

The calculation of the new id for newTodo is correct, but ensure that todos is not empty to avoid errors with Math.max. Consider adding a check or default value if todos might be empty.

title: title,

Check failure on line 45 in src/App.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'todo' is already declared in the upper scope on line 6 column 10
completed: false,
userId: userId,
user: usersFromServer.find(user => user.id === userId) || {

Choose a reason for hiding this comment

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

The logic to find the user by userId is correct, but ensure that usersFromServer is always available and populated to avoid potential runtime errors.

id: 0,
name: 'Unknown User',
username: 'unknown',
email: 'unknown@example.com',
},
}

setTodos(previous => [...previous, newTodo]);
setTitle('');
setUserId(0);
return;
}
return (
<div className="App">
<h1>Add todo form</h1>

<form action="/api/todos" method="POST">
<form action="/api/todos" method="POST" onSubmit={onSubmit}>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<label htmlFor="titleInput">Todo title</label>
<input type="text"
data-cy="titleInput"
value={title}
onChange={(event)=> {
setTitle(event.target.value)
if (titleError) setTitleError(false)
}}
placeholder='enter title'
/>
{titleError && <span className="error">Please enter a title</span>}

</div>

<div className="field">
<select data-cy="userSelect">
<option value="0" disabled>
<label htmlFor="userSelect">User</label>
<select data-cy="userSelect" value={userId} onChange={
(event)=>{
setUserId(+event.target.value)

Choose a reason for hiding this comment

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

The setUserId function uses +event.target.value to convert the value to a number, which is correct. Ensure that the userId is always a valid number to prevent unexpected behavior.

if (userError) {
setUserError(false)
}
}

}>
<option value="0">
Choose a user
</option>
{usersFromServer.map(user => {

return(<option value= {user.id} key = {user.id}>
{user.name}
</option>)

})}

</select>

<span className="error">Please choose a user</span>
{userError && <span className="error">Please choose a user</span>}

</div>

<button type="submit" data-cy="submitButton">
Add
</button>
</form>

<section className="TodoList">
<article data-id="1" className="TodoInfo TodoInfo--completed">
<h2 className="TodoInfo__title">delectus aut autem</h2>

<a className="UserInfo" href="mailto:Sincere@april.biz">
Leanne Graham
</a>
</article>

<article data-id="15" className="TodoInfo TodoInfo--completed">
<h2 className="TodoInfo__title">delectus aut autem</h2>

<a className="UserInfo" href="mailto:Sincere@april.biz">
Leanne Graham
</a>
</article>

<article data-id="2" className="TodoInfo">
<h2 className="TodoInfo__title">
quis ut nam facilis et officia qui
</h2>

<a className="UserInfo" href="mailto:Julianne.OConner@kory.org">
Patricia Lebsack
</a>
</article>
</section>
<TodoList todos={todos}/>

Choose a reason for hiding this comment

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

Ensure that each TodoInfo component within the TodoList has a unique key prop. This is crucial for React's reconciliation process and helps prevent potential rendering issues.

</div>
);
};
26 changes: 25 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
export const TodoInfo = () => {};
import React from "react";
import { todo} from "../types";
import classNames from "classnames";
import { UserInfo } from "../UserInfo";

Choose a reason for hiding this comment

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

The UserInfo component should not be imported directly here. Instead, the user data should be passed down as a prop from the parent component to maintain data consistency and follow best practices. Consider refactoring to receive the user data as a prop.


type Props = {
todo: todo;
};

export const TodoInfo: React.FC<Props> = ({ todo }) => {

Check failure on line 10 in src/components/TodoInfo/TodoInfo.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'todo' is already declared in the upper scope on line 2 column 10

return (
<article
data-id={todo.id}
className={classNames("TodoInfo", { "TodoInfo--completed": todo.completed })}
>
<h2 className="TodoInfo__title">{todo.title}</h2>
{todo.user ? (
<UserInfo user={todo.user} />
) : (
<p className="TodoInfo__no-user">User not found</p>
)}
</article>
);
};
15 changes: 14 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export const TodoList = () => {};
import { todo } from "../types";
import { TodoInfo } from "../TodoInfo";
type Props = {
todos: todo[]
}

export const TodoList: React.FC<Props> = ({todos}) => {
return (<section className="TodoList">
{todos.map(todo => [
<TodoInfo todo={todo} key = {todo.id}/>

Check failure on line 10 in src/components/TodoList/TodoList.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'todo' is already declared in the upper scope on line 1 column 10
])}

</section>)
};
15 changes: 14 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export const UserInfo = () => {};
import React from "react";
import { user } from "../types";

Choose a reason for hiding this comment

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

The user type should be passed down as a prop from the parent component rather than being imported directly. This ensures data consistency and follows best practices. Consider refactoring to receive the user data as a prop.


type Props = {
user: user
}
export const UserInfo: React.FC <Props> = ({user}) => {

Check failure on line 7 in src/components/UserInfo/UserInfo.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'user' is already declared in the upper scope on line 2 column 10

return (
<a className="UserInfo" href={`mailto:${user.email}`}>
{user.name}
</a>
)
};
19 changes: 19 additions & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type todo = {

Check failure on line 1 in src/components/types.ts

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Type Alias name `todo` must match one of the following formats: PascalCase
id : number,
title: string,
completed: boolean,
userId: number
user?: {
id: number;
name: string;
username: string;
email: string;
};
};

export type user = {

Check failure on line 14 in src/components/types.ts

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Type Alias name `user` must match one of the following formats: PascalCase
id: number,
name: string,
username: string,
email: string
};
Loading