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

develop:modified_todo-form #2900

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,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://pti4ka121.github.io/react_add-todo-form/) and add it to the PR description.
8 changes: 4 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": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
126 changes: 92 additions & 34 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,119 @@
import './App.scss';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
import usersFromServer from './api/users';
import todosFromServer from './api/todos';
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

Ensure that the imported files usersFromServer and todosFromServer exist and are correctly implemented in the project. This is crucial for the functionality of the app.

import { useState } from 'react';
import { FormData, Todo } from './types';
import { TodoList } from './components/TodoList';
import { getNewTodoId } from './services';

const initialForm: FormData = {
title: '',
userId: 0,
};

export const App = () => {
const [todos, setTodos] = useState<Todo[]>(todosFromServer);

Choose a reason for hiding this comment

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

The initial state for todos is set using todosFromServer. Ensure that this data is correctly fetched and structured as expected.

const [formData, setFormData] = useState(initialForm);
const [isTitleError, setIsTitleError] = useState(false);
const [isUserError, setIsUserError] = useState(false);

const { title, userId } = formData;

const isDisabledAdd = !title.trim() || !userId;

const reset = () => {
setFormData({ ...initialForm });
};

const handleAddTodo = (inputData: FormData) => {
const newTodo = {
...inputData,
id: getNewTodoId(todos),
completed: false,
};

setTodos(currentTodos => [...currentTodos, newTodo]);
};

Choose a reason for hiding this comment

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

The handleAddTodo function correctly adds a new todo with a unique ID and default completed status. Ensure that getNewTodoId is implemented correctly to avoid ID conflicts.


const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setFormData(prevData => ({
...prevData,
title: event.target.value,
}));
setIsTitleError(false);
};

const handleUserChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setFormData(prevData => ({
...prevData,
userId: +event.target.value,
}));
setIsUserError(false);
};

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
if (isDisabledAdd) {
setIsTitleError(!title);
setIsUserError(!userId);

return;
}

handleAddTodo(formData);
reset();
};

Choose a reason for hiding this comment

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

The handleSubmit function correctly prevents form submission if validation fails and resets the form upon successful submission. Ensure that the error states are managed properly to provide user feedback.


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 className="label" htmlFor="title">
Title:
</label>
<input
id="title"
type="text"
data-cy="titleInput"
placeholder="Enter title"
value={title}
onChange={handleTitleChange}
/>
{isTitleError && <span className="error">Please enter a title</span>}
</div>

<div className="field">
<select data-cy="userSelect">
<label className="label" htmlFor="user">
User:
</label>
<select
id="user"
data-cy="userSelect"
value={userId}
onChange={handleUserChange}
>
<option value="0" disabled>
Choose a user
</option>
{usersFromServer.map(user => (
<option key={user.id} value={user.id}>
{user.name}
</option>
))}

Choose a reason for hiding this comment

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

Ensure that usersFromServer is an array of user objects with id and name properties, as this is crucial for rendering the user options correctly.

</select>

<span className="error">Please choose a user</span>
{isUserError && <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>
);
};
28 changes: 27 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
export const TodoInfo = () => {};
import { getUserById } from '../../services';
import { Todo } from '../../types';

Choose a reason for hiding this comment

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

Avoid using the import keyword in non-root components like TodoInfo. Instead, prepare data in the App component and pass it down as props. This aligns with the checklist guideline: 'Prepare data in one place (App component) and pass it to child components. Don't use import keyword in non-root components.'

import { UserInfo } from '../UserInfo';

import classNames from 'classnames';

type Props = {
todo: Todo;
};

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

Choose a reason for hiding this comment

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

Ensure that getUserById is correctly fetching the user data based on todo.userId. This function should be verified to return the correct user object or null if not found.

return (
<article
data-id={todo.id}
className={classNames('TodoInfo', {
'TodoInfo--completed': todo.completed,
})}
key={todo.id}
>
<h2 className="TodoInfo__title">{todo.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 '../../types';
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';

type Props = {
user: User;
};

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

export const getUserById = (userId: number): User | null => {
return usersFromServer.find(user => user.id === userId) || null;
};

export const getNewTodoId = (todoList: Todo[]) => {
return Math.max(...todoList.map(todo => todo.id)) + 1;
};
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface Todo {
id: number;
title: string;
completed: boolean;
userId: number;
}

export interface User {
id: number;
name: string;
username: string;
email: string;
}

export type FormData = Omit<Todo, 'id' | 'completed'>;
Loading