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 #2921

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Develop #2921

Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ button {
}

.error {
margin: 0;
padding: 0;
color: red;
}

Expand All @@ -37,3 +39,10 @@ button {
.UserInfo {
font-style: italic;
}
.field {
padding-bottom: 10px;

&__title {
margin-right: 4px;
}
}
131 changes: 96 additions & 35 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,122 @@
import { useState } from 'react';
import './App.scss';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
import usersFromServer from './api/users';
import todosFromServer from './api/todos';
import { TodoList } from './components/TodoList';
import { getHigherId } from './serveses/todos';

Choose a reason for hiding this comment

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

The import path './serveses/todos' seems to be incorrect. Please verify the correct path for the getHigherId function.

import { getUserById } from './serveses/user';

Choose a reason for hiding this comment

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

The import path './serveses/user' seems to be incorrect. Please verify the correct path for the getUserById function.


import { User } from './serveses/types';

const completedTodos = todosFromServer.map(todo => {
const user = getUserById(usersFromServer, todo.userId);

return {
...todo,
user,
};
});

export const App = () => {
const [title, setTitle] = useState('');
const [hasTitleError, setHasTitleError] = useState('');

Choose a reason for hiding this comment

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

The useState hook for hasTitleError should be initialized with a boolean value (e.g., false) instead of an empty string, as it is used to track the presence of an error.


const [selectedUserId, setSelectedUserId] = useState(0);
const [hasUserError, setHasUserError] = useState('');

Choose a reason for hiding this comment

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

The useState hook for hasUserError should be initialized with a boolean value (e.g., false) instead of an empty string, as it is used to track the presence of an error.


const [todos, setTodos] = useState(completedTodos);

const handleTitle = (event: React.ChangeEvent<HTMLInputElement>) => {
setTitle(event.target.value);
setHasTitleError('');
};

const handlUser = (event: React.ChangeEvent<HTMLSelectElement>) => {

Choose a reason for hiding this comment

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

There is a typo in the function name handlUser. It should be handleUser to maintain consistency and clarity in naming conventions.

setSelectedUserId(+event.target.value);
setHasUserError('');
};

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

if (!title) {
setHasTitleError('Please enter a title');
}

if (!selectedUserId) {
setHasUserError('Please choose a user');
}

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

setTodos(currentTodos => {
const user = getUserById(usersFromServer, selectedUserId);

const todo = {
id: getHigherId(todos),
title,
completed: false,
userId: +selectedUserId,
user,
};

return [...currentTodos, todo];
});

setTitle('');
setSelectedUserId(0);
};

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

<form action="/api/todos" method="POST">
<form action="/api/todos" method="POST" onSubmit={addTodo}>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<label htmlFor="title">
<span className="field__title">Title:</span>
<input
type="text"
id="title"
data-cy="titleInput"
value={title}
placeholder="Title"
onChange={handleTitle}
/>
</label>

{hasTitleError && <span className="error">{hasTitleError}</span>}
</div>

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

<span className="error">Please choose a user</span>
{hasUserError && <span className="error">{hasUserError}</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:[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>

<article data-id="2" className="TodoInfo">
<h2 className="TodoInfo__title">
quis ut nam facilis et officia qui
</h2>

<a className="UserInfo" href="mailto:[email protected]">
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 classNames from 'classnames';
import React from 'react';
import { UserInfo } from '../UserInfo';
import { Todo } from '../../serveses/types';

Choose a reason for hiding this comment

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

The import path '../../serveses/types' seems to be incorrect. Please verify the correct path for the Todo type.

type Props = {
todo: Todo;
};

export const TodoInfo: React.FC<Props> = ({ todo }) => {
return (
<article
data-id={todo.id}
className={classNames('TodoInfo', {
'TodoInfo--completed': todo.completed,
})}
>
<h2 className="TodoInfo__title">{todo.title}</h2>
<UserInfo user={todo.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 React from 'react';
import { TodoInfo } from '../TodoInfo';
import { Todo } from '../../serveses/types';

Choose a reason for hiding this comment

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

The import path '../../serveses/types' seems to be incorrect. Please verify the correct path for the Todo type.

type 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>
);
};
15 changes: 14 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export const UserInfo = () => {};
import React from 'react';
import { User } from '../../serveses/types';

Choose a reason for hiding this comment

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

The import path '../../serveses/types' seems to be incorrect. Please verify the correct path for the User type.

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/serveses/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Todo = {
id: number;
title: string;
completed: boolean;
userId: number;
};

export const getHigherId = (todos: Todo[]) => {
return Math.max(...todos.map(todo => todo.id)) + 1;
};
14 changes: 14 additions & 0 deletions src/serveses/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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;
};
10 changes: 10 additions & 0 deletions src/serveses/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type User = {
id: number;
name: string;
username: string;
email: string;
};

Choose a reason for hiding this comment

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

The User type is redefined here, which is unnecessary. Consider importing the User type from types.ts to maintain consistency and avoid redundancy.


export const getUserById = (users: User[], id: number) => {
return users.find(user => user.id === id) || null;
};
Loading