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

solution #2923

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
121 changes: 88 additions & 33 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,116 @@
import { useEffect, useState } from 'react';
import './App.scss';
import todosFromServer from './api/todos';
import usersFromServer from './api/users';

Choose a reason for hiding this comment

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

The usersFromServer should not be directly imported and used here. According to the task requirements, data preparation should be centralized in the App component, and then passed as props to child components.

import { TodoList } from './components/TodoList';
import { Todo } from './type/todo';
import { User } from './type/user';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';

export const App = () => {
const [todos, setTodos] = useState([] as Todo[]);
const [todoTitle, setTodoTitle] = useState('');
const [userId, setUserId] = useState(0);

Choose a reason for hiding this comment

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

The userId state is initialized to 0, which is also the value used for the disabled option in the user select dropdown. This means that if a user doesn't change the selection, the form will submit with userId as 0, which might not be valid. Consider initializing userId to a non-zero value or handling this case explicitly.

Choose a reason for hiding this comment

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

Initializing userId to 0 can lead to form submission with an invalid user ID. Consider initializing userId to a non-zero value or adding explicit handling for this case.

const users: User[] = usersFromServer;
const [userError, setUserError] = useState(false);
const [titleError, setTitleError] = useState(false);

const clearState = () => {
setUserError(false);
setTitleError(false);
setTodoTitle('');
setUserId(0);
};

const changeTitle = (event: React.ChangeEvent<HTMLInputElement>) => {
event.preventDefault();
setTitleError(false);
setTodoTitle(event.target.value);
};

const addNewTodo = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const isUserExist = users.some(el => el.id === userId);

if (!isUserExist || todoTitle.trim().length === 0) {
setTitleError(todoTitle.trim().length === 0);
setUserError(!isUserExist);

return;
}

const ids = todos.map(el => el.id);

const newTodo = {
id: ids ? Math.max(...ids) + 1 : 1,
AlenaLoik marked this conversation as resolved.
Show resolved Hide resolved
title: todoTitle,
completed: false,
userId: userId,
user: users.find(el => el.id === userId),
};

setTodos([...todos, newTodo]);
clearState();
};

useEffect(() => {
const prepareTodos = todosFromServer.map(user => {
return {
...user,
user: users.find(el => el.id === user.userId),
};
});

setTodos(prepareTodos);
}, [users]);

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

<form action="/api/todos" method="POST">
<form onSubmit={addNewTodo}>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<input
type="text"
data-cy="titleInput"
placeholder="Add new todo..."
value={todoTitle}
onChange={event => changeTitle(event)}
/>
{titleError && <span className="error">Please enter a title</span>}
</div>

<div className="field">
<select data-cy="userSelect">
<select
data-cy="userSelect"
onChange={event => {
setUserError(false);
setUserId(+event.target.value);
}}
value={userId}
>
<option value="0" disabled>
Choose a user
</option>
{users.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} />
</div>
);
};
23 changes: 22 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export const TodoInfo = () => {};
import { Todo } from '../../type/todo';
import { UserInfo } from '../UserInfo';
import classNames from 'classnames';

interface Props {
todo: Todo;
}

export const TodoInfo: React.FC<Props> = ({ todo }) => {
const { id, title, completed, user } = todo;

return (
<article
data-id={id}
key={id}
className={classNames('TodoInfo', { 'TodoInfo--completed': completed })}
>
<h2 className="TodoInfo__title">{title}</h2>
{user && <UserInfo user={user} />}
</article>
);
};
17 changes: 16 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
export const TodoList = () => {};
import { Todo } from '../../type/todo';
import { TodoInfo } from '../TodoInfo';

interface Props {
todos: Todo[];
}

export const TodoList: React.FC<Props> = ({ todos }) => {
return (
<section className="TodoList">
{todos.map(todo => {
return <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 '../../type/user';

interface Props {
user: User;
}

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

export type Todo = {
id: number;
title: string;
completed: boolean;
userId: number;
user?: User;
};
6 changes: 6 additions & 0 deletions src/type/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