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

need to add UserInfo class #2927

Open
wants to merge 1 commit 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
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://Dorosh90.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": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
69 changes: 18 additions & 51 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,28 @@
import './App.scss';
import { Todo, TodoList, User } from './components/TodoList';
import todosFromServer from './api/todos';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
import usersFromServer from './api/users';
import { Form } from './components/Form';
import { useState } from 'react';

export const App = () => {
return (
<div className="App">
<h1>Add todo form</h1>

<form action="/api/todos" method="POST">
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
</div>

<div className="field">
<select data-cy="userSelect">
<option value="0" disabled>
Choose a user
</option>
</select>

<span className="error">Please choose a user</span>
</div>
const [usersList] = useState<User[]>(usersFromServer);
const [todoList, setuTodoList] = useState<Todo[]>(todosFromServer);

Choose a reason for hiding this comment

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

The function name setuTodoList seems to be a typo. It should likely be setTodoList to match the naming convention and improve code readability.


<button type="submit" data-cy="submitButton">
Add
</button>
</form>
const addNewTodo = (newTodo: Todo) => {
setuTodoList((currentTodo: Todo[]) => [...currentTodo, newTodo]);
};

<section className="TodoList">
<article data-id="1" className="TodoInfo TodoInfo--completed">
<h2 className="TodoInfo__title">delectus aut autem</h2>

<a className="UserInfo" href="mailto:[email protected]">
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:[email protected]">
Leanne Graham
</a>
</article>
return (
<>
<div className="App">
<h1>Add todo form</h1>

<article data-id="2" className="TodoInfo">
<h2 className="TodoInfo__title">
quis ut nam facilis et officia qui
</h2>
<Form users={usersList} todos={todoList} addFunc={addNewTodo} />

<a className="UserInfo" href="mailto:[email protected]">
Patricia Lebsack
</a>
</article>
</section>
</div>
<TodoList todos={todoList} users={usersList} />
</div>
</>
);
};
2 changes: 1 addition & 1 deletion src/api/todos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default [
{
id: 15,
title: 'some other todo',
completed: false,
completed: true,
userId: 1,
},
{
Expand Down
104 changes: 104 additions & 0 deletions src/components/Form/Form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { User } from '../TodoList';
import { Todo } from '../TodoList';
import React, { useState } from 'react';

interface Props {
users: User[];
todos: Todo[];
addFunc: (newTodo: Todo) => void;
}

export const Form: React.FC<Props> = ({ users, todos, addFunc }) => {
const [title, setTitle] = useState('');

const [userId, setUserId] = useState(0);

const [titleError, setTitleError] = useState(false);
const [userError, setUserError] = useState(false);

function onSubmitChacge(event: React.FormEvent) {

Choose a reason for hiding this comment

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

The function name onSubmitChacge seems to be a typo. It should likely be onSubmitChange to match the intended functionality.

event.preventDefault();

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

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

if (title && userId) {
addFunc({
id: Math.max(...todos.map(todo => todo.id)) + 1,
title: title,
completed: false,
userId: userId,
});

setTitle('');
setUserId(0);
}

return;
}

return (
<form action="/api/todos" method="POST" onSubmit={onSubmitChacge}>
<div className="field">
<label htmlFor="title">
Title: {''}
<input
name="title"
value={title}
type="text"
data-cy="titleInput"
placeholder="Enter ad title"
onChange={event => setTitle(event.target.value)}
/>
{titleError && !title && (
<span className="error">Please enter a title</span>
)}
</label>
</div>

<div className="field">
<label htmlFor="">

Choose a reason for hiding this comment

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

The htmlFor attribute in the label should match the id of the select element for better accessibility. Consider adding an id to the select element and updating this attribute accordingly.

User: {''}
<select
data-cy="userSelect"
value={userId}
onChange={event => {
const selectedId = Number(event.target.value);
const selectedUser = users.find(user => user.id === selectedId);

if (selectedUser) {
setUserId(selectedUser.id);
}
}}
>
<option value="0" disabled>
Choose a user
</option>
{users.map(user => {
return (
<option key={user.id} value={user.id}>
{user.name}
</option>
);
})}
</select>
</label>
{userError && userId === 0 && (
<span className="error">Please choose a user</span>
)}
</div>
<button type="submit" data-cy="submitButton">
Add
</button>
</form>
);
};
1 change: 1 addition & 0 deletions src/components/Form/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Form';
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 React from 'react';
import { Todo } from '../TodoList';
import { User } from '../TodoList';
import { UserInfo } from '../UserInfo';
import classNames from 'classnames';

export const TodoInfo: React.FC<{ todo: Todo; user: User | undefined }> = ({
todo,
user,
}) => {
return (
<article
data-id={todo.id}
className={classNames('TodoInfo', {
'TodoInfo--completed': todo.completed,
})}
>
<h2 className="TodoInfo__title">{todo.title}</h2>

{user && <UserInfo user={user} />}
</article>
);
};
34 changes: 33 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
export const TodoList = () => {};
import React from 'react';
import { TodoInfo } from '../TodoInfo';

export interface Todo {
id: number;
title: string;
completed: boolean;
userId: number;
}

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

interface Props {
todos: Todo[];
users: User[];
}

export const TodoList: React.FC<Props> = ({ todos, users }) => {
return (
<section>
{todos.map(todo => {
const user = users.find(u => u.id === todo.userId);

return <TodoInfo key={todo.id} todo={todo} user={user} />;
})}
</section>
);
};
11 changes: 10 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export const UserInfo = () => {};
import { User } from '../TodoList';
import React from 'react';

export const UserInfo: React.FC<{ user: User }> = ({ user }) => {
return (
<a className="UserInfo" href={'mailto:' + user.email}>
{user.name}
</a>
);
};
Loading