-
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 bb515c1
Showing
8 changed files
with
203 additions
and
54 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,40 @@ | ||
import './App.scss'; | ||
import { TodoList } from './components/TodoList'; | ||
import todosFromServer from './api/todos'; | ||
import { useState } from 'react'; | ||
import { getUserById } from './services/user'; | ||
import { Todo } from './types/Todo'; | ||
import { PostForm } from './PostForm'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
const initialTodos: Todo[] = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: getUserById(todo.userId), | ||
})); | ||
|
||
function getNewTodoId(todos: Todo[]) { | ||
const maxId = Math.max(...todos.map(todo => todo.id)); | ||
|
||
return maxId + 1; | ||
} | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>(initialTodos); | ||
|
||
const onAddTodo = ({ id, ...data }: Todo) => { | ||
const newTodo = { | ||
id: getNewTodoId(todos), | ||
...data, | ||
}; | ||
|
||
setTodos(currentTodo => [...currentTodo, newTodo]); | ||
}; | ||
|
||
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> | ||
|
||
<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> | ||
<PostForm onSubmit={onAddTodo} /> | ||
<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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import usersFromServer from './api/users'; | ||
import { useState } from 'react'; | ||
import { getUserById } from './services/user'; | ||
import { Todo } from './types/Todo'; | ||
|
||
type Props = { | ||
onSubmit: (todo: Todo) => void; | ||
}; | ||
|
||
export const PostForm: React.FC<Props> = ({ onSubmit }) => { | ||
const [titleErrorMessage, setTitleErrorMessage] = useState(''); | ||
const [title, setTitle] = useState(''); | ||
const hasTitleError = titleErrorMessage !== ''; | ||
const [hasUserIdError, setHasUserIdError] = useState(false); | ||
const [userId, setUserId] = useState(0); | ||
|
||
const handleUserIdChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(+event.target.value); | ||
setHasUserIdError(false); | ||
}; | ||
|
||
const reset = () => { | ||
setUserId(0); | ||
setTitle(''); | ||
setTitleErrorMessage(''); | ||
setHasUserIdError(false); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setHasUserIdError(!userId); | ||
|
||
if (!title) { | ||
setTitleErrorMessage('Please enter a title'); | ||
|
||
return; | ||
} | ||
|
||
if (!title || !userId) { | ||
return; | ||
} | ||
|
||
onSubmit({ | ||
id: 0, | ||
title, | ||
completed: false, | ||
userId, | ||
user: getUserById(userId), | ||
}); | ||
|
||
reset(); | ||
}; | ||
|
||
return ( | ||
<form action="/api/todos" method="POST" onSubmit={handleSubmit}> | ||
<div className="field"> | ||
Title: | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter a title" | ||
value={title} | ||
onChange={event => { | ||
const inputValue = event.target.value; | ||
|
||
setTitle(inputValue); | ||
|
||
if (inputValue) { | ||
setTitleErrorMessage(''); | ||
} else { | ||
setTitleErrorMessage('Please enter a title'); | ||
} | ||
}} | ||
/> | ||
{hasTitleError && <span className="error">{titleErrorMessage}</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
User: | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={handleUserIdChange} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
|
||
{usersFromServer.map(user => ( | ||
<option value={user.id} key={user.id}> | ||
{user.name} | ||
</option> | ||
))} | ||
</select> | ||
{hasUserIdError && <span className="error">Please choose a user</span>} | ||
</div> | ||
|
||
<button type="submit" data-cy="submitButton"> | ||
Add | ||
</button> | ||
</form> | ||
); | ||
}; |
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,19 @@ | ||
export const TodoInfo = () => {}; | ||
import { Todo } from '../../types/Todo'; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
}; | ||
|
||
export const TodoInfo: React.FC<Props> = ({ 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,16 @@ | ||
export const TodoList = () => {}; | ||
import { Todo } from '../../types/Todo'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Props = { | ||
todos: Todo[]; | ||
}; | ||
|
||
export const TodoList: React.FC<Props> = ({ 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,13 @@ | ||
export const UserInfo = () => {}; | ||
import { User } from '../../types/User'; | ||
|
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import usersFromServer from '../api/users'; | ||
import { User } from '../types/User'; | ||
|
||
export function getUserById(userId: number): User | null { | ||
return usersFromServer.find(user => user.id === userId) || null; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { User } from './User'; | ||
|
||
export interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
user: User | null; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface User { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
} |