-
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 #2888
base: master
Are you sure you want to change the base?
solution #2888
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,27 +1,96 @@ | ||
import './App.scss'; | ||
import React, { useState, FormEvent, ChangeEvent } from 'react'; | ||
import usersFromServer from './api/users'; | ||
import todosFromServer from './api/todos'; | ||
import { TodoList } from './components/TodoList/TodoList'; | ||
import { User, Todo } from './types'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
const findUserId = (userId: number) => | ||
usersFromServer.find(user => user.id === userId); | ||
|
||
const todosWithUser = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: findUserId(todo.userId), | ||
})); | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>(todosWithUser); | ||
const [title, setTitle] = useState<string>(''); | ||
const [userId, setUserId] = useState<number>(0); | ||
const [titleError, setTitleError] = useState<boolean>(false); | ||
const [userError, setUserError] = useState<boolean>(false); | ||
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. Boolean variables should start from |
||
|
||
const handleAddTodo = (event: FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setTitleError(false); | ||
setUserError(false); | ||
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. You don't need reset errors at first |
||
|
||
if (!title.trim()) { | ||
setTitleError(true); | ||
} | ||
|
||
if (userId === 0) { | ||
setUserError(true); | ||
} | ||
|
||
if (title.trim() && userId !== 0) { | ||
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. You do |
||
const newTodo: Todo = { | ||
id: Math.max(...todos.map(todo => todo.id)) + 1, | ||
title, | ||
completed: false, | ||
user: findUserId(userId), | ||
userId, | ||
}; | ||
|
||
setTodos([...todos, newTodo]); | ||
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 you set state depending on the previous one use functional |
||
setTitle(''); | ||
setUserId(0); | ||
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. Create a function for resetting |
||
} | ||
}; | ||
|
||
export const App = () => { | ||
return ( | ||
<div className="App"> | ||
<h1>Add todo form</h1> | ||
<h1>Add TODO Form</h1> | ||
|
||
<form action="/api/todos" method="POST"> | ||
<form onSubmit={handleAddTodo}> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<label htmlFor="titleInput">Title</label> | ||
<input | ||
type="text" | ||
id="titleInput" | ||
data-cy="titleInput" | ||
placeholder="Enter TODO title" | ||
value={title} | ||
onChange={(e: ChangeEvent<HTMLInputElement>) => { | ||
setTitle(e.target.value); | ||
setTitleError(false); | ||
}} | ||
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. Create functions instead of passing anonim function to |
||
/> | ||
{titleError && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<label htmlFor="userSelect">User</label> | ||
<select | ||
id="userSelect" | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={(e: ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(Number(e.target.value)); | ||
setUserError(false); | ||
}} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
{usersFromServer.map((user: User) => ( | ||
<option key={user.id} value={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"> | ||
|
@@ -30,31 +99,7 @@ export const App = () => { | |
</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> | ||
<TodoList todos={todos} /> | ||
</section> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
export const TodoInfo = () => {}; | ||
import React from 'react'; | ||
import { UserInfo } from '../UserInfo/UserInfo'; | ||
import { Todo } from '../../types'; | ||
|
||
interface TodoInfoProps { | ||
todo: Todo; | ||
} | ||
|
||
export const TodoInfo: React.FC<TodoInfoProps> = ({ todo }) => { | ||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={`TodoInfo ${todo.completed ? 'TodoInfo--completed' : ''}`} | ||
> | ||
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. To set classes conditionally use classnames or clsx library |
||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
{todo.user ? ( | ||
<UserInfo user={todo.user} /> | ||
) : ( | ||
<div className="UserInfo">No user assigned</div> | ||
)} | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
export const TodoList = () => {}; | ||
import React from 'react'; | ||
import { TodoInfo } from '../TodoInfo/TodoInfo'; | ||
import { Todo } from '../../types'; | ||
|
||
interface TodoListProps { | ||
todos: Todo[]; | ||
} | ||
|
||
export const TodoList: React.FC<TodoListProps> = ({ todos }) => { | ||
return ( | ||
<div> | ||
{todos.map(todo => { | ||
return <TodoInfo key={todo.id} todo={todo} />; | ||
})} | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
export const UserInfo = () => {}; | ||
import React from 'react'; | ||
|
||
interface User { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
interface UserInfoProps { | ||
user: User; | ||
} | ||
|
||
export const UserInfo: React.FC<UserInfoProps> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface User { | ||
id: number; | ||
name: string; | ||
email: string; | ||
} | ||
|
||
export interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
user?: User; | ||
} |
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.
As I see you are looking for user in this function not for id