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

test failed #1166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
161 changes: 124 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React, { useEffect, useState } from 'react';
import classNames from 'classnames';

import 'bulma/css/bulma.css';
Expand All @@ -9,52 +10,138 @@ import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';

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>
import { Post } from './types/Post';
import { User } from './types/User';

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
import { getUsers } from './api/users';
import { getPosts } from './api/posts';

<Loader />
export const App: React.FC = () => {
const [users, setUsers] = useState<User[]>([]);
const [posts, setPosts] = useState<Post[]>([]);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
const [isLoadingUsers, setIsLoadingUsers] = useState(false);
const [isLoadingPosts, setIsLoadingPosts] = useState(false);

const [usersError, setUsersError] = useState('');
const [postsError, setPostsError] = useState('');

const [selectedUserId, setSelectedUserId] = useState<number | null>(null);
const [selectedPostId, setSelectedPostId] = useState<number | null>(null);

const selectedPost = posts.find(post => post.id === selectedPostId);

useEffect(() => {
const loadUsers = async () => {
setIsLoadingUsers(true);
setUsersError('');

try {
const loadedUsers = await getUsers();

setUsers(loadedUsers);
} catch {
setUsersError('Somthing went wrong');
} finally {
setIsLoadingUsers(false);
}
};

loadUsers();
}, []);

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
useEffect(() => {
if (!selectedUserId) {
return;
}

const loadPosts = async () => {
setPosts([]);
setIsLoadingPosts(true);
setPostsError('');

try {
const loadedPost = await getPosts(selectedUserId);

setPosts(loadedPost);
} catch {
setPostsError('Failed to load posts');
} finally {
setIsLoadingPosts(false);
}
};

loadPosts();
}, [selectedUserId]);

return (
<main className="section">
<div className="container">
<div className="tile is-ancestor columns">
<div className="tile is-parent column">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
users={users}
selectedUserId={selectedUserId}
setSelectedUserId={setSelectedUserId}
/>
{isLoadingUsers && <Loader />}
{usersError && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
{usersError}
</div>
)}
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{!selectedUserId ? (
<p data-cy="NoSelectedUser">No user selected</p>
) : isLoadingPosts ? (
<Loader />
) : postsError ? (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
{postsError}
</div>
) : posts.length === 0 ? (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
) : (
<PostsList
posts={posts}
selectedPostId={selectedPostId}
setSelectedPostId={setSelectedPostId}
/>
)}
</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 },
)}
>
{selectedPost && (
<div className="tile is-child box is-success ">
<PostDetails post={selectedPost} />
</div>
)}
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
14 changes: 14 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

export const getComment = (postId: number) => {
return client.get<Comment[]>(`/comments?postId=${postId}`);
};

export const deleteComment = (commentId: number) => {
return client.delete(`/comments/${commentId}`);
};

export const addComment = (newComment: Comment) => {
return client.post(`/comments`, newComment);
};
6 changes: 6 additions & 0 deletions src/api/posts.ts
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.ts
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');
};
Loading
Loading