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

task solution #2151

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
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://DenysShurubura.github.io/react_add-todo-form/) and add it to the PR description.
139 changes: 100 additions & 39 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,122 @@
import './App.scss';
import { useState } from 'react';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
import { TodoList } from './components/TodoList';
import { Todo } from './types';

import usersFromServer from './api/users';
import todosFromServer from './api/todos';

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

const todos: Todo[] = todosFromServer.map(todo => ({
...todo,
user: getUserById(todo.userId),
}));

export const App = () => {
const [currentTodos, setCurrentTodos] = useState(todos);
const [title, setTitle] = useState('');
const [titleError, setTitleError] = useState(false);
const [userId, setUserId] = useState(0);
const [userError, setUserError] = useState(false);

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

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

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

if (title && userId) {
const newTodo = {
id: Math.max(...currentTodos.map(todo => todo.id)) + 1,
title,
completed: false,
userId,
user: getUserById(userId),
};

setCurrentTodos(prevTodos => [...prevTodos, newTodo]);

setTitle('');
setTitleError(false);
setUserId(0);
setUserError(false);
}
};

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

<form action="/api/todos" method="POST">
<form
action="/api/todos"
method="POST"
onSubmit={submitHandler}
>
<div className="field">
<input type="text" data-cy="titleInput" />
<span className="error">Please enter a title</span>
<label>
Title:
<input
type="text"
data-cy="titleInput"
placeholder="Enter a title"
value={title}
onChange={(event) => {
setTitle(event.target.value);
setTitleError(false);
}}
/>
</label>

{titleError
&& (<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>
<label>
User:
<select
data-cy="userSelect"
defaultValue="0"
onChange={event => {
setUserId(+event.target.value);
setUserError(false);
}}
>
<option value="0" disabled>Choose a user</option>
{usersFromServer.map(user => (
<option
value={user.id}
key={user.id}
>
{user.name}
</option>
))}
</select>
</label>

<span className="error">Please choose a user</span>
{userError
&& (<span className="error">Please choose a user</span>)}
</div>

<button type="submit" data-cy="submitButton">
<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={currentTodos} />
</div>
);
};
22 changes: 21 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
export const TodoInfo = () => {};
import { Todo } from '../../types';
import { UserInfo } from '../UserInfo';

interface 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>

<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';
import { TodoInfo } from '../TodoInfo';

interface 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';

interface Props {
user: User | null
}

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

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