-
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
add solution #2121
base: master
Are you sure you want to change the base?
add solution #2121
Changes from all 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,25 +1,117 @@ | ||||||
import './App.scss'; | ||||||
|
||||||
// import usersFromServer from './api/users'; | ||||||
// import todosFromServer from './api/todos'; | ||||||
import { useState } from 'react'; | ||||||
import usersFromServer from './api/users'; | ||||||
import todosFromServer from './api/todos'; | ||||||
import { TodoList } from './components/TodoList'; | ||||||
import { TodoWithUser } from './types/TodoType'; | ||||||
|
||||||
function findUsers(userId: number) { | ||||||
return usersFromServer.find((user) => user.id === userId) || null; | ||||||
} | ||||||
|
||||||
const dataList = todosFromServer.map((todo) => { | ||||||
return { | ||||||
...todo, | ||||||
user: findUsers(todo.userId), | ||||||
}; | ||||||
}); | ||||||
|
||||||
export const App = () => { | ||||||
const [currentData, setCurrentData] = useState<TodoWithUser[]>(dataList); | ||||||
const [title, setTitle] = useState(''); | ||||||
const [currennUserId, setCurrentUserId] = useState(0); | ||||||
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.
Suggested change
|
||||||
|
||||||
const [titleValid, setTitleValid] = useState(false); | ||||||
const [selectValid, setSelectValid] = useState(false); | ||||||
|
||||||
const getTodoIds = currentData.map((todo) => todo.id); | ||||||
const getNewId = Math.max(...getTodoIds) + 1; | ||||||
|
||||||
function clear() { | ||||||
setTitle(''); | ||||||
setCurrentUserId(0); | ||||||
setTitleValid(false); | ||||||
setSelectValid(false); | ||||||
} | ||||||
|
||||||
const handlerSubmit = (event: React.ChangeEvent<HTMLFormElement>) => { | ||||||
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.
Suggested change
|
||||||
event.preventDefault(); | ||||||
|
||||||
const newTodo: TodoWithUser = { | ||||||
id: getNewId, | ||||||
completed: false, | ||||||
title, | ||||||
userId: currennUserId, | ||||||
user: findUsers(currennUserId), | ||||||
}; | ||||||
|
||||||
if (!title) { | ||||||
setTitleValid(true); | ||||||
} | ||||||
|
||||||
if (!currennUserId) { | ||||||
setSelectValid(true); | ||||||
} | ||||||
|
||||||
if (title && currennUserId) { | ||||||
setCurrentData((prevData) => [...prevData, newTodo]); | ||||||
clear(); | ||||||
} | ||||||
}; | ||||||
|
||||||
const handlerSelectUser = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||||||
if (selectValid) { | ||||||
setSelectValid(false); | ||||||
} | ||||||
|
||||||
setCurrentUserId(+event.target.value); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<div className="App"> | ||||||
<h1>Add todo form</h1> | ||||||
|
||||||
<form action="/api/todos" method="POST"> | ||||||
<form onSubmit={handlerSubmit} action="/api/todos" method="POST"> | ||||||
<div className="field"> | ||||||
<input type="text" data-cy="titleInput" /> | ||||||
<span className="error">Please enter a title</span> | ||||||
<label> | ||||||
{'Title: '} | ||||||
<input | ||||||
placeholder="Enter a title" | ||||||
value={title} | ||||||
onChange={(event: React.ChangeEvent<HTMLInputElement>) => { | ||||||
setTitle(event.target.value); | ||||||
|
||||||
if (titleValid) { | ||||||
setTitleValid(false); | ||||||
} | ||||||
}} | ||||||
type="text" | ||||||
data-cy="titleInput" | ||||||
/> | ||||||
</label> | ||||||
{titleValid && <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> | ||||||
<label> | ||||||
{'User: '} | ||||||
<select | ||||||
value={currennUserId} | ||||||
onChange={handlerSelectUser} | ||||||
data-cy="userSelect" | ||||||
> | ||||||
<option value="0" disabled> | ||||||
Choose a user | ||||||
</option> | ||||||
{usersFromServer.map((user) => ( | ||||||
<option key={user.id} value={user.id}> | ||||||
{user.name} | ||||||
</option> | ||||||
))} | ||||||
</select> | ||||||
</label> | ||||||
{selectValid && <span className="error">Please choose a user</span>} | ||||||
</div> | ||||||
|
||||||
<button type="submit" data-cy="submitButton"> | ||||||
|
@@ -28,33 +120,7 @@ export const App = () => { | |||||
</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> | ||||||
<TodoList todos={currentData} /> | ||||||
</section> | ||||||
</div> | ||||||
); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
export const TodoInfo = () => {}; | ||
import { TodoWithUser } from '../../types/TodoType'; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
type Props = { | ||
todo: TodoWithUser; | ||
}; | ||
export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||
return ( | ||
<article | ||
key={todo.id} | ||
data-id={todo.id.toString()} | ||
className={`TodoInfo ${todo.completed && 'TodoInfo--completed'}`} | ||
> | ||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
|
||
{todo.user && <UserInfo user={todo.user} />} | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
export const TodoList = () => {}; | ||
import { TodoWithUser } from '../../types/TodoType'; | ||
import { TodoInfo } from '../TodoInfo'; | ||
|
||
type Props = { | ||
todos: TodoWithUser[]; | ||
}; | ||
|
||
export const TodoList: React.FC<Props> = ({ todos }) => { | ||
return ( | ||
<> | ||
{todos.map((todo: TodoWithUser) => ( | ||
<TodoInfo key={todo.id} todo={todo} /> | ||
))} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
export const UserInfo = () => {}; | ||
import { User } from '../../types/UsersType'; | ||
|
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user?.email}`}> | ||
{user?.name} | ||
</a> | ||
); | ||
}; | ||
Comment on lines
+7
to
+13
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. [REACT KNOWLEDGE] - Don't render the component if the property that you pass to the component has |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { User } from './UsersType'; | ||
|
||
export interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
} | ||
|
||
export interface TodoWithUser extends Todo { | ||
user?: User | null; | ||
} |
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; | ||
} |
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.
Would be great to use descriptive meaningful names