-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d91581
commit ccbea11
Showing
7 changed files
with
3,314 additions
and
2,215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,107 @@ | ||
import './App.scss'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
import usersFromServer from './api/users'; | ||
import todosFromServer from './api/todos'; | ||
import { TodoList } from './components/TodoList'; | ||
import { useState } from 'react'; | ||
|
||
interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
} | ||
|
||
interface User { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
} | ||
|
||
export const App = () => { | ||
const [todos, setTodos] = useState<Todo[]>(todosFromServer); | ||
const [users] = useState<User[]>(usersFromServer); | ||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
|
||
const [titleError, setTitleError] = useState(false); | ||
const [useError, setUseError] = useState(false); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setTitleError(!title); | ||
|
||
setUseError(!userId); | ||
|
||
if (!title || !userId) { | ||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: Math.max(...todos.map(todo => todo.id + 1)), | ||
title: title.trim(), | ||
completed: false, | ||
userId, | ||
}; | ||
|
||
setTodos(currentTodo => [...currentTodo, newTodo]); | ||
setTitle(''); | ||
setUserId(0); | ||
}; | ||
|
||
const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value); | ||
setTitleError(false); | ||
}; | ||
|
||
const handleUserIdChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(+event.target.value); | ||
setUseError(false); | ||
}; | ||
|
||
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" | ||
value={title} | ||
onChange={handleTitleChange} | ||
/> | ||
{titleError && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<select | ||
data-cy="userSelect" | ||
onChange={handleUserIdChange} | ||
value={userId} | ||
> | ||
<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> | ||
{useError && <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: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} users={users} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
export const TodoInfo = () => {}; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type TodoInfoProps = { | ||
title: string; | ||
completed: boolean; | ||
user: { | ||
name: string; | ||
email: string; | ||
}; | ||
}; | ||
|
||
export const TodoInfo: React.FC<TodoInfoProps> = ({ | ||
title, | ||
completed, | ||
user, | ||
}) => { | ||
return ( | ||
<article | ||
data-id="1" | ||
className={`TodoInfo ${completed ? 'TodoInfo--completed' : ''}`} | ||
> | ||
<h2 className="TodoInfo__title">{title}</h2> | ||
|
||
<UserInfo name={user.name} email={user.email} /> | ||
</article> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,44 @@ | ||
export const TodoList = () => {}; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
}; | ||
|
||
type User = { | ||
id: number; | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
type TodoListProps = { | ||
todos: Todo[]; | ||
users: User[]; | ||
}; | ||
|
||
export const TodoList: React.FC<TodoListProps> = ({ todos, users }) => { | ||
const getUserById = (userId: number) => | ||
users.find(user => user.id === userId); | ||
|
||
return ( | ||
<section className="TodoList"> | ||
{todos.map(todo => { | ||
const user = getUserById(todo.userId); | ||
|
||
return ( | ||
<TodoInfo | ||
key={todo.id} | ||
title={todo.title} | ||
completed={todo.completed} | ||
user={{ | ||
name: user?.name || 'Empty', | ||
email: user?.email || '', | ||
}} | ||
/> | ||
); | ||
})} | ||
</section> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
export const UserInfo = () => {}; | ||
type TodoUserProps = { | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
export const UserInfo: React.FC<TodoUserProps> = ({ name, email }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${email}`}> | ||
{name} | ||
</a> | ||
); | ||
}; |