-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
problem #1210
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,87 @@ | ||
import classNames from 'classnames'; | ||
//import classNames from 'classnames'; | ||
|
||
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
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'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
@@ -53,8 +94,9 @@ export const App = () => ( | |
<div className="tile is-child box is-success "> | ||
<PostDetails /> | ||
</div> | ||
</div>*/} | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
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}`); | ||
}; |
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`); | ||
}; |
This file was deleted.
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> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './PostsList'; |
This file was deleted.
There was a problem hiding this comment.
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.