Skip to content

Commit

Permalink
Solution v 0.92
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasyl Zhyliakov committed Oct 27, 2024
1 parent 3aa6d6d commit c5534fe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Post } from './types/Post';
import { getPostsFromServer } from './api/posts';

export const App = () => {
const [isDropDownOpen, setIsDropDownOpen] = useState(false);
const [users, setUsers] = useState<User[]>([]);
const [selectedUserId, setSelectedUserId] = useState<number | null>(null);
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -35,6 +36,7 @@ export const App = () => {
setPosts([]);
setErrorMessage('');
setNotificationMessage('');
setActivePostId(null);

getPostsFromServer(selectedUserId)
.then(fetchPosts => {
Expand All @@ -59,6 +61,19 @@ export const App = () => {

return null;
}, [activePostId, posts]);

useEffect(() => {
const handleClickOutside = () => {
setIsDropDownOpen(false);
};

document.addEventListener('click', handleClickOutside);

return () => {
document.removeEventListener('click', handleClickOutside);
};
}, []);


return (
<main className="section">
Expand All @@ -68,9 +83,11 @@ export const App = () => {
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
isDropDownOpen={isDropDownOpen}
users={users}
setSelectedUserId={setSelectedUserId}
selectedUserId={selectedUserId}
setIsDropDownOpen={setIsDropDownOpen}
/>
</div>

Expand Down
4 changes: 3 additions & 1 deletion src/components/OneUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const OneUser: React.FC<Props> = ({
return (
<a
href={`#user-${user.id}`}
className={cn("dropdown-item", {'is-active': selectedUserId === user.id})}
className={cn('dropdown-item', {
'is-active': selectedUserId === user.id,
})}
onClick={handleUserSelect}
>
{user.name}
Expand Down
4 changes: 1 addition & 3 deletions src/components/PostsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export const PostsList: React.FC<Props> = ({
<div data-cy="PostsList">
<p className="title">Posts:</p>

<table
className='table is-striped is-hoverable is-narrow is-fullwidth'
>
<table className="table is-striped is-hoverable is-narrow is-fullwidth">
<thead>
<tr className="has-background-link-light">
<th>#</th>
Expand Down
8 changes: 6 additions & 2 deletions src/components/UserSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useState } from 'react';
import cn from 'classnames';

import { User } from '../types/User';
Expand All @@ -8,21 +7,26 @@ type Props = {
users: User[];
setSelectedUserId: (id: number | null) => void;
selectedUserId: number | null;
isDropDownOpen: boolean;
setIsDropDownOpen: (status: boolean) => void;
};

export const UserSelector: React.FC<Props> = ({
isDropDownOpen,
setIsDropDownOpen,
users,
setSelectedUserId,
selectedUserId,
}) => {
const [isDropDownOpen, setIsDropDownOpen] = useState(false);


const activeUser = users.find(user => user.id === selectedUserId);

return (
<div
data-cy="UserSelector"
className={cn('dropdown', { 'is-active': isDropDownOpen })}
onClick={(event) => event.stopPropagation()}
>
<div className="dropdown-trigger">
<button
Expand Down

0 comments on commit c5534fe

Please sign in to comment.