Skip to content

Commit

Permalink
onClick_HandlerFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykyta Romanishyn committed Aug 25, 2024
1 parent f013c28 commit 1ad5170
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 78 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/build
/node_modules
/.eslintrc.js
184 changes: 116 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@mate-academy/eslint-config-react-typescript": "latest",
"@mate-academy/scripts": "^1.7.9",
"@mate-academy/scripts": "^1.9.4",
"@mate-academy/students-ts-config": "latest",
"@mate-academy/stylelint-config": "latest",
"@types/node": "^16.18.80",
Expand Down
25 changes: 18 additions & 7 deletions src/components/UserSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useContext, useEffect, useRef, useState } from 'react';
import React, {
useContext,
useEffect,
useRef,
useState,
useCallback,
} from 'react';
import classNames from 'classnames';
import { User } from '../types/User';
import { getUsers } from '../api/users';
Expand Down Expand Up @@ -35,6 +41,15 @@ export const UserSelector: React.FC = () => {
};
}, []);

const handleUserSelection = useCallback(
(user: User) => {
setSelectedUser(user);
setSelectedPost(null);
setIsVisible(false);
},
[setSelectedUser, setSelectedPost, setIsVisible],
);

return (
<div
ref={dropdownRef}
Expand Down Expand Up @@ -73,18 +88,14 @@ export const UserSelector: React.FC = () => {
className={classNames('dropdown-item', {
'is-active': user.id === selectedUser?.id,
})}
onClick={() => {
setSelectedUser(user);
setSelectedPost(null);
setIsVisible(false);
}}
onClick={() => handleUserSelection(user)}
>
{user.name}
</a>
))
) : (
<div className="dropdown-item has-background-danger has-text-light">
Something go wrong! Refresh the page or try again later.
Something went wrong! Refresh the page or try again later.
</div>
)}
</div>
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"extends": "@mate-academy/students-ts-config",
"include": [
"src"
"src",
".eslintrc.js"
],
"compilerOptions": {
"sourceMap": false,
"types": ["node", "cypress"]
"types": ["node", "cypress"],
"jsx": "react"
}
}

0 comments on commit 1ad5170

Please sign in to comment.