Skip to content
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

implementation #2160

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 106 additions & 38 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,129 @@
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 { User } from './types/user';
import { findUserById } from './services/findUserById';
import { getNextTodoId } from './services/getNextTodoId';
import { Todo } from './types/todo';

interface FormFileds {
title: string;
user: User | undefined;
}

export const App = () => {
const [titleError, setEmptyTitleFieldError] = useState(false);
const [userError, setEmptyUserFieldError] = useState(false);

const [todosList, setTodosList] = useState<Todo[]>(todosFromServer);

const [formData, setFormData]
= useState<FormFileds>({ title: '', user: undefined });

const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

setEmptyTitleFieldError(!formData.title.trim());
setEmptyUserFieldError(!formData.user);

if (!formData.title.trim() || !formData.user) {
return;
}

setTodosList(prevTodos => [...prevTodos, {
id: getNextTodoId(todosList),
title: formData.title.trim(),
completed: false,
userId: formData.user ? formData.user.id : 0,
}]);

const form = event.target as HTMLFormElement;

form.reset();

setFormData(({ title: '', user: undefined }));
};

const keyCheck = (event: React.KeyboardEvent) => {
if (!/^[A-Za-zА-Яа-яЁёіїєґҐґ ]*$/
.test(event.key)) {
event.preventDefault();
}
};

const handleUserChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const user = findUserById(+event.currentTarget.value);

setFormData({
title: formData.title,
user: user || undefined,
});

setEmptyUserFieldError(false);
};

const handleTitleChange = (event: React.FormEvent<HTMLInputElement>) => {
setFormData({
...formData,
title: event.currentTarget.value,
});

setEmptyTitleFieldError(false);
};

return (
<div className="App">
<h1>Add todo form</h1>

<form action="/api/todos" method="POST">
<form
action="/api/todos"
method="POST"
onSubmit={handleFormSubmit}
>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<label htmlFor="title">Title: </label>
<input
placeholder="Enter a title"
id="title"
onInput={event => handleTitleChange(event)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onInput={event => handleTitleChange(event)}
onInput={handleTitleChange}

we can simplify it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix it everywhere

type="text"
data-cy="titleInput"
onKeyDown={keyCheck}
/>
{titleError
&& (<span className="error">Please enter a title</span>)}
</div>

<div className="field">
<select data-cy="userSelect">
<label htmlFor="user">User: </label>
<select
id="user"
data-cy="userSelect"
defaultValue={0}
onChange={handleUserChange}
>
<option value="0" disabled>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>
{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={todosList} />
</div>
);
};
27 changes: 26 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
export const TodoInfo = () => {};
import classNames from 'classnames';
import { findUserById } from '../../services/findUserById';
import { Todo } from '../../types/todo';
import { UserInfo } from '../UserInfo';

type Props = {
todo: Todo;
};

export const TodoInfo: React.FC<Props> = ({ todo }) => {
const user = findUserById(todo.userId);

return (
<article
data-id={todo.id}
className={classNames('TodoInfo', {
'TodoInfo--completed': todo.completed,
})}
>
<h2 className="TodoInfo__title">
{todo.title}
</h2>
{todo.userId !== 0 && user && (<UserInfo user={user} />)}
</article>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{todo.userId !== 0 && user && (<UserInfo user={user} />)}
{user && (<UserInfo user={user} />)}

);
};
20 changes: 19 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
export const TodoList = () => {};
import { Todo } from '../../types/todo';
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>
);
};
14 changes: 13 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
export const UserInfo = () => {};
import { User } from '../../types/user';

type Props = {
user: User
};

export const UserInfo: React.FC<Props> = ({ user }) => {
return (
<a className="UserInfo" href={`mailto:${user.email}`}>
{user.name}
</a>
);
};
6 changes: 6 additions & 0 deletions src/services/findUserById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import usersFromServer from '../api/users';
import { User } from '../types/user';

export const findUserById = (id: number): User | null => {
return usersFromServer.find(user => user.id === id) || null;
};
5 changes: 5 additions & 0 deletions src/services/getNextTodoId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Todo } from '../types/todo';

export const getNextTodoId = (todos: Todo[]) => {
return Math.max(...todos.map(todo => todo.id)) + 1;
};
6 changes: 6 additions & 0 deletions src/types/todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type Todo = {
id: number,
title: string,
completed: boolean,
userId: number,
};
6 changes: 6 additions & 0 deletions src/types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type User = {
id: number,
name: string,
username: string,
email: string,
};
Loading