diff --git a/src/App.tsx b/src/App.tsx index a9a9bb4c53..e79135fcf2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,114 @@ import './App.scss'; -// import usersFromServer from './api/users'; -// import todosFromServer from './api/todos'; +import usersFromServer from './api/users'; +import todosFromServer from './api/todos'; +import { UserInfo } from './components/UserInfo'; +import { TodoList } from './components/TodoList'; +import { useState } from 'react'; + +type Todo = { + id: number; + title: string; + userId: number; + completed: boolean; +}; + +export const App = ({}) => { + const [todoList, setTodoList] = useState([...todosFromServer]); + const [title, setTitle] = useState(''); + const [userValue, setUserValue] = useState(0); + const [inputError, setInputError] = useState(''); + const [selectError, setSelectError] = useState(''); + + function getUserById(userId: number) { + return usersFromServer.find(user => user.id === userId) || null; + } + + const todos = todoList.map(todo => ({ + ...todo, + user: getUserById(todo.userId), + })); + + function getNewId(listOfTodos: Todo[]) { + const maxId = Math.max(...listOfTodos.map(todo => todo.id)); + + return maxId + 1; + } + + const handleInputError = (event: React.ChangeEvent) => { + event.preventDefault(); + setTitle(event.target.value); + setInputError(''); + }; + + const handleSelectError = (event: React.ChangeEvent) => { + event.preventDefault(); + setUserValue(+event.target.value); + setSelectError(''); + }; + + const cleanForm = () => { + setTitle(''); + setUserValue(0); + setInputError(''); + setSelectError(''); + }; + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault(); + + const todo: Todo = { + id: getNewId(todos), + title: title, + userId: userValue, + completed: false, + }; + + if (!title && !userValue) { + setInputError('Please enter a title'); + setSelectError('Please choose a user'); + } else if (!title) { + setInputError('Please enter a title'); + } else if (!userValue) { + setSelectError('Please choose a user'); + } else { + setTodoList([...todoList, todo]); + cleanForm(); + } + }; -export const App = () => { return (

Add todo form

-
+
- - Please enter a title + + + {inputError}
- + + + - Please choose a user + {selectError}
-
- - - - - -
+
); }; diff --git a/src/components/TodoInfo/TodoInfo.tsx b/src/components/TodoInfo/TodoInfo.tsx index d164511fa8..dde24331ad 100644 --- a/src/components/TodoInfo/TodoInfo.tsx +++ b/src/components/TodoInfo/TodoInfo.tsx @@ -1 +1,32 @@ -export const TodoInfo = () => {}; +import classNames from 'classnames'; + +type TodoInfoProps = { + id: number; + title: string; + email: string; + name: string; + completed: boolean; +}; + +export const TodoInfo = ({ + id, + title, + email, + name, + completed, +}: TodoInfoProps) => { + return ( + + ); +}; diff --git a/src/components/TodoList/TodoList.tsx b/src/components/TodoList/TodoList.tsx index c12fae07c0..8a07560165 100644 --- a/src/components/TodoList/TodoList.tsx +++ b/src/components/TodoList/TodoList.tsx @@ -1 +1,37 @@ -export const TodoList = () => {}; +import { TodoInfo } from '../TodoInfo'; + +type Todo = { + id: number; + title: string; + completed: boolean; + userId: number; + user: { + id: number; + name: string; + username: string; + email: string; + } | null; +}; + +export const TodoList = ({ todos }: { todos: Todo[] }) => { + return ( +
+ {todos.map((todo: Todo) => { + if (!todo.user) { + return null; + } + + return ( + + ); + })} +
+ ); +}; diff --git a/src/components/UserInfo/UserInfo.tsx b/src/components/UserInfo/UserInfo.tsx index f7bf0410ec..cc276daf12 100644 --- a/src/components/UserInfo/UserInfo.tsx +++ b/src/components/UserInfo/UserInfo.tsx @@ -1 +1,20 @@ -export const UserInfo = () => {}; +type User = { + id: number; + name: string; + username: string; + email: string; +}; + +export const UserInfo = ({ users }: { users: User[] }) => { + return ( + <> + {users.map((user: User) => { + return ( + + ); + })} + + ); +};