Skip to content
Open

Develop #3572

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://Skay9n.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
110 changes: 75 additions & 35 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,101 @@
import './App.scss';
import { TodoList } from './components/TodoList';
import usersFromServer from './api/users';
import todosFromServer from './api/todos';
import { Todo, User } from './types/types';
import { useState } from 'react';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
const getUserById = (userId: number): User | null => {
return usersFromServer.find((user: User) => user.id === userId) || null;
};

const getTodoWithUser = (todo: Omit<Todo, 'user'>): Todo => {
return { ...todo, user: getUserById(todo.userId) };
};

const initialTodos = todosFromServer.map(getTodoWithUser);

export const App = () => {
const [todos, setTodos] = useState(initialTodos);
const [title, setTitle] = useState('');
const [userId, setUserId] = useState(0);
const [titleError, setTitleError] = useState(false);
const [userError, setUserError] = useState(false);

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

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

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

if (!title || !userId) {
return;
}

const newTodo: Todo = {
id: todos.length > 0 ? Math.max(...todos.map(todo => todo.id)) + 1 : 1,
title,
userId,
completed: false,
user: getUserById(userId),
};

setTodos([...todos, newTodo]);
setTitle('');
setUserId(0);
};

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>
<input
type="text"
data-cy="titleInput"
value={title}
placeholder="Enter a 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">
<select
data-cy="userSelect"
value={userId}
onChange={event => {
setUserId(+event.target.value);
setUserError(false);
}}
>
<option value="0" disabled>
Choose a user
</option>
{usersFromServer.map(user => (
<option key={user.id} value={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>
);
};
22 changes: 21 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
export const TodoInfo = () => {};
import { UserInfo } from '../UserInfo';
import { Todo } from '../../types/types';

type Props = {
todo: Todo;
};

export const TodoInfo = ({ todo }: Props) => {
return (
<>
<article
data-id={todo.id}
className={`TodoInfo ${todo.completed ? 'TodoInfo--completed' : ''}`}
>
<h2 className="TodoInfo__title">{todo.title}</h2>

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

Choose a reason for hiding this comment

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

It's a good practice to define component-specific props within the component's file. The Props type defined in types/types.ts is only used by TodoList. It would be better to move this type definition into this file to make the component more self-contained. The types.ts file should be reserved for types that are shared across multiple components, such as Todo and User.


export const TodoList = ({ todos }: Props) => {
return (
<section className="TodoList">
{todos.map(todo => (
<TodoInfo key={todo.id} todo={todo} />
))}
</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/types';

type Props = {
user: User;
};

export const UserInfo = ({ user }: Props) => {
return (
<a className="UserInfo" href={`mailto:${user.email}`}>
{user.name}
</a>
);
};
18 changes: 18 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type User = {
id: number;
name: string;
username: string;
email: string;
};

export type Todo = {
id: number;
title: string;
completed: boolean;
userId: number;
user: User | null;
};

export type Props = {
todos: Todo[];
};
Comment on lines +16 to +18
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This Props type is only used by the TodoList component. It's a best practice to define props specific to a single component within that component's file. This improves component encapsulation and makes the codebase easier to navigate and maintain. Please move this type definition to src/components/TodoList/TodoList.tsx.