-
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
need to add UserInfo class #2927
base: master
Are you sure you want to change the base?
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,28 @@ | ||
import './App.scss'; | ||
import { Todo, TodoList, User } from './components/TodoList'; | ||
import todosFromServer from './api/todos'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
import usersFromServer from './api/users'; | ||
import { Form } from './components/Form'; | ||
import { useState } from 'react'; | ||
|
||
export const App = () => { | ||
return ( | ||
<div className="App"> | ||
<h1>Add todo form</h1> | ||
|
||
<form action="/api/todos" method="POST"> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<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> | ||
|
||
<span className="error">Please choose a user</span> | ||
</div> | ||
const [usersList] = useState<User[]>(usersFromServer); | ||
const [todoList, setuTodoList] = useState<Todo[]>(todosFromServer); | ||
|
||
<button type="submit" data-cy="submitButton"> | ||
Add | ||
</button> | ||
</form> | ||
const addNewTodo = (newTodo: Todo) => { | ||
setuTodoList((currentTodo: Todo[]) => [...currentTodo, newTodo]); | ||
}; | ||
|
||
<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> | ||
return ( | ||
<> | ||
<div className="App"> | ||
<h1>Add todo form</h1> | ||
|
||
<article data-id="2" className="TodoInfo"> | ||
<h2 className="TodoInfo__title"> | ||
quis ut nam facilis et officia qui | ||
</h2> | ||
<Form users={usersList} todos={todoList} addFunc={addNewTodo} /> | ||
|
||
<a className="UserInfo" href="mailto:[email protected]"> | ||
Patricia Lebsack | ||
</a> | ||
</article> | ||
</section> | ||
</div> | ||
<TodoList todos={todoList} users={usersList} /> | ||
</div> | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { User } from '../TodoList'; | ||
import { Todo } from '../TodoList'; | ||
import React, { useState } from 'react'; | ||
|
||
interface Props { | ||
users: User[]; | ||
todos: Todo[]; | ||
addFunc: (newTodo: Todo) => void; | ||
} | ||
|
||
export const Form: React.FC<Props> = ({ users, todos, addFunc }) => { | ||
const [title, setTitle] = useState(''); | ||
|
||
const [userId, setUserId] = useState(0); | ||
|
||
const [titleError, setTitleError] = useState(false); | ||
const [userError, setUserError] = useState(false); | ||
|
||
function onSubmitChacge(event: React.FormEvent) { | ||
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 function name |
||
event.preventDefault(); | ||
|
||
if (!title) { | ||
setTitleError(true); | ||
} else { | ||
setTitleError(false); | ||
} | ||
|
||
if (!userId) { | ||
setUserError(true); | ||
} else { | ||
setUserError(false); | ||
} | ||
|
||
if (title && userId) { | ||
addFunc({ | ||
id: Math.max(...todos.map(todo => todo.id)) + 1, | ||
title: title, | ||
completed: false, | ||
userId: userId, | ||
}); | ||
|
||
setTitle(''); | ||
setUserId(0); | ||
} | ||
|
||
return; | ||
} | ||
|
||
return ( | ||
<form action="/api/todos" method="POST" onSubmit={onSubmitChacge}> | ||
<div className="field"> | ||
<label htmlFor="title"> | ||
Title: {''} | ||
<input | ||
name="title" | ||
value={title} | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter ad title" | ||
onChange={event => setTitle(event.target.value)} | ||
/> | ||
{titleError && !title && ( | ||
<span className="error">Please enter a title</span> | ||
)} | ||
</label> | ||
</div> | ||
|
||
<div className="field"> | ||
<label htmlFor=""> | ||
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 |
||
User: {''} | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={event => { | ||
const selectedId = Number(event.target.value); | ||
const selectedUser = users.find(user => user.id === selectedId); | ||
|
||
if (selectedUser) { | ||
setUserId(selectedUser.id); | ||
} | ||
}} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
{users.map(user => { | ||
return ( | ||
<option key={user.id} value={user.id}> | ||
{user.name} | ||
</option> | ||
); | ||
})} | ||
</select> | ||
</label> | ||
{userError && userId === 0 && ( | ||
<span className="error">Please choose a user</span> | ||
)} | ||
</div> | ||
<button type="submit" data-cy="submitButton"> | ||
Add | ||
</button> | ||
</form> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Form'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
export const TodoInfo = () => {}; | ||
import React from 'react'; | ||
import { Todo } from '../TodoList'; | ||
import { User } from '../TodoList'; | ||
import { UserInfo } from '../UserInfo'; | ||
import classNames from 'classnames'; | ||
|
||
export const TodoInfo: React.FC<{ todo: Todo; user: User | undefined }> = ({ | ||
todo, | ||
user, | ||
}) => { | ||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={classNames('TodoInfo', { | ||
'TodoInfo--completed': todo.completed, | ||
})} | ||
> | ||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
|
||
{user && <UserInfo user={user} />} | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
export const TodoList = () => {}; | ||
import React from 'react'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
export interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
} | ||
|
||
export interface User { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
} | ||
|
||
interface Props { | ||
todos: Todo[]; | ||
users: User[]; | ||
} | ||
|
||
export const TodoList: React.FC<Props> = ({ todos, users }) => { | ||
return ( | ||
<section> | ||
{todos.map(todo => { | ||
const user = users.find(u => u.id === todo.userId); | ||
|
||
return <TodoInfo key={todo.id} todo={todo} user={user} />; | ||
})} | ||
</section> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
export const UserInfo = () => {}; | ||
import { User } from '../TodoList'; | ||
import React from 'react'; | ||
|
||
export const UserInfo: React.FC<{ user: User }> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={'mailto:' + user.email}> | ||
{user.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.
The function name
setuTodoList
seems to be a typo. It should likely besetTodoList
to match the naming convention and improve code readability.