-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #2921
base: master
Are you sure you want to change the base?
Develop #2921
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,122 @@ | ||
import { 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 { getHigherId } from './serveses/todos'; | ||
import { getUserById } from './serveses/user'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import path './serveses/user' seems to be incorrect. Please verify the correct path for the |
||
|
||
import { User } from './serveses/types'; | ||
|
||
const completedTodos = todosFromServer.map(todo => { | ||
const user = getUserById(usersFromServer, todo.userId); | ||
|
||
return { | ||
...todo, | ||
user, | ||
}; | ||
}); | ||
|
||
export const App = () => { | ||
const [title, setTitle] = useState(''); | ||
const [hasTitleError, setHasTitleError] = useState(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
const [selectedUserId, setSelectedUserId] = useState(0); | ||
const [hasUserError, setHasUserError] = useState(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
const [todos, setTodos] = useState(completedTodos); | ||
|
||
const handleTitle = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value); | ||
setHasTitleError(''); | ||
}; | ||
|
||
const handlUser = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the function name |
||
setSelectedUserId(+event.target.value); | ||
setHasUserError(''); | ||
}; | ||
|
||
const addTodo = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
if (!title) { | ||
setHasTitleError('Please enter a title'); | ||
} | ||
|
||
if (!selectedUserId) { | ||
setHasUserError('Please choose a user'); | ||
} | ||
|
||
if (!title || !selectedUserId) { | ||
return; | ||
} | ||
|
||
setTodos(currentTodos => { | ||
const user = getUserById(usersFromServer, selectedUserId); | ||
|
||
const todo = { | ||
id: getHigherId(todos), | ||
title, | ||
completed: false, | ||
userId: +selectedUserId, | ||
user, | ||
}; | ||
|
||
return [...currentTodos, todo]; | ||
}); | ||
|
||
setTitle(''); | ||
setSelectedUserId(0); | ||
}; | ||
|
||
return ( | ||
<div className="App"> | ||
<h1>Add todo form</h1> | ||
|
||
<form action="/api/todos" method="POST"> | ||
<form action="/api/todos" method="POST" onSubmit={addTodo}> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<label htmlFor="title"> | ||
<span className="field__title">Title:</span> | ||
<input | ||
type="text" | ||
id="title" | ||
data-cy="titleInput" | ||
value={title} | ||
placeholder="Title" | ||
onChange={handleTitle} | ||
/> | ||
</label> | ||
|
||
{hasTitleError && <span className="error">{hasTitleError}</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<select | ||
data-cy="userSelect" | ||
value={selectedUserId} | ||
onChange={handlUser} | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
{usersFromServer.map((user: User) => { | ||
return ( | ||
<option key={user.id} value={user.id}> | ||
{user.name} | ||
</option> | ||
); | ||
})} | ||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{hasUserError && <span className="error">{hasUserError}</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> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
export const TodoInfo = () => {}; | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import { UserInfo } from '../UserInfo'; | ||
import { Todo } from '../../serveses/types'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import path '../../serveses/types' seems to be incorrect. Please verify the correct path for the |
||
type Props = { | ||
todo: Todo; | ||
}; | ||
|
||
export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={classNames('TodoInfo', { | ||
'TodoInfo--completed': todo.completed, | ||
})} | ||
> | ||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
<UserInfo user={todo.user} /> | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
export const TodoList = () => {}; | ||
import React from 'react'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
import { Todo } from '../../serveses/types'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import path '../../serveses/types' seems to be incorrect. Please verify the correct path for the |
||
type Props = { | ||
todos: Todo[]; | ||
}; | ||
export const TodoList: React.FC<Props> = ({ todos }) => { | ||
return ( | ||
<section className="TodoList"> | ||
{todos.map(todo => { | ||
return <TodoInfo todo={todo} key={todo.id} />; | ||
})} | ||
</section> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
export const UserInfo = () => {}; | ||
import React from 'react'; | ||
import { User } from '../../serveses/types'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import path '../../serveses/types' seems to be incorrect. Please verify the correct path for the |
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
}; | ||
|
||
export const getHigherId = (todos: Todo[]) => { | ||
return Math.max(...todos.map(todo => todo.id)) + 1; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type User = { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
}; | ||
|
||
export type Todo = { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
user: User | null; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type User = { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
export const getUserById = (users: User[], id: number) => { | ||
return users.find(user => user.id === id) || null; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import path './serveses/todos' seems to be incorrect. Please verify the correct path for the
getHigherId
function.