Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixka4 committed Jan 18, 2025
1 parent c4d03d0 commit fa62a71
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 40 deletions.
127 changes: 90 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,114 @@
import React, { useState } from 'react';
import './App.scss';
import { TodoList } from './components/TodoList';
import usersFromServer from './api/users';
import todosFromServer from './api/todos';

// import usersFromServer from './api/users';
// import todosFromServer from './api/todos';
type User = {
id: number;
name: string;
email: string;
username: string;
};

type Todo = {
id: number;
title: string;
completed: boolean;
user: User | null;
};

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

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

export const App: React.FC = () => {
const [title, setTitle] = useState('');
const [userId, setUserId] = useState(0);
const [hasTitleError, setHasTitleError] = useState(false);
const [hasSelectError, setHasSelectError] = useState(false);
const [todos, setTodos] = useState<Todo[]>(initialTodos);

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

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

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

setHasTitleError(!title);
setHasSelectError(!userId);

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

const newTodo: Todo = {
id: todos.length + 1,
title,
completed: false,
user: getUserById(userId),
};

setTodos(currentTodos => [...currentTodos, newTodo]);
setTitle('');
setUserId(0);
};

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

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

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

<span className="error">Please choose a user</span>
{hasSelectError && (
<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>
<TodoList todos={todos} />
</div>
);
};
18 changes: 17 additions & 1 deletion src/components/TodoInfo/TodoInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export const TodoInfo = () => {};
import { UserInfo } from '../UserInfo';

type TodoInfoProps = {
todo: Todo;
};

export const TodoInfo: React.FC<TodoInfoProps> = ({ 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>
);
};
23 changes: 22 additions & 1 deletion src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export const TodoList = () => {};
import { TodoInfo } from '../TodoInfo';

type Todo = {
id: number;
title: string;
completed: boolean;
user: User | null;
};

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

export const TodoList: React.FC<TodoListProps> = ({ todos }) => {
return (
<section className="TodoList">
{todos.map(todo => (
<TodoInfo key={todo.id} todo={todo} />
))}
</section>
);
};
12 changes: 11 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export const UserInfo = () => {};
type UserInfoProps = {
user: User;
};

export const UserInfo: React.FC<UserInfoProps> = ({ user }) => {
return (
<a className="UserInfo" href={`mailto:${user.email}`}>
{user.name}
</a>
);
};

0 comments on commit fa62a71

Please sign in to comment.