-
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
first solution #2127
base: master
Are you sure you want to change the base?
first solution #2127
Changes from all commits
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,122 @@ | ||
import React, { useState } from 'react'; | ||
import './App.scss'; | ||
|
||
// 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'; | ||
|
||
import { TodoList } from './components/TodoList'; | ||
|
||
const findUserById = (id: number) => { | ||
return usersFromServer.find(user => user.id === id) || null; | ||
}; | ||
|
||
const todosWithUsers = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: findUserById(todo.userId), | ||
})); | ||
|
||
export const App: React.FC = () => { | ||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
|
||
const [hasTitleError, setHasTitleError] = useState(false); | ||
const [hasUserError, setHasUserError] = useState(false); | ||
|
||
const [newTodos, setNewTodos] = useState<Todo[]>(todosWithUsers); | ||
|
||
const handleTitleInput = ((e: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(e.target.value); | ||
setHasTitleError(false); | ||
}); | ||
|
||
const handleUserSelect = (e: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(+e.target.value); | ||
setHasUserError(false); | ||
}; | ||
|
||
const resetForm = () => { | ||
setTitle(''); | ||
setUserId(0); | ||
}; | ||
|
||
const handleSubmit = ((e: React.ChangeEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (!title && !userId) { | ||
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. better to use 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. Then I will always have both mistakes |
||
setHasTitleError(!hasTitleError); | ||
setHasUserError(!hasUserError); | ||
|
||
return; | ||
} | ||
|
||
if (!title) { | ||
setHasTitleError(!hasTitleError); | ||
|
||
return; | ||
} | ||
|
||
if (!userId) { | ||
setHasUserError(!hasUserError); | ||
|
||
return; | ||
} | ||
Comment on lines
+54
to
+64
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 actually don't need these 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. I need this to prevent changes and raise individual errors if there are any |
||
|
||
const newTodo = { | ||
id: Math.max(...newTodos.map(todo => todo.id)) + 1, | ||
title, | ||
userId, | ||
completed: false, | ||
user: findUserById(userId), | ||
}; | ||
|
||
setNewTodos(prevState => [...prevState, newTodo]); | ||
resetForm(); | ||
}); | ||
|
||
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> | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter a Title" | ||
value={title} | ||
onChange={handleTitleInput} | ||
/> | ||
|
||
{hasTitleError && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={handleUserSelect} | ||
> | ||
<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> | ||
{hasUserError && <span className="error">Please choose a user</span>} | ||
</div> | ||
|
||
<button type="submit" data-cy="submitButton"> | ||
Add | ||
</button> | ||
</form> | ||
Comment on lines
+82
to
117
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. Consider creating a separate file for a ToDoForm and it's hooks, it will keep your App file more readable |
||
|
||
<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={newTodos} /> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
export const TodoInfo = () => {}; | ||
import React from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { UserInfo } from '../UserInfo'; | ||
import { Todo } from '../../types/Todo'; | ||
|
||
interface Props { | ||
todo: Todo, | ||
} | ||
|
||
export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||
const isCompleted = todo.completed && 'TodoInfo--completed'; | ||
|
||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={cn('TodoInfo', isCompleted)} | ||
> | ||
|
||
<h2 className="TodoInfo__title"> | ||
{todo.title} | ||
</h2> | ||
|
||
{todo.user && <UserInfo user={todo.user} />} | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
export const TodoList = () => {}; | ||
import React from 'react'; | ||
|
||
import { TodoInfo } from '../TodoInfo/TodoInfo'; | ||
import { Todo } from '../../types/Todo'; | ||
|
||
type Props = { | ||
todos: Todo[]; | ||
}; | ||
|
||
export const TodoList: React.FC<Props> = ({ todos }) => ( | ||
<section className="TodoList"> | ||
{todos.map(todo => ( | ||
<TodoInfo key={todo.id} todo={todo} /> | ||
))} | ||
</section> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
export const UserInfo = () => {}; | ||
import React from 'react'; | ||
import { User } from '../../types/User'; | ||
|
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import usersFromServer from '../api/users'; | ||
|
||
export const findUserById = (id: number) => { | ||
return usersFromServer.find(user => user.id === id) || null; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { User } from './User'; | ||
|
||
export interface Todo { | ||
id: number, | ||
title: string, | ||
completed: boolean, | ||
userId: number, | ||
user: User | null, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface 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.
It's better to relocate this function to a separate file.