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

solution #2888

Open
wants to merge 2 commits 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
133 changes: 98 additions & 35 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,114 @@
import './App.scss';
import React, { useState, FormEvent, ChangeEvent } from 'react';
import usersFromServer from './api/users';
import todosFromServer from './api/todos';
import { TodoList } from './components/TodoList/TodoList';
import { User, Todo } from './types';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
const findUserById = (userId: number) =>
usersFromServer.find(user => user.id === userId);

const todosWithUser = todosFromServer.map(todo => ({
...todo,
user: findUserById(todo.userId),
}));

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>(todosWithUser);
const [title, setTitle] = useState<string>('');
const [userId, setUserId] = useState<number>(0);
const [isTitleError, setIsTitleError] = useState<boolean>(false);
const [isUserError, setIsUserError] = useState<boolean>(false);

const resetErrors = () => {
setIsTitleError(false);
setIsUserError(false);
};

const resetForm = () => {
setTitle('');
setUserId(0);
};

const handleTitleChange = (e: ChangeEvent<HTMLInputElement>) => {
setTitle(e.target.value);
setIsTitleError(false);
};

const handleUserChange = (e: ChangeEvent<HTMLSelectElement>) => {
setUserId(Number(e.target.value));
setIsUserError(false);
};

const isFormValid = () => {
const hasTitle = title.trim();
const hasUser = userId !== 0;

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

if (!hasUser) {
setIsUserError(true);
}

return hasTitle && hasUser;
};

const handleAddTodo = (event: FormEvent) => {
event.preventDefault();
resetErrors();

if (isFormValid()) {
const newTodo: Todo = {
id: Math.max(...todos.map(todo => todo.id)) + 1,
title,
completed: false,
user: findUserById(userId),
userId,
};

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

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

<form action="/api/todos" method="POST">
<form onSubmit={handleAddTodo}>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<label htmlFor="titleInput">Title</label>
<input
type="text"
id="titleInput"
data-cy="titleInput"
placeholder="Enter TODO title"
value={title}
onChange={handleTitleChange}
/>
{isTitleError && <span className="error">Please enter a title</span>}
</div>

<div className="field">
<select data-cy="userSelect">
<label htmlFor="userSelect">User</label>
<select
id="userSelect"
data-cy="userSelect"
value={userId}
onChange={handleUserChange}
>
<option value="0" disabled>
Choose a user
</option>
{usersFromServer.map((user: User) => (
<option key={user.id} value={user.id}>
{user.name}
</option>
))}
</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">
Expand All @@ -30,31 +117,7 @@ export const App = () => {
</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>
<TodoList todos={todos} />
</section>
</div>
);
Expand Down
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 { UserInfo } from '../UserInfo/UserInfo';
import { Todo } from '../../types';

interface TodoInfoProps {
todo: Todo;
}

export const TodoInfo: React.FC<TodoInfoProps> = ({ todo }) => {
return (
<article
data-id={todo.id}
className={`TodoInfo ${todo.completed ? 'TodoInfo--completed' : ''}`}
>

Choose a reason for hiding this comment

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

To set classes conditionally use classnames or clsx library

<h2 className="TodoInfo__title">{todo.title}</h2>
{todo.user ? (
<UserInfo user={todo.user} />
) : (
<div className="UserInfo">No user assigned</div>
)}
</article>
);
};
18 changes: 17 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export const TodoList = () => {};
import React from 'react';
import { TodoInfo } from '../TodoInfo/TodoInfo';
import { Todo } from '../../types';

interface TodoListProps {
todos: Todo[];
}

export const TodoList: React.FC<TodoListProps> = ({ todos }) => {
return (
<div>
{todos.map(todo => {
return <TodoInfo key={todo.id} todo={todo} />;
})}
</div>
);
};
19 changes: 18 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
export const UserInfo = () => {};
import React from 'react';

interface User {
name: string;
email: string;
}

interface UserInfoProps {
user: User;
}

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

export interface Todo {
id: number;
title: string;
completed: boolean;
userId: number;
user?: User;
}
Loading