-
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
15 changed files
with
197 additions
and
164 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
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,101 @@ | ||
import React, { useState } from 'react'; | ||
import { Todo, User } from '../../types'; | ||
import { createNewId, getUserById, removeInvalidCharacters } from '../../utils'; | ||
|
||
type Props = { | ||
todos: Todo[]; | ||
users: User[]; | ||
handleAdd: (todo: Todo) => void; | ||
}; | ||
|
||
export const NewTodoForm: React.FC<Props> = ({ todos, users, handleAdd }) => { | ||
const [title, setTitle] = useState(''); | ||
const [hasTitleInputError, setHasTitleInputError] = useState(false); | ||
const [userId, setUserId] = useState(0); | ||
const [hasUserSelectError, setHasUserSelectError] = useState(false); | ||
|
||
function handleSubmit(event: React.FormEvent<HTMLFormElement>) { | ||
event.preventDefault(); | ||
|
||
if (!title) { | ||
setHasTitleInputError(true); | ||
} | ||
|
||
if (!userId) { | ||
setHasUserSelectError(true); | ||
} | ||
|
||
if (title && userId) { | ||
setTitle(''); | ||
setUserId(0); | ||
handleAdd({ | ||
id: createNewId(todos), | ||
title: removeInvalidCharacters(title), | ||
completed: false, | ||
userId, | ||
user: getUserById(users, userId), | ||
}); | ||
} | ||
} | ||
|
||
function handleTitleInputChange(event: React.ChangeEvent<HTMLInputElement>) { | ||
setTitle(event.target.value); | ||
setHasTitleInputError(false); | ||
} | ||
|
||
function handleUserSelect(event: React.ChangeEvent<HTMLSelectElement>) { | ||
setUserId(Number(event.target.value)); | ||
setHasUserSelectError(false); | ||
} | ||
|
||
return ( | ||
<form action="/api/todos" method="POST" onSubmit={handleSubmit}> | ||
<div className="field"> | ||
<label> | ||
{`Title: `} | ||
<input | ||
type="text" | ||
name="title" | ||
placeholder="Enter a title" | ||
value={title} | ||
onChange={handleTitleInputChange} | ||
data-cy="titleInput" | ||
/> | ||
</label> | ||
{hasTitleInputError && ( | ||
<span className="error">Please enter a title</span> | ||
)} | ||
</div> | ||
|
||
<div className="field"> | ||
<label> | ||
{`User: `} | ||
<select | ||
name="user" | ||
required | ||
value={userId} | ||
onChange={handleUserSelect} | ||
data-cy="userSelect" | ||
> | ||
<option value="0" disabled> | ||
Choose a user | ||
</option> | ||
{users.map(user => ( | ||
<option value={user.id} key={user.id}> | ||
{user.name} | ||
</option> | ||
))} | ||
</select> | ||
</label> | ||
|
||
{hasUserSelectError && ( | ||
<span className="error">Please choose a user</span> | ||
)} | ||
</div> | ||
|
||
<button type="submit" data-cy="submitButton"> | ||
Add | ||
</button> | ||
</form> | ||
); | ||
}; |
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 @@ | ||
export * from './NewTodoForm'; |
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,27 +1,20 @@ | ||
import { getUserById } from '../../services'; | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import { Todo } from '../../types'; | ||
import { UserInfo } from '../UserInfo'; | ||
|
||
import classNames from 'classnames'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
}; | ||
|
||
export const TodoInfo: React.FC<Props> = ({ todo }) => { | ||
const user = getUserById(todo.userId); | ||
|
||
return ( | ||
<article | ||
data-id={todo.id} | ||
className={classNames('TodoInfo', { | ||
'TodoInfo--completed': todo.completed, | ||
})} | ||
key={todo.id} | ||
> | ||
<h2 className="TodoInfo__title">{todo.title}</h2> | ||
|
||
{user && <UserInfo user={user} />} | ||
</article> | ||
); | ||
}; | ||
export const TodoInfo: React.FC<Props> = ({ todo }) => ( | ||
<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} />} | ||
</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,16 +1,15 @@ | ||
import React from 'react'; | ||
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> | ||
); | ||
}; | ||
export const TodoList: React.FC<Props> = ({ todos }) => ( | ||
<section className="TodoList"> | ||
{todos.map(todo => ( | ||
<TodoInfo todo={todo} key={todo.id} /> | ||
))} | ||
</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,13 +1,12 @@ | ||
import React from 'react'; | ||
import { User } from '../../types'; | ||
|
||
type Props = { | ||
user: User; | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); | ||
}; | ||
export const UserInfo: React.FC<Props> = ({ user }) => ( | ||
<a className="UserInfo" href={`mailto:${user.email}`}> | ||
{user.name} | ||
</a> | ||
); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
userId: number; | ||
user: User | null; | ||
} |
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 interface User { | ||
id: number; | ||
name: string; | ||
username: string; | ||
email: string; | ||
} |
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,2 @@ | ||
export * from './Todo'; | ||
export * from './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,9 @@ | ||
import { Todo } from '../types'; | ||
|
||
export function createNewId(todos: Todo[]) { | ||
const maxId = todos.reduce((acc: number, todo: Todo) => { | ||
return Math.max(todo.id, acc); | ||
}, 0); | ||
|
||
return maxId + 1; | ||
} |
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,5 @@ | ||
import { User } from '../types'; | ||
|
||
export function getUserById(users: User[], userId: number) { | ||
return users.find(user => user.id === userId) || null; | ||
} |
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,3 @@ | ||
export * from './createNewId'; | ||
export * from './getUserById'; | ||
export * from './removeInvalidCharacters'; |
Oops, something went wrong.