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 1 commit
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
135 changes: 97 additions & 38 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,120 @@
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';

export const App = () => {
const [emptyTitleFieldError, setEmptyTitleFieldError] = useState(false);
const [emptyUserFieldError, setEmptyUserFieldError] = useState(false);
Copy link

@andriy-shymkiv andriy-shymkiv Nov 4, 2023

Choose a reason for hiding this comment

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

I think enough just:

Suggested change
const [emptyTitleFieldError, setEmptyTitleFieldError] = useState(false);
const [emptyUserFieldError, setEmptyUserFieldError] = useState(false);
const [titleError, setTitleError] = useState(false);
const [userError, setUserError] = useState(false);

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

const [formData, setFormData]
= useState<{ title: string, user: User | null }>({ title: '', user: null });
Copy link

@andriy-shymkiv andriy-shymkiv Nov 4, 2023

Choose a reason for hiding this comment

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

better to create an interface for it:

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

Choose a reason for hiding this comment

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

Suggested change
= useState<{ title: string, user: User | null }>({ title: '', user: null });
= useState<FormFields>({ title: '', user: null });


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

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

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

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

Choose a reason for hiding this comment

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

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


const form = event.target as HTMLFormElement;

form.reset();

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

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

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

setEmptyUserFieldError(false);
};

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

Choose a reason for hiding this comment

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

Suggested change
setFormData({
title: event.currentTarget.value,
user: formData.user,
});
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={event => handleFormSubmit(event)}
>
<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}
/>
{emptyTitleFieldError
&& (<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={event => handleUserChange(event)}

Choose a reason for hiding this comment

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

Suggested change
onChange={event => handleUserChange(event)}
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>
{emptyUserFieldError
&& (<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>
);
};
25 changes: 24 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
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 }) => {
return (
<article
data-id={todo.id}
className={classNames('TodoInfo', {
'TodoInfo--completed': todo.completed,
})}
>
<h2 className="TodoInfo__title">
{todo.title}
</h2>
<UserInfo user={findUserById(todo.userId)} />
</article>

Choose a reason for hiding this comment

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

user can be null, you need to handle this case here

Copy link
Author

@Daniilart01 Daniilart01 Nov 3, 2023

Choose a reason for hiding this comment

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

I decide to move this check using user?.name into TodoInfo component. In case User is null no info will be displayed, but in current implementation we have no possibilities to meet User === null, because when it's null we'll recive ann error 'Please choose user'

Choose a reason for hiding this comment

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

@Daniilart01 but you don't know where else this component can be reused so need to check if user exist

);
};
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 => (
usersFromServer.find(user => user.id === id) as User

Choose a reason for hiding this comment

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

find can return the undefined

Suggested change
usersFromServer.find(user => user.id === id) as User
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