-
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
react_add-todo-form solution #2161
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,130 @@ | ||
import './App.scss'; | ||
import React, { useState } from 'react'; | ||
import { TodoList } from './components/TodoList'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
import usersFromServer from './api/users'; | ||
import todosFromServer from './api/todos'; | ||
|
||
import { Todo } from './types/Todo'; | ||
|
||
function getUserById(userId: number) { | ||
return usersFromServer.find(user => user.id === userId); | ||
} | ||
|
||
function getTodoId(todos: Todo[]) { | ||
const maxId = Math.max(...todos.map(todo => todo.id)); | ||
|
||
return maxId + 1; | ||
} | ||
|
||
const initialTodos: Todo[] = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: getUserById(todo.userId), | ||
})); | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>(initialTodos); | ||
|
||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
const [hasNoTitleError, setHasNoTitleError] = useState(true); | ||
const [hasNoUserIdError, setHasNoUserIdError] = useState(true); | ||
|
||
const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value); | ||
setHasNoTitleError(true); | ||
}; | ||
|
||
const handleUserIdChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(+event.target.value); | ||
setHasNoUserIdError(true); | ||
}; | ||
|
||
const addTodo = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
if (!title.trim()) { | ||
setHasNoTitleError(false); | ||
|
||
return; | ||
} | ||
|
||
if (userId === 0) { | ||
setHasNoUserIdError(false); | ||
|
||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: getTodoId(todos), | ||
title, | ||
completed: false, | ||
userId, | ||
user: usersFromServer.find(user => user.id === userId), | ||
}; | ||
|
||
setTodos([...todos, newTodo]); | ||
|
||
setTitle(''); | ||
setUserId(0); | ||
}; | ||
|
||
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={addTodo} | ||
> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<input | ||
type="text" | ||
value={title} | ||
data-cy="titleInput" | ||
pattern="^[a-zA-Z0-9\sА-Яа-яіІєЄїЇґҐ ]*$" | ||
placeholder="Please enter a title" | ||
onChange={handleTitleChange} | ||
/> | ||
{!hasNoTitleError && ( | ||
<span className="error">Please enter a title</span> | ||
)} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<option value="0" disabled>Choose a user</option> | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={handleUserIdChange} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
{usersFromServer.map(user => ( | ||
<option | ||
value={user.id} | ||
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. Destructure |
||
key={user.id} | ||
> | ||
{user.name} | ||
</option> | ||
))} | ||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{!hasNoUserIdError && ( | ||
<span className="error">Please choose a user</span> | ||
)} | ||
</div> | ||
|
||
<button type="submit" data-cy="submitButton"> | ||
<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} /> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1 +1,35 @@ | ||||||||||||
export const TodoInfo = () => {}; | ||||||||||||
import cn from 'classnames'; | ||||||||||||
import { Todo } from '../../types/Todo'; | ||||||||||||
import { UserInfo } from '../UserInfo'; | ||||||||||||
|
||||||||||||
type Props = { | ||||||||||||
todo: Todo; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||||||||||||
const { | ||||||||||||
id, | ||||||||||||
title, | ||||||||||||
completed, | ||||||||||||
user, | ||||||||||||
} = todo; | ||||||||||||
|
||||||||||||
return ( | ||||||||||||
<article | ||||||||||||
data-id={id} | ||||||||||||
key={id} | ||||||||||||
className={cn('TodoInfo', { | ||||||||||||
'TodoInfo--completed': completed, | ||||||||||||
})} | ||||||||||||
> | ||||||||||||
<h2 className="TodoInfo__title"> | ||||||||||||
{title} | ||||||||||||
</h2> | ||||||||||||
|
||||||||||||
{user && ( | ||||||||||||
|
||||||||||||
<UserInfo user={user} /> | ||||||||||||
)} | ||||||||||||
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.
Suggested change
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. |
||||||||||||
</article> | ||||||||||||
); | ||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
export const TodoList = () => {}; | ||
import React from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Props = { | ||
todos: Todo[]; | ||
}; | ||
|
||
export const TodoList: React.FC<Props> = ({ todos }) => { | ||
return ( | ||
<section className="TodoList"> | ||
{todos.map(todo => ( | ||
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. Use semantic tags for list |
||
<TodoInfo todo={todo} key={todo.id} /> | ||
))} | ||
</section> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
export const UserInfo = () => {}; | ||
import React from 'react'; | ||
import { User } from '../../types/User'; | ||
|
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
const { name, email } = user; | ||
|
||
return ( | ||
<a className="UserInfo" href={`mailto:${email}`}> | ||
{name} | ||
</a> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { User } from './User'; | ||
|
||
export type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
user?: User; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type User = { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
}; |
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.
looks like you can use here getUserById function