Skip to content

Commit

Permalink
add getUser
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaTkachuk committed Dec 21, 2024
1 parent bc88f9d commit abd9aa2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -77,9 +77,7 @@ export const App = () => {
value={title}
onChange={handleTitle}
/>
{hasTitleError && (
<span className="error">Please enter a title{title}</span>
)}
{hasTitleError && <span className="error">Please enter a title</span>}
</div>

<div className="field">
Expand Down
10 changes: 3 additions & 7 deletions src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -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<Props> = ({ todo }) => {
const user = getUser(usersFromServer, todo.userId);
const user = getUser(todo.userId);

if (!user) {
return <span className="UserInfo">Unknown User</span>;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/getUser.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit abd9aa2

Please sign in to comment.