-
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
add solution #2911
base: master
Are you sure you want to change the base?
add solution #2911
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,112 @@ | ||
import React, { useState } from 'react'; | ||
import './App.scss'; | ||
import { UserInfo } from './components/UserInfo'; | ||
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'; | ||
type TodoProps = { | ||
id: number; | ||
title: string; | ||
userId: number; | ||
completed: boolean; | ||
name: string; | ||
}; | ||
|
||
export const App: React.FC<TodoProps> = () => { | ||
const [errorTitle, setErrorTitle] = useState(false); | ||
const [errorSelect, setErrorSelect] = useState(false); | ||
const [title, setTitle] = useState(''); | ||
const [selectUser, setSelectUser] = useState(0); | ||
const [todos, setTodos] = useState(todosFromServer); | ||
|
||
function cleanForm() { | ||
setTitle(''); | ||
setSelectUser(0); | ||
setErrorTitle(false); | ||
setErrorSelect(false); | ||
} | ||
|
||
function handleTitleChange(event: React.ChangeEvent<HTMLInputElement>) { | ||
setTitle(event.target.value); | ||
setErrorTitle(false); | ||
} | ||
|
||
function handleSelectChange(event: React.ChangeEvent<HTMLSelectElement>) { | ||
setSelectUser(+event.target.value); | ||
setErrorSelect(false); | ||
} | ||
|
||
function handleSubmit(event: React.FormEvent) { | ||
event.preventDefault(); | ||
|
||
setErrorTitle(!title); | ||
setErrorSelect(selectUser === 0); | ||
|
||
if (!title || selectUser === 0) { | ||
return; | ||
} | ||
|
||
const selectedUser = usersFromServer.find(user => user.id === selectUser); | ||
const userName = selectedUser ? selectedUser.name : 'Unknown'; | ||
|
||
const newTodo: TodoProps = { | ||
id: Math.max(...todos.map(todo => todo.id)) + 1, | ||
title, | ||
userId: selectUser, | ||
completed: false, | ||
name: userName, | ||
}; | ||
|
||
setTodos([...todos, newTodo]); | ||
cleanForm(); | ||
} | ||
|
||
export const App = () => { | ||
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> | ||
<label className="label" htmlFor="post-title"> | ||
Title: | ||
</label> | ||
<input | ||
id="post-title" | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter a title" | ||
value={title} | ||
onChange={handleTitleChange} | ||
/> | ||
{errorTitle && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<label className="label" htmlFor="post-user"> | ||
User: | ||
</label> | ||
<select | ||
id="post-user" | ||
data-cy="userSelect" | ||
required | ||
value={selectUser} | ||
onChange={handleSelectChange} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
<UserInfo users={usersFromServer} /> | ||
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 |
||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{errorSelect && <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 users={usersFromServer} todos={todos} /> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
export const TodoInfo = () => {}; | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
type TodoProps = { | ||
todo: { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
}; | ||
user: { | ||
id: number; | ||
name: string; | ||
email: string; | ||
}; | ||
}; | ||
|
||
export const TodoInfo: React.FC<TodoProps> = ({ todo, user }) => { | ||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={classNames('TodoInfo', { | ||
'TodoInfo--completed': todo.completed, | ||
'TodoInfo--active': !todo.completed, | ||
})} | ||
> | ||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,40 @@ | ||
export const TodoList = () => {}; | ||
import React from 'react'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type User = { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
}; | ||
|
||
type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
}; | ||
|
||
type Props = { | ||
users: User[]; | ||
todos: Todo[]; | ||
}; | ||
|
||
export const TodoList: React.FC<Props> = ({ users, todos }) => { | ||
return ( | ||
<section className="TodoList"> | ||
{todos.map(todo => { | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
const user = users.find(user => user.id === todo.userId); | ||
|
||
return ( | ||
<TodoInfo | ||
key={todo.id} | ||
todo={todo} | ||
user={user || { id: 0, name: 'Unknown', username: '', email: '' }} | ||
/> | ||
); | ||
})} | ||
</section> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
export const UserInfo = () => {}; | ||
import React from 'react'; | ||
|
||
type User = { | ||
id: number; | ||
name: string; | ||
}; | ||
|
||
type Props = { | ||
users: User[]; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ users }) => { | ||
return ( | ||
<> | ||
{users.map(user => ( | ||
<option key={user.id} value={user.id}> | ||
{user.name} | ||
</option> | ||
))} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"src" | ||
], | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"sourceMap": false, | ||
"types": ["node", "cypress"] | ||
} | ||
|
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.
The
name
property inTodoProps
might be unnecessary since the name is derived from the user data when creating a new todo. Consider removing it from theTodoProps
type.