Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-agerone committed Nov 12, 2024
1 parent 8b7ce12 commit be9298c
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 250 deletions.
14 changes: 14 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.tile {
&.is-ancestor {
display: flex;
}

&.is-parent {
display: flex;
flex-direction: column;
flex-grow: 1;
padding: 15px;
}
}

.Sidebar {
overflow: hidden;
opacity: 0;
Expand All @@ -21,3 +34,4 @@
.message-body {
white-space: pre-line;
}

156 changes: 115 additions & 41 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,134 @@
import classNames from 'classnames';

import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';

import { useEffect, useState } from 'react';
import { User } from './types/User';
import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { client } from './utils/fetchClient';
import { Post } from './types/Post';
import classNames from 'classnames';
import { PostDetails } from './components/PostDetails';

export const App = () => (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector />
</div>
export const App = () => {
const [users, setUsers] = useState<User[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [posts, setPosts] = useState<Post[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
const loadUsers = async () => {
setIsLoading(true);
setError('');
try {
const fetchedUsers = await client.get<User[]>('/users');

<Loader />
if (fetchedUsers.length === 0) {
throw new Error('No users found');
}

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
setUsers(fetchedUsers);
} catch (err) {
setError('Failed to load users');
} finally {
setIsLoading(false);
}
};

const loadUserPosts = async (userId: number) => {
setIsLoading(true);
setError('');
try {
const fetchedPosts = await client.get<Post[]>(`/posts?userId=${userId}`);

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
setPosts(fetchedPosts);
} catch (err) {
setError('Failed to load posts');
} finally {
setIsLoading(false);
}
};

useEffect(() => {
if (selectedUser) {
loadUserPosts(selectedUser.id);
setSelectedPost(null);
}
}, [selectedUser]);

useEffect(() => {
loadUsers();
}, []);

return (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
users={users}
selectedUser={selectedUser}
onSelectUser={setSelectedUser}
/>
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{!selectedUser && posts.length === 0 && !isLoading && (
<p data-cy="NoSelectedUser">No user selected</p>
)}
{isLoading && <Loader />}
{error && (
<div
data-cy="PostsLoadingError"
className="notification is-danger"
>
{error}
</div>
)}

{!isLoading && !!posts.length ? (
<PostsList
posts={posts}
openedPost={selectedPost}
onOpen={setSelectedPost}
/>
) : (
!isLoading &&
!error &&
posts.length === 0 &&
selectedUser && (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)
)}
</div>
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
{ 'Sidebar--open': selectedPost },
)}
>
<div className="tile is-child box is-success">
{selectedPost && <PostDetails post={selectedPost} />}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
Loading

0 comments on commit be9298c

Please sign in to comment.