-
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
Showing
7 changed files
with
177 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
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,131 @@ | ||
import './App.scss'; | ||
import { useState } from 'react'; | ||
|
||
// 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 { Todo } from './Types/Todo'; | ||
|
||
function getUserById(userId: number) { | ||
return usersFromServer.find(user => user.id === userId) || null; | ||
} | ||
|
||
const preparedTodos: Todo[] = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: getUserById(todo.userId), | ||
})); | ||
|
||
function getMaxId(recievedTodos: Todo[]) { | ||
const maxId = Math.max( | ||
...recievedTodos.map(todo => todo.id), | ||
); | ||
|
||
return maxId + 1; | ||
} | ||
|
||
export const App = () => { | ||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
const [todos, setTodos] = useState(preparedTodos); | ||
|
||
const [hasTitleError, setHasTitleError] = useState(false); | ||
const [selectedIdUserError, setSelectedIdUserError] = useState(false); | ||
|
||
function handleSubmit(event: React.ChangeEvent<HTMLFormElement>) { | ||
event.preventDefault(); | ||
|
||
let hasErrors = false; | ||
|
||
if (!title) { | ||
setHasTitleError(true); | ||
hasErrors = true; | ||
} else { | ||
setHasTitleError(false); | ||
} | ||
|
||
if (!userId) { | ||
setSelectedIdUserError(true); | ||
hasErrors = true; | ||
} else { | ||
setSelectedIdUserError(false); | ||
} | ||
|
||
if (hasErrors) { | ||
return; | ||
} | ||
|
||
const newPost: Todo = { | ||
id: getMaxId(todos), | ||
title, | ||
completed: false, | ||
userId, | ||
user: getUserById(userId), | ||
}; | ||
|
||
setTodos(currentTodos => [...currentTodos, newPost]); | ||
setTitle(''); | ||
setUserId(0); | ||
} | ||
|
||
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> | ||
<label htmlFor="titleInput">Title: </label> | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter a title" | ||
value={title} | ||
onChange={(event) => { | ||
setTitle(event.target.value); | ||
setHasTitleError(false); | ||
}} | ||
/> | ||
|
||
{hasTitleError && ( | ||
<span className="error">Please enter a title</span> | ||
)} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<label htmlFor="userSelect">User: </label> | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={(event) => { | ||
setUserId(+event.target.value); | ||
setSelectedIdUserError(false); | ||
}} | ||
> | ||
<option value="0" disabled>Choose a user</option> | ||
{usersFromServer.map(user => ( | ||
<option | ||
key={user.id} | ||
value={user.id} | ||
> | ||
{user.name} | ||
</option> | ||
))} | ||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{selectedIdUserError && ( | ||
<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 |
---|---|---|
@@ -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, | ||
} |
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,26 @@ | ||
export const TodoInfo = () => {}; | ||
import classNames from 'classnames'; | ||
import { Todo } from '../../Types/Todo'; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type Props = { | ||
todo: Todo, | ||
}; | ||
|
||
export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||
return ( | ||
<> | ||
<article | ||
className={classNames('TodoInfo', { | ||
'TodoInfo--completed': todo.completed, | ||
})} | ||
data-id={todo.id} | ||
> | ||
<h2 className="TodoInfo__title"> | ||
{todo.title} | ||
</h2> | ||
|
||
<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 todo={todo} key={todo.id} /> | ||
))} | ||
</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 | null, | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user?.email}`}> | ||
{user?.name} | ||
</a> | ||
); | ||
}; |