-
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
c4d03d0
commit fa62a71
Showing
4 changed files
with
140 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,114 @@ | ||
import React, { useState } from 'react'; | ||
import './App.scss'; | ||
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'; | ||
type User = { | ||
id: number; | ||
name: string; | ||
email: string; | ||
username: string; | ||
}; | ||
|
||
type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
user: User | null; | ||
}; | ||
|
||
function getUserById(userId: number): User | null { | ||
return usersFromServer.find(user => user.id === userId) || null; | ||
} | ||
|
||
const initialTodos: Todo[] = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: getUserById(todo.userId), | ||
})); | ||
|
||
export const App: React.FC = () => { | ||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
const [hasTitleError, setHasTitleError] = useState(false); | ||
const [hasSelectError, setHasSelectError] = useState(false); | ||
const [todos, setTodos] = useState<Todo[]>(initialTodos); | ||
|
||
const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value); | ||
setHasTitleError(false); | ||
}; | ||
|
||
const handleSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(+event.target.value); | ||
setHasSelectError(false); | ||
}; | ||
|
||
const handelSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setHasTitleError(!title); | ||
setHasSelectError(!userId); | ||
|
||
if (!title || !userId) { | ||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: todos.length + 1, | ||
title, | ||
completed: false, | ||
user: getUserById(userId), | ||
}; | ||
|
||
setTodos(currentTodos => [...currentTodos, 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={handelSubmit}> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
Title: | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter a title" | ||
value={title} | ||
onChange={handleTitleChange} | ||
/> | ||
{hasTitleError && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
User: | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={handleSelectChange} | ||
> | ||
<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> | ||
{hasSelectError && ( | ||
<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} /> | ||
</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,17 @@ | ||
export const TodoInfo = () => {}; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type TodoInfoProps = { | ||
todo: Todo; | ||
}; | ||
|
||
export const TodoInfo: React.FC<TodoInfoProps> = ({ todo }) => { | ||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={`TodoInfo ${todo.completed ? 'TodoInfo--completed' : ''}`} | ||
> | ||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
{todo.user && <UserInfo user={todo.user} />} | ||
</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,22 @@ | ||
export const TodoList = () => {}; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
user: User | null; | ||
}; | ||
|
||
type TodoListProps = { | ||
todos: Todo[]; | ||
}; | ||
|
||
export const TodoList: React.FC<TodoListProps> = ({ todos }) => { | ||
return ( | ||
<section className="TodoList"> | ||
{todos.map(todo => ( | ||
<TodoInfo key={todo.id} todo={todo} /> | ||
))} | ||
</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,11 @@ | ||
export const UserInfo = () => {}; | ||
type UserInfoProps = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<UserInfoProps> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); | ||
}; |