From 0472175c903285c63858b24200c4a80691bd22c6 Mon Sep 17 00:00:00 2001 From: andrew gidrovich Date: Thu, 2 Nov 2023 19:35:51 +0200 Subject: [PATCH] add task solution --- src/App.tsx | 127 +++++++++++++++++++-------- src/api/users.ts | 2 +- src/components/TodoInfo/TodoInfo.tsx | 26 +++++- src/components/TodoList/TodoList.tsx | 18 +++- src/components/UserInfo/UserInfo.tsx | 18 +++- src/types.tsx | 14 +++ 6 files changed, 163 insertions(+), 42 deletions(-) create mode 100644 src/types.tsx diff --git a/src/App.tsx b/src/App.tsx index 9646bf5c6f..781153298c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,61 +1,112 @@ +import { ChangeEventHandler, FormEventHandler, 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 { Todo } from './types'; export const App = () => { + const [selectedUserId, setSelectedUserId] = useState(0); + const [typedTitle, setTypedTitle] = useState(''); + const [todosList, setTodosList] = useState(todosFromServer); + const [hasTitleError, setHasTitleError] = useState(false); + const [hasNameError, setHasNameError] = useState(false); + + const handleSubmit: FormEventHandler = (event) => { + event.preventDefault(); + + if (!typedTitle) { + setHasTitleError(true); + } + + if (selectedUserId === 0) { + setHasNameError(true); + } + + const maxId = Math.max(...todosList.map(todo => todo.id)); + + if (typedTitle !== '' && selectedUserId) { + const newTodo: Todo = { + id: maxId + 1, + title: typedTitle, + userId: +selectedUserId, + completed: false, + }; + + setTodosList((prevTodos) => [...prevTodos, newTodo]); + setTypedTitle(''); + setSelectedUserId(0); + } + }; + + const handleChangeTitle: ChangeEventHandler = (event) => { + setTypedTitle(event.target.value); + setHasTitleError(false); + }; + + const handleUserChange: ChangeEventHandler = (event) => { + setSelectedUserId(+event.target.value); + setHasNameError(false); + }; + + const todos = () => todosList.map(todo => ({ + ...todo, + user: usersFromServer.find(u => u.id === todo.userId), + })); + return (

Add todo form

- - Please enter a title + + + {hasTitleError && Please enter a title}
- + + + {hasNameError + && Please choose a user}
-
-
- - - - - -
+
); }; diff --git a/src/api/users.ts b/src/api/users.ts index 8438ad7a5f..101e16a84d 100644 --- a/src/api/users.ts +++ b/src/api/users.ts @@ -59,4 +59,4 @@ export default [ username: 'Moriah.Stanton', email: 'Rey.Padberg@karina.biz', }, -]; +]; \ No newline at end of file diff --git a/src/components/TodoInfo/TodoInfo.tsx b/src/components/TodoInfo/TodoInfo.tsx index d164511fa8..ac30133259 100644 --- a/src/components/TodoInfo/TodoInfo.tsx +++ b/src/components/TodoInfo/TodoInfo.tsx @@ -1 +1,25 @@ -export const TodoInfo = () => {}; +import cn from 'classnames'; +import { UserInfo } from '../UserInfo'; +import { Todo } from '../../types'; + +interface TodoInfoProps { + todo: Todo; + // user?: User; +} + +export const TodoInfo = ({ todo }: TodoInfoProps) => { + return ( +
+

+ {todo.title} +

+ {todo.user + && } +
+ ); +}; diff --git a/src/components/TodoList/TodoList.tsx b/src/components/TodoList/TodoList.tsx index c12fae07c0..1f666cebfa 100644 --- a/src/components/TodoList/TodoList.tsx +++ b/src/components/TodoList/TodoList.tsx @@ -1 +1,17 @@ -export const TodoList = () => {}; +import { TodoInfo } from '../TodoInfo'; +import { Todo } from '../../types'; + +type TodoListProps = { + todos: Todo[]; +}; + +export const TodoList = ({ todos }: TodoListProps) => ( +
+ {todos.map(todo => ( + + ))} +
+); diff --git a/src/components/UserInfo/UserInfo.tsx b/src/components/UserInfo/UserInfo.tsx index f7bf0410ec..aae66929aa 100644 --- a/src/components/UserInfo/UserInfo.tsx +++ b/src/components/UserInfo/UserInfo.tsx @@ -1 +1,17 @@ -export const UserInfo = () => {}; +import { User } from '../../types'; + +type UserInfoProps = { + user?: User, +}; + +export const UserInfo = ({ user }: UserInfoProps) => { + if (!user) { + return null; + } + + return ( + + {user.name} + + ); +}; diff --git a/src/types.tsx b/src/types.tsx new file mode 100644 index 0000000000..6b2d8cb7b1 --- /dev/null +++ b/src/types.tsx @@ -0,0 +1,14 @@ +export interface Todo { + id: number; + title: string; + userId: number; + completed: boolean; + user?: User | null; +} + +export interface User { + id: number, + name: string, + username: string, + email: string, +}