Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem #1210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

problem #1210

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 71 additions & 29 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,87 @@
import classNames from 'classnames';
//import classNames from 'classnames';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The classnames import is commented out. If you plan to use dynamic class names, consider uncommenting this line.

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

import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
//import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PostDetails component is commented out. If this component is intended to be part of the application, consider uncommenting it and ensuring it is used correctly.

import { useEffect, useState } from 'react';
import { User } from './types/User';
import { getUsers } from './api/users';
import { Loader } from './components/Loader';
import { PostsList } from './components/PostsList/PostsList';
import { Post } from './types/Post';
import { getPosts } from './api/posts';

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 [posts, setPosts] = useState<Post[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [notification, setNotification] = useState('');
const [isLoading, setIsLoading] = useState(false);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
// #region load
useEffect(() => {
getUsers().then(setUsers);
}, []);

<Loader />
useEffect(() => {
if (selectedUser) {
setIsLoading(true);
setPosts([]);
setNotification('');

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
getPosts(selectedUser.id)
.then(res => {
if (res.length === 0) {
setNotification('is-warning');
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notification state is set to 'is-warning' when there are no posts. Ensure that this aligns with your application's design and user experience goals.


<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
setPosts(res);
})
.catch(() => setNotification('is-danger'))
.finally(() => setIsLoading(false));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notification state is set to 'is-danger' on error. Consider providing more detailed error messages to improve user experience.

}
}, [selectedUser]);
// #endregion

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} onSelect={setSelectedUser} />
</div>

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

{notification === 'is-danger' && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}

{notification === 'is-warning' && (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
)}
</div>
</div>
</div>
</div>

<div
{/*<div
data-cy="Sidebar"
className={classNames(
'tile',
Expand All @@ -53,8 +94,9 @@ export const App = () => (
<div className="tile is-child box is-success ">
<PostDetails />
</div>
</div>*/}
</div>
</div>
</div>
</main>
);
</main>
);
};
6 changes: 6 additions & 0 deletions src/api/posts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

export const getPosts = (userId: number) => {
return client.get<Post[]>(`/posts?userId=${userId}`);
};
6 changes: 6 additions & 0 deletions src/api/users.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

export const getUsers = () => {
return client.get<User[]>(`/users`);
};
86 changes: 0 additions & 86 deletions src/components/PostsList.tsx

This file was deleted.

59 changes: 59 additions & 0 deletions src/components/PostsList/PostsList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import classNames from 'classnames';
import React, { useState } from 'react';
import { Post } from '../../types/Post';

interface Props {
posts: Post[];
}

export const PostsList: React.FC<Props> = ({ posts }) => {
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

const handleOnClick = (post: Post) => {
setSelectedPost(prevPost =>
prevPost && prevPost.id === post.id ? null : post,
);
};

return (
<div data-cy="PostsList">
<p className="title">Posts:</p>

<table className="table is-fullwidth is-striped is-hoverable is-narrow">
<thead>
<tr className="has-background-link-light">
<th>#</th>
<th>Title</th>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<th> </th>
</tr>
</thead>

<tbody>
{posts.map(post => (
<tr data-cy="Post" key={post.id}>
<td data-cy="PostId">{post.id}</td>

<td data-cy="PostTitle">{post.title}</td>

<td className="has-text-right is-vcentered">
<button
type="button"
data-cy="PostButton"
className={classNames('button is-link', {
'is-light': !selectedPost || selectedPost.id !== post.id,
})}
onClick={() => handleOnClick(post)}
>
{!selectedPost || selectedPost.id !== post.id
? 'Open'
: 'Close'}
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
};
1 change: 1 addition & 0 deletions src/components/PostsList/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PostsList';
42 changes: 0 additions & 42 deletions src/components/UserSelector.tsx

This file was deleted.

Loading
Loading