-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
solution #2914
base: master
Are you sure you want to change the base?
solution #2914
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,107 @@ | ||
import './App.scss'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
import usersFromServer from './api/users'; | ||
import todosFromServer from './api/todos'; | ||
import { TodoList } from './components/TodoList'; | ||
import { useState } from 'react'; | ||
|
||
interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
} | ||
|
||
interface User { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
} | ||
|
||
export const App = () => { | ||
const [todos, setTodos] = useState<Todo[]>(todosFromServer); | ||
const [users] = useState<User[]>(usersFromServer); | ||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
|
||
const [titleError, setTitleError] = useState(false); | ||
const [useError, setUseError] = useState(false); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setTitleError(!title); | ||
|
||
setUseError(!userId); | ||
|
||
if (!title || !userId) { | ||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: Math.max(...todos.map(todo => todo.id + 1)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calculation for the new |
||
title: title.trim(), | ||
completed: false, | ||
userId, | ||
}; | ||
|
||
setTodos(currentTodo => [...currentTodo, newTodo]); | ||
setTitle(''); | ||
setUserId(0); | ||
}; | ||
|
||
const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value); | ||
setTitleError(false); | ||
}; | ||
|
||
const handleUserIdChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(+event.target.value); | ||
setUseError(false); | ||
}; | ||
|
||
return ( | ||
<div className="App"> | ||
<h1>Add todo form</h1> | ||
|
||
<form action="/api/todos" method="POST"> | ||
<form action="/api/todos" method="POST" onSubmit={handleSubmit}> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
value={title} | ||
onChange={handleTitleChange} | ||
/> | ||
{titleError && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<select | ||
data-cy="userSelect" | ||
onChange={handleUserIdChange} | ||
value={userId} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
|
||
{usersFromServer.map(user => ( | ||
<option value={user.id} key={user.id}> | ||
{user.name} | ||
</option> | ||
))} | ||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{useError && <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:[email protected]"> | ||
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:[email protected]"> | ||
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:[email protected]"> | ||
Patricia Lebsack | ||
</a> | ||
</article> | ||
</section> | ||
<TodoList todos={todos} users={users} /> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
export const TodoInfo = () => {}; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type TodoInfoProps = { | ||
title: string; | ||
completed: boolean; | ||
user: { | ||
name: string; | ||
email: string; | ||
}; | ||
}; | ||
|
||
export const TodoInfo: React.FC<TodoInfoProps> = ({ | ||
title, | ||
completed, | ||
user, | ||
}) => { | ||
return ( | ||
<article | ||
data-id="1" | ||
className={`TodoInfo ${completed ? 'TodoInfo--completed' : ''}`} | ||
> | ||
<h2 className="TodoInfo__title">{title}</h2> | ||
|
||
<UserInfo name={user.name} email={user.email} /> | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,44 @@ | ||
export const TodoList = () => {}; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
}; | ||
|
||
type User = { | ||
id: number; | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
type TodoListProps = { | ||
todos: Todo[]; | ||
users: User[]; | ||
}; | ||
|
||
export const TodoList: React.FC<TodoListProps> = ({ todos, users }) => { | ||
const getUserById = (userId: number) => | ||
users.find(user => user.id === userId); | ||
|
||
return ( | ||
<section className="TodoList"> | ||
{todos.map(todo => { | ||
const user = getUserById(todo.userId); | ||
|
||
return ( | ||
<TodoInfo | ||
key={todo.id} | ||
title={todo.title} | ||
completed={todo.completed} | ||
user={{ | ||
name: user?.name || 'Empty', | ||
email: user?.email || '', | ||
}} | ||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a user is not found, the default name is set to 'Empty' and the email to an empty string. While this handles the case where a user might not be found, it might be more informative to use a placeholder like 'Unknown User' or 'No Email' to make it clear that the user data is missing. |
||
/> | ||
); | ||
})} | ||
</section> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
export const UserInfo = () => {}; | ||
type TodoUserProps = { | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
export const UserInfo: React.FC<TodoUserProps> = ({ name, email }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${email}`}> | ||
{name} | ||
</a> | ||
); | ||
}; |
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.
There is a typo in the state variable name
useError
. It should likely beuserError
to accurately reflect its purpose of handling user selection errors.