-
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
6 changed files
with
157 additions
and
42 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,121 @@ | ||
import React, { useState } from 'react'; | ||
|
||
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 { Todo } from './types/Todo'; | ||
import { User } from './types/User'; | ||
|
||
const getNewTodoId = (todo: Todo[]) => { | ||
const maxId = Math.max(...todo.map(todoMax => todoMax.id)); | ||
|
||
return maxId + 1; | ||
}; | ||
|
||
const getUserById = (userId: number): User => { | ||
return usersFromServer.find(user => user.id === userId) as User; | ||
}; | ||
|
||
const initialTodos = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: getUserById(todo.userId), | ||
})); | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>(initialTodos); | ||
const [title, setTitle] = useState(''); | ||
const [userId, setUserId] = useState(0); | ||
const [hasTitleError, setTitleError] = useState(false); | ||
const [hasUserIdError, setUserIdError] = useState(false); | ||
|
||
const handlUserSelect = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setUserId(Number(event.currentTarget.value)); | ||
setUserIdError(false); | ||
}; | ||
|
||
const handleTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const regExp = /[^a-zA-ZА-ЩЬЮЯҐЄІЇа-щьюяґєі0-9\s]/g; | ||
const value = e.currentTarget.value.replace(regExp, ''); | ||
|
||
setTitle(value); | ||
setTitleError(false); | ||
}; | ||
|
||
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
setTitleError(!title); | ||
setUserIdError(!userId); | ||
|
||
if (title && userId) { | ||
setTitle(''); | ||
setUserId(0); | ||
|
||
const newTodo = { | ||
id: getNewTodoId(todos), | ||
title, | ||
completed: false, | ||
userId, | ||
user: getUserById(userId), | ||
}; | ||
|
||
setTodos(currentTodos => [...currentTodos, newTodo]); | ||
} | ||
}; | ||
|
||
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={handleFormSubmit}> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<label> | ||
{'Title: '} | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
value={title} | ||
onChange={handleTitleChange} | ||
placeholder="Enter a title" | ||
/> | ||
</label> | ||
{hasTitleError && <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> | ||
<label> | ||
{'User: '} | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={handlUserSelect} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
|
||
{usersFromServer.map(user => ( | ||
<option key={user.id} value={user.id}> | ||
{user.name} | ||
</option> | ||
))} | ||
</select> | ||
</label> | ||
|
||
<span className="error">Please choose a user</span> | ||
{hasUserIdError && ( | ||
<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:[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> | ||
|
||
<article data-id="2" className="TodoInfo"> | ||
<h2 className="TodoInfo__title"> | ||
quis ut nam facilis et officia qui | ||
</h2> | ||
|
||
<a className="UserInfo" href="mailto:[email protected]"> | ||
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 { Todo } from '../../types/Todo'; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
}; | ||
|
||
export const TodoInfo: React.FC<Props> = ({ todo }) => ( | ||
<article | ||
data-id={todo.id} | ||
className={`TodoInfo ${todo.completed ? 'TodoInfo--completed' : ''}`} | ||
> | ||
<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,14 @@ | ||
export const TodoList = () => {}; | ||
import { Todo } from '../../types/Todo'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Props = { | ||
todos: Todo[]; | ||
}; | ||
|
||
export const TodoList: React.FC<Props> = ({ todos }) => ( | ||
<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,12 @@ | ||
export const UserInfo = () => {}; | ||
import React from 'react'; | ||
import { User } from '../../types/User'; | ||
|
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => ( | ||
<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,9 @@ | ||
import { User } from './User'; | ||
|
||
export type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
user: User; | ||
}; |
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 type User = { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
}; |