-
Notifications
You must be signed in to change notification settings - Fork 3.2k
add solution #3555
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
base: master
Are you sure you want to change the base?
add solution #3555
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,131 @@ | ||
| import './App.scss'; | ||
|
|
||
| // import usersFromServer from './api/users'; | ||
| // import todosFromServer from './api/todos'; | ||
| import usersFromServer from './api/users'; | ||
| import todosFromServer from './api/todos'; | ||
| import { useState } from 'react'; | ||
| import { TodoList } from './components/TodoList'; | ||
|
|
||
| export interface ToDo { | ||
| id: number; | ||
| title: string; | ||
| completed: boolean; | ||
| userId: number; | ||
| user: User; | ||
| } | ||
|
0-nira-0 marked this conversation as resolved.
|
||
|
|
||
| export interface User { | ||
| id: number; | ||
| name: string; | ||
| username: string; | ||
| email: string; | ||
| } | ||
|
|
||
| export const App = () => { | ||
| const usersToDos = todosFromServer | ||
| .map(todo => { | ||
| return { | ||
| ...todo, | ||
| user: usersFromServer.find(user => user.id === todo.userId), | ||
| }; | ||
| }) | ||
| .filter((todo): todo is ToDo => todo.user !== undefined); | ||
|
|
||
| const [title, setTitle] = useState(''); | ||
| const [userId, setUserId] = useState(0); | ||
| const [visibleToDos, setVisibleToDos] = useState([...usersToDos]); | ||
|
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. This violates checklist item #3: "3. [CODE KNOWLEDGE] - If you are using a non-mutating array method, you don't need to create a copy of the array". You create an unnecessary copy when initializing state: |
||
|
|
||
| const [titleError, setTitleError] = useState(false); | ||
| const [userIdError, setUserIdError] = useState(false); | ||
|
|
||
| const userOfToDo = usersFromServer.find(user => user.id === userId); | ||
|
|
||
| function handleSubmision() { | ||
| if (title === '') { | ||
| setTitleError(() => true); | ||
| } | ||
|
|
||
| if (userId === 0) { | ||
| setUserIdError(() => true); | ||
| } | ||
|
|
||
| if (title !== '' && userId !== 0 && userOfToDo) { | ||
| setVisibleToDos(previousToDos => { | ||
| return [ | ||
| ...previousToDos, | ||
| { | ||
| id: previousToDos.reduce((max, t) => Math.max(max, t.id), 0) + 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. This violates checklist item #1: "1. [CODE STYLE] - Don't use one-letter variable naming." The reducer uses a one-letter variable |
||
| title: title, | ||
| completed: false, | ||
| userId: userId, | ||
| user: userOfToDo, | ||
| }, | ||
|
0-nira-0 marked this conversation as resolved.
|
||
| ]; | ||
| }); | ||
|
|
||
| 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={event => { | ||
| handleSubmision(); | ||
| event.preventDefault(); | ||
| }} | ||
| > | ||
| <div className="field"> | ||
| <input type="text" data-cy="titleInput" /> | ||
| <span className="error">Please enter a title</span> | ||
| <label htmlFor="title-input">Title: </label> | ||
| <input | ||
| id="title-input" | ||
| type="text" | ||
| data-cy="titleInput" | ||
| placeholder={'Enter a title'} | ||
| value={title} | ||
| onChange={event => { | ||
| setTitle(event.target.value); | ||
| setTitleError(false); | ||
| }} | ||
| /> | ||
| {titleError && <span className="error">Please enter a title</span>} | ||
| </div> | ||
|
|
||
| <div className="field"> | ||
| <select data-cy="userSelect"> | ||
| <label htmlFor="user-select">User: </label> | ||
| <select | ||
| id="user-select" | ||
| data-cy="userSelect" | ||
| value={userId} | ||
| onChange={event => { | ||
| setUserId(+event.target.value); | ||
| setUserIdError(false); | ||
| }} | ||
| > | ||
| <option value="0" disabled> | ||
|
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. Minor correctness issue: the default option uses a string value |
||
| Choose a user | ||
| </option> | ||
|
|
||
| {usersFromServer.map(user => ( | ||
| <option value={user.id} key={user.id}> | ||
| {user.name} | ||
| </option> | ||
| ))} | ||
| </select> | ||
|
|
||
| <span className="error">Please choose a user</span> | ||
| {userIdError && <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={visibleToDos} /> | ||
|
0-nira-0 marked this conversation as resolved.
|
||
| </div> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,23 @@ | ||
| export const TodoInfo = () => {}; | ||
| import { UserInfo } from '../UserInfo'; | ||
| import { ToDo } from '../../App'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| type Props = { | ||
| todo: ToDo; | ||
| }; | ||
|
|
||
| export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||
| const { user, id, completed, title } = todo; | ||
|
|
||
| return ( | ||
| <article | ||
| data-id={id} | ||
| className={classNames('TodoInfo', { | ||
| 'TodoInfo--completed': completed, | ||
| })} | ||
| > | ||
| <h2 className="TodoInfo__title">{title}</h2> | ||
| {user && <UserInfo user={user} />} | ||
| </article> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,16 @@ | ||
| export const TodoList = () => {}; | ||
| import { ToDo } from '../../App'; | ||
| import { TodoInfo } from '../TodoInfo'; | ||
|
|
||
| type Props = { | ||
| todos: ToDo[]; | ||
| }; | ||
|
|
||
| export const TodoList: React.FC<Props> = ({ 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. Potential type/symbol error: the component is typed as |
||
| return ( | ||
| <section className="TodoList"> | ||
| {todos.map(todo => ( | ||
| <TodoInfo key={todo.id} todo={todo} /> | ||
| ))} | ||
| </section> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,13 @@ | ||
| export const UserInfo = () => {}; | ||
| import { User } from '../../App'; | ||
|
|
||
| type Props = { | ||
| user: User; | ||
| }; | ||
|
|
||
| export const UserInfo: React.FC<Props> = ({ user }) => { | ||
| return ( | ||
| <a className="UserInfo" href={`mailto:${user.email}`}> | ||
| {user.name} | ||
| </a> | ||
| ); | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.