-
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 task solution #2909
base: master
Are you sure you want to change the base?
add task solution #2909
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,61 +1,116 @@ | ||
import './App.scss'; | ||
|
||
// import usersFromServer from './api/users'; | ||
// import todosFromServer from './api/todos'; | ||
import { TodoList } from './components/TodoList'; | ||
import usersFromServer from './api/users'; | ||
import todosFromServer from './api/todos'; | ||
import React, { useState } from 'react'; | ||
import { todo } from './components/types'; | ||
|
||
export const App = () => { | ||
const editedTodos = todosFromServer.map(todo => ({ | ||
...todo, | ||
user: usersFromServer.find(user => user.id === todo.userId) || { | ||
id: 0, | ||
name: 'Unknown User', | ||
username: 'unknown', | ||
email: 'unknown@example.com', | ||
}, | ||
})); | ||
|
||
const [todos, setTodos] = useState<todo[]>(editedTodos) | ||
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 [title, setTitle] = useState('') | ||
const [userId, setUserId] = useState(0) | ||
const [titleError, setTitleError] = useState(false) | ||
const [userError, setUserError] = useState(false) | ||
|
||
const onSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
let isError = false; | ||
|
||
if (title.trim() === '') { | ||
setTitleError(true); | ||
isError = true; | ||
} | ||
if (userId === 0) { | ||
setUserError(true); | ||
isError = true; | ||
} | ||
|
||
if( isError) { | ||
return; | ||
} | ||
|
||
const newTodo: todo = { | ||
id: Math.max(...todos.map(todo => todo.id)) + 1, | ||
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 calculation of the new |
||
title: title, | ||
completed: false, | ||
userId: userId, | ||
user: usersFromServer.find(user => user.id === userId) || { | ||
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 logic to find the user by |
||
id: 0, | ||
name: 'Unknown User', | ||
username: 'unknown', | ||
email: 'unknown@example.com', | ||
}, | ||
} | ||
|
||
setTodos(previous => [...previous, newTodo]); | ||
setTitle(''); | ||
setUserId(0); | ||
return; | ||
} | ||
return ( | ||
<div className="App"> | ||
<h1>Add todo form</h1> | ||
|
||
<form action="/api/todos" method="POST"> | ||
<form action="/api/todos" method="POST" onSubmit={onSubmit}> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<label htmlFor="titleInput">Todo title</label> | ||
<input type="text" | ||
data-cy="titleInput" | ||
value={title} | ||
onChange={(event)=> { | ||
setTitle(event.target.value) | ||
if (titleError) setTitleError(false) | ||
}} | ||
placeholder='enter title' | ||
/> | ||
{titleError && <span className="error">Please enter a title</span>} | ||
|
||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<option value="0" disabled> | ||
<label htmlFor="userSelect">User</label> | ||
<select data-cy="userSelect" value={userId} onChange={ | ||
(event)=>{ | ||
setUserId(+event.target.value) | ||
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 |
||
if (userError) { | ||
setUserError(false) | ||
} | ||
} | ||
|
||
}> | ||
<option value="0"> | ||
Choose a user | ||
</option> | ||
{usersFromServer.map(user => { | ||
|
||
return(<option value= {user.id} key = {user.id}> | ||
{user.name} | ||
</option>) | ||
|
||
})} | ||
|
||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{userError && <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}/> | ||
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. Ensure that each |
||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
export const TodoInfo = () => {}; | ||
import React from "react"; | ||
import { todo} from "../types"; | ||
import classNames from "classnames"; | ||
import { UserInfo } from "../UserInfo"; | ||
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 |
||
|
||
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> | ||
{todo.user ? ( | ||
<UserInfo user={todo.user} /> | ||
) : ( | ||
<p className="TodoInfo__no-user">User not found</p> | ||
)} | ||
</article> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
export const TodoList = () => {}; | ||
import { todo } from "../types"; | ||
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>) | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
export const UserInfo = () => {}; | ||
import React from "react"; | ||
import { user } from "../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 |
||
|
||
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,19 @@ | ||
export type todo = { | ||
id : number, | ||
title: string, | ||
completed: boolean, | ||
userId: number | ||
user?: { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
}; | ||
}; | ||
|
||
export type 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.
The
usersFromServer
andtodosFromServer
data should be prepared in the App component and passed down as props to child components. This ensures data consistency and maintainability. Consider refactoring to pass the necessary data as props.