Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaSheva1987 committed Jul 19, 2023
1 parent 943dfce commit fb9503c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 71 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ form to add new comments.
1. Implement comment deletion
- Delete the commnet immediately not waiting for the server response to improve the UX.
1. (*) Handle `Add` and `Delete` errors so the user can retry

[DEMO LINK](https://RomaSheva1987.github.io/react_dynamic-list-of-posts/)
1 change: 0 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"baseUrl": "http://localhost:3000",
"defaultCommandTimeout": 2000,
"video": true,
"viewportHeight": 1920,
"viewportWidth": 1080,
Expand Down
8 changes: 0 additions & 8 deletions cypress/support/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
import '@mate-academy/cypress-tools/support';

declare global {
namespace Cypress {
interface Chainable<Subject> {
byDataCy(name: string, text: string): Chainable<JQuery>;
}
}
}
18 changes: 0 additions & 18 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
require('@mate-academy/cypress-tools/support');

Cypress.Commands.add(
'byDataCy',
{ prevSubject: 'optional' },

(subject, name, text = '') => {
const target = subject || cy;
const selector = `[data-cy="${name}"]`;

if (text) {
return target.contain(selector, text);
}

return subject
? subject.find(selector)
: cy.get(selector);
},
);
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@cypress/webpack-dev-server": "^1.8.4",
"@mate-academy/cypress-tools": "^1.0.5",
"@mate-academy/eslint-config-react-typescript": "^1.0.5",
"@mate-academy/scripts": "^1.2.3",
"@mate-academy/scripts": "^1.2.8",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^17.0.23",
Expand Down
55 changes: 25 additions & 30 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import { Comment } from './types/Comment';
import { ErrorMessage } from './types/ErrorMessage';

export const App: React.FC = () => {
const [usersFromServer, setUseesFromServer] = useState<User[]>([]);
const [users, setUsers] = useState<User[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [isUserLoader, setIsUserLoad] = useState(true);
const [isUserLoading, setIsUserLoading] = useState(true);

const [postsFromServer, setPostsFromServer] = useState<Post[] | null>(null);
const [posts, setPosts] = useState<Post[] | null>(null);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

const [comments, setCcomments] = useState<Comment[]>([]);
const [isCommentsLoader, setIsCommentsLoad] = useState(false);
const [isCommentsLoading, setIsCommentsLoading] = useState(false);

const [isFormVisible, setIsFormVisible] = useState(false);
const [apiErrors, setApiErrors] = useState({
Expand Down Expand Up @@ -88,36 +88,36 @@ export const App: React.FC = () => {

const loadUsers = async () => {
try {
const users = await getUsers();
const usersFromServer = await getUsers();

setUseesFromServer(users);
setUsers(usersFromServer);
} catch {
setApiErrors((prevState) => ({ ...prevState, userError: true }));
}
};

const loadPosts = async () => {
if (selectedUser) {
setIsUserLoad(true);
setIsUserLoading(true);

setApiErrors((prevState) => ({ ...prevState, postError: false }));
try {
const posts = await getPosts(selectedUser.id);
const postsFromServer = await getPosts(selectedUser.id);

setPostsFromServer(posts);
setPosts(postsFromServer);
} catch {
setApiErrors((prevState) => ({ ...prevState, postError: true }));
} finally {
setIsUserLoad(false);
setIsUserLoading(false);
}
}
};

const LoadComments = async () => {
const loadComments = async () => {
if (selectedPost) {
setApiErrors((prevState) => ({ ...prevState, commentsError: false }));

setIsCommentsLoad(true);
setIsCommentsLoading(true);
setCcomments([]);

try {
Expand All @@ -127,7 +127,7 @@ export const App: React.FC = () => {
} catch {
setApiErrors((prevState) => ({ ...prevState, commentsError: true }));
} finally {
setIsCommentsLoad(false);
setIsCommentsLoading(false);
}
}
};
Expand All @@ -141,7 +141,7 @@ export const App: React.FC = () => {
}, [selectedUser]);

useEffect(() => {
LoadComments();
loadComments();
}, [selectedPost]);

return (
Expand All @@ -152,7 +152,7 @@ export const App: React.FC = () => {
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
users={usersFromServer}
users={users}
selectedUser={selectedUser}
onUserSelect={handleSelectUser}
/>
Expand All @@ -176,7 +176,7 @@ export const App: React.FC = () => {

{selectedUser && (
<>
{isUserLoader && (
{isUserLoading && (
<Loader />
)}

Expand All @@ -189,9 +189,7 @@ export const App: React.FC = () => {
</div>
) : (
<>
{!isUserLoader
&& postsFromServer
&& !postsFromServer.length
{!isUserLoading && !posts?.length
&& (
<div
className="notification is-warning"
Expand All @@ -200,16 +198,13 @@ export const App: React.FC = () => {
No posts yet
</div>
)}
{!isUserLoader
&& postsFromServer
&& postsFromServer.length > 0
&& (
<PostsList
posts={postsFromServer}
selectedPost={selectedPost}
onPostSelect={handleSelectPost}
/>
)}
{!isUserLoading && !!posts?.length && (
<PostsList
posts={posts}
selectedPost={selectedPost}
onPostSelect={handleSelectPost}
/>
)}
</>
)}
</>
Expand All @@ -235,7 +230,7 @@ export const App: React.FC = () => {
selectedPost={selectedPost}
comments={comments}
isCommentsError={commentsError}
isLoader={isCommentsLoader}
isLoading={isCommentsLoading}
isFormVisible={isFormVisible}
isDeleteError={deleteCommentError}
onDelete={removeComment}
Expand Down
8 changes: 4 additions & 4 deletions src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const NewCommentForm: React.FC<Props> = ({ selectedPost, onAdd }) => {
};

const [comment, setComment] = useState(initComment);
const [isButtonLoad, setIsButtonLoad] = useState(false);
const [isButtonLoading, setIsButtonLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [validationErrors, setValidationErrors] = useState({
isName: true,
Expand Down Expand Up @@ -73,7 +73,7 @@ export const NewCommentForm: React.FC<Props> = ({ selectedPost, onAdd }) => {

try {
if (isFormValid) {
setIsButtonLoad(true);
setIsButtonLoading(true);
const newComment = await addComment(comment);

onAdd(newComment);
Expand All @@ -82,7 +82,7 @@ export const NewCommentForm: React.FC<Props> = ({ selectedPost, onAdd }) => {
} catch {
setIsError(true);
} finally {
setIsButtonLoad(false);
setIsButtonLoading(false);
}
};

Expand Down Expand Up @@ -231,7 +231,7 @@ export const NewCommentForm: React.FC<Props> = ({ selectedPost, onAdd }) => {
type="submit"
className={classNames(
'button is-link', {
'is-loading': isButtonLoad,
'is-loading': isButtonLoading,
},
)}
>
Expand Down
12 changes: 6 additions & 6 deletions src/components/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Props = {
selectedPost: Post | null;
comments: Comment[];
isCommentsError: boolean;
isLoader: boolean;
isLoading: boolean;
isFormVisible: boolean;
isDeleteError: boolean;
onDelete: (commentId: number) => void;
Expand All @@ -21,7 +21,7 @@ export const PostDetails: React.FC<Props> = ({
selectedPost,
comments,
isCommentsError,
isLoader,
isLoading,
isFormVisible,
isDeleteError,
onDelete,
Expand All @@ -44,7 +44,7 @@ export const PostDetails: React.FC<Props> = ({
</div>

<div className="block">
{isLoader && (
{isLoading && (
<Loader />
)}

Expand All @@ -54,13 +54,13 @@ export const PostDetails: React.FC<Props> = ({
</div>
) : (
<>
{(!comments.length && !isLoader) && (
{(!comments.length && !isLoading) && (
<p className="title is-4" data-cy="NoCommentsMessage">
No comments yet
</p>
)}

{(comments.length > 0 && !isLoader) && (
{(comments.length > 0 && !isLoading) && (
<p className="title is-4">Comments:</p>
)}

Expand Down Expand Up @@ -103,7 +103,7 @@ export const PostDetails: React.FC<Props> = ({
</div>
)}

{(!isLoader && !isFormVisible) && (
{(!isLoading && !isFormVisible) && (
<button
data-cy="WriteCommentButton"
type="button"
Expand Down

0 comments on commit fb9503c

Please sign in to comment.