Skip to content

Commit

Permalink
react_add-todo-form
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav65 committed Dec 24, 2024
1 parent 8d91581 commit bb515c1
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 54 deletions.
81 changes: 30 additions & 51 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
import './App.scss';
import { TodoList } from './components/TodoList';
import todosFromServer from './api/todos';
import { useState } from 'react';
import { getUserById } from './services/user';
import { Todo } from './types/Todo';
import { PostForm } from './PostForm';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
const initialTodos: Todo[] = todosFromServer.map(todo => ({
...todo,
user: getUserById(todo.userId),
}));

function getNewTodoId(todos: Todo[]) {
const maxId = Math.max(...todos.map(todo => todo.id));

return maxId + 1;
}

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>(initialTodos);

const onAddTodo = ({ id, ...data }: Todo) => {
const newTodo = {
id: getNewTodoId(todos),
...data,
};

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

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>

<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>
<PostForm onSubmit={onAddTodo} />
<TodoList todos={todos} />
</div>
);
};
104 changes: 104 additions & 0 deletions src/PostForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import usersFromServer from './api/users';
import { useState } from 'react';
import { getUserById } from './services/user';
import { Todo } from './types/Todo';

type Props = {
onSubmit: (todo: Todo) => void;
};

export const PostForm: React.FC<Props> = ({ onSubmit }) => {
const [titleErrorMessage, setTitleErrorMessage] = useState('');
const [title, setTitle] = useState('');
const hasTitleError = titleErrorMessage !== '';
const [hasUserIdError, setHasUserIdError] = useState(false);
const [userId, setUserId] = useState(0);

const handleUserIdChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setUserId(+event.target.value);
setHasUserIdError(false);
};

const reset = () => {
setUserId(0);
setTitle('');
setTitleErrorMessage('');
setHasUserIdError(false);
};

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

setHasUserIdError(!userId);

if (!title) {
setTitleErrorMessage('Please enter a title');

return;
}

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

onSubmit({
id: 0,
title,
completed: false,
userId,
user: getUserById(userId),
});

reset();
};

return (
<form action="/api/todos" method="POST" onSubmit={handleSubmit}>
<div className="field">
Title:&nbsp;
<input
type="text"
data-cy="titleInput"
placeholder="Enter a title"
value={title}
onChange={event => {
const inputValue = event.target.value;

setTitle(inputValue);

if (inputValue) {
setTitleErrorMessage('');
} else {
setTitleErrorMessage('Please enter a title');
}
}}
/>
{hasTitleError && <span className="error">{titleErrorMessage}</span>}
</div>

<div className="field">
User:&nbsp;
<select
data-cy="userSelect"
value={userId}
onChange={handleUserIdChange}
>
<option value="0" disabled>
Choose a user
</option>

{usersFromServer.map(user => (
<option value={user.id} key={user.id}>
{user.name}
</option>
))}
</select>
{hasUserIdError && <span className="error">Please choose a user</span>}
</div>

<button type="submit" data-cy="submitButton">
Add
</button>
</form>
);
};
20 changes: 19 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
export const TodoInfo = () => {};
import { Todo } from '../../types/Todo';
import { UserInfo } from '../UserInfo';

type Props = {
todo: Todo;
};

export const TodoInfo: React.FC<Props> = ({ todo }) => {
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>
);
};
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/Todo';
import { TodoInfo } from '../TodoInfo';

type Props = {
todos: Todo[];
};

export const TodoList: React.FC<Props> = ({ todos }) => {
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/User';

type Props = {
user: User;
};

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

export function getUserById(userId: number): User | null {
return usersFromServer.find(user => user.id === userId) || null;
}
9 changes: 9 additions & 0 deletions src/types/Todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { User } from './User';

export interface Todo {
id: number;
title: string;
completed: boolean;
userId: number;
user: User | null;
}
6 changes: 6 additions & 0 deletions src/types/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface User {
id: number;
name: string;
username: string;
email: string;
}

0 comments on commit bb515c1

Please sign in to comment.