Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ Implement the ability to add TODOs to the `TodoList` implemented in the **Static
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_add-todo-form/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://y-ivsnko.github.io/react_add-todo-form/) and add it to the PR description.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
135 changes: 98 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,122 @@
import { FormEvent, useState } from 'react';
import './App.scss';
import { TodoList } from './components/TodoList';

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

import { Todo } from './types/todo';

Check failure on line 8 in src/App.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Missing file extension for "./types/todo"
import { User } from './types/User';

function getUserById(userId: number): User {
const user = usersFromServer.find(preson => preson.id === userId);

Choose a reason for hiding this comment

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

This is a minor typo. It looks like preson should be person for better clarity.


if (!user) {
throw new Error(`User with id ${userId} was not found`);
}

return user;
}

export const App = () => {
const [title, setTitle] = useState('');
const [titleError, setTitleError] = useState(false);
const [selectedUserId, setSelectedUserId] = useState('');
const [userError, setUserError] = useState(false);
const [todos, setTodos] = useState<Todo[]>(
todosFromServer.map(todo => ({
...todo,
user: getUserById(todo.userId),
})),
);

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

const hasTitle = title.trim().length > 0;
const hasSelectedUser = selectedUserId !== '';

if (!hasTitle) {
setTitleError(true);
}

if (!hasSelectedUser) {
setUserError(true);
}

if (!hasTitle || !hasSelectedUser) {
return;
}

const userId = +selectedUserId;
const user = getUserById(userId);
const maxTodoId =
todos.length > 0 ? Math.max(...todos.map(todo => todo.id)) : 0;

const newTodo: Todo = {
id: maxTodoId + 1,
title: title.trim(),
userId,
completed: false,
user,
};

setTodos(currentTodos => [...currentTodos, newTodo]);
setTitle('');
setSelectedUserId('');
setTitleError(false);
setUserError(false);
};

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

<form action="/api/todos" method="POST">
<form onSubmit={handleSubmit}>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<label htmlFor="title">Title:</label>
<input
id="title"
type="text"
placeholder="Enter a title"
data-cy="titleInput"
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">
<option value="0" disabled>
Choose a user
</option>
<label htmlFor="userId">User:</label>
<select
id="userId"
data-cy="userSelect"
value={selectedUserId}
onChange={event => {
setSelectedUserId(event.target.value);
setUserError(false);
}}
>
<option 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>
{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>
);
};
24 changes: 23 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
export const TodoInfo = () => {};
import { UserInfo } from '../../components/UserInfo';
import { Todo } from '../../types/todo';

Check failure on line 2 in src/components/TodoInfo/TodoInfo.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Missing file extension for "../../types/todo"
import classNames from 'classnames';

type Props = {
todo: Todo;
};

export const TodoInfo = ({ todo }: Props) => {
return (
<article
data-id={todo.id}
className={classNames('TodoInfo', {
'TodoInfo--completed': todo.completed,
})}
key={todo.id}

Choose a reason for hiding this comment

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

The key prop is used by React to identify which items have changed, are added, or are removed in a list. It should be applied to the component when it is rendered within a map function (as is correctly done in TodoList.tsx). Placing the key prop on an element inside the component itself is redundant and has no effect, as key is a special prop that is not passed down to the component's children.

Choose a reason for hiding this comment

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

This key prop is redundant. The key is already being correctly applied to the <TodoInfo /> component where it's mapped in the TodoList component. The key should be on the elements in the array, not on the root element inside the component itself.

>
<h2 className="TodoInfo__title">{todo.title}</h2>

<UserInfo user={todo.user} />
</article>
);
};
15 changes: 14 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export const TodoList = () => {};
import { Todo } from '../../types/todo';

Check failure on line 1 in src/components/TodoList/TodoList.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Missing file extension for "../../types/todo"
import { TodoInfo } from '../TodoInfo';

interface Props {
todos: Todo[];
}

export const TodoList = ({ todos }: Props) => (
<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';

interface Props {
user: User;
}

export const UserInfo = ({ user }: Props) => {
return (
<a className="UserInfo" href={`mailto:${user.email}`}>
{user.name}
</a>
);
};
9 changes: 9 additions & 0 deletions src/types/Todo.ts
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;
}
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 interface User {
id: number;
name: string;
username: string;
email: string;
}
Loading