From abd9aa2a484f8ce5bdba96b1cfd9a89ed00540bc Mon Sep 17 00:00:00 2001 From: Andrii Tkachuk Date: Sat, 21 Dec 2024 21:09:58 +0100 Subject: [PATCH] add getUser --- src/App.tsx | 6 ++---- src/components/UserInfo/UserInfo.tsx | 10 +++------- src/utils/getUser.ts | 6 ++++++ 3 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 src/utils/getUser.ts diff --git a/src/App.tsx b/src/App.tsx index 7e144fe95..589344e27 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -9,7 +9,7 @@ import { Todo } from './types/types'; const initialTodos: Todo[] = todosFromServer; function getNewTodoId(todos: Todo[]) { - const maxId = Math.max(...todos.map(todo => todo.id)); + const maxId = Math.max(0, ...todos.map(todo => todo.id)); return maxId + 1; } @@ -77,9 +77,7 @@ export const App = () => { value={title} onChange={handleTitle} /> - {hasTitleError && ( - Please enter a title{title} - )} + {hasTitleError && Please enter a title}
diff --git a/src/components/UserInfo/UserInfo.tsx b/src/components/UserInfo/UserInfo.tsx index 66358154d..f66664b91 100644 --- a/src/components/UserInfo/UserInfo.tsx +++ b/src/components/UserInfo/UserInfo.tsx @@ -1,17 +1,13 @@ import React from 'react'; -import { Todo, User } from '../../types/types'; -import usersFromServer from '../../api/users'; +import { Todo } from '../../types/types'; +import { getUser } from '../../utils/getUser'; type Props = { todo: Todo; }; -function getUser(arrUsers: User[], userId: number): User | undefined { - return arrUsers.find(el => el.id === userId); -} - export const UserInfo: React.FC = ({ todo }) => { - const user = getUser(usersFromServer, todo.userId); + const user = getUser(todo.userId); if (!user) { return Unknown User; diff --git a/src/utils/getUser.ts b/src/utils/getUser.ts new file mode 100644 index 000000000..781cc3860 --- /dev/null +++ b/src/utils/getUser.ts @@ -0,0 +1,6 @@ +import { User } from '../types/types'; +import usersFromServer from '../api/users'; + +export function getUser(userId: number): User | undefined { + return usersFromServer.find(el => el.id === userId); +}