-
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
Solution #889
base: master
Are you sure you want to change the base?
Solution #889
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,9 +1 @@ | ||
import '@mate-academy/cypress-tools/support'; | ||
|
||
declare global { | ||
namespace Cypress { | ||
interface Chainable<Subject> { | ||
byDataCy(name: string, text: string): Chainable<JQuery>; | ||
} | ||
} | ||
} |
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); | ||
}, | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
import React from 'react'; | ||||||
import React, { useEffect, useState } from 'react'; | ||||||
import 'bulma/bulma.sass'; | ||||||
import '@fortawesome/fontawesome-free/css/all.css'; | ||||||
import './App.scss'; | ||||||
|
@@ -9,37 +9,84 @@ import { PostDetails } from './components/PostDetails'; | |||||
import { UserSelector } from './components/UserSelector'; | ||||||
import { Loader } from './components/Loader'; | ||||||
|
||||||
import { User } from './types/User'; | ||||||
import { Post } from './types/Post'; | ||||||
|
||||||
import { getPosts } from './api/ApiMethods'; | ||||||
|
||||||
export const App: React.FC = () => { | ||||||
const [selectUser, setSelectUser] = useState<User | null>(null); | ||||||
const [userPosts, setUserPosts] = useState<Post[]>([]); | ||||||
const [loading, setLoading] = useState(false); | ||||||
const [hasSelect, setHasSelect] = useState(false); | ||||||
const [hasError, setHasError] = useState(false); | ||||||
const [openPost, setOpenPost] = useState<Post | null>(null); | ||||||
const [openForm, setOpenForm] = useState(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.
Suggested change
fix everywhere: https://dev.to/michi/tips-on-naming-boolean-variables-cleaner-code-35ig |
||||||
useEffect(() => { | ||||||
if (selectUser) { | ||||||
setLoading(true); | ||||||
setHasError(false); | ||||||
|
||||||
getPosts(selectUser.id) | ||||||
.then(posts => setUserPosts(posts)) | ||||||
.catch(() => setHasError(true)) | ||||||
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.
Suggested change
we can simplify this logic |
||||||
.finally(() => { | ||||||
setLoading(false); | ||||||
setHasSelect(true); | ||||||
}); | ||||||
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. you may just check |
||||||
} | ||||||
}, [selectUser]); | ||||||
|
||||||
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 /> | ||||||
<UserSelector | ||||||
setSelectUser={setSelectUser} | ||||||
selectUser={selectUser} | ||||||
/> | ||||||
</div> | ||||||
|
||||||
<div className="block" data-cy="MainContent"> | ||||||
<p data-cy="NoSelectedUser"> | ||||||
No user selected | ||||||
</p> | ||||||
|
||||||
{loading ? ( | ||||||
<Loader /> | ||||||
) : ( | ||||||
<div className="block" data-cy="MainContent"> | ||||||
{!selectUser && ( | ||||||
<p data-cy="NoSelectedUser">No user selected</p> | ||||||
)} | ||||||
|
||||||
<div | ||||||
className="notification is-danger" | ||||||
data-cy="PostsLoadingError" | ||||||
> | ||||||
Something went wrong! | ||||||
</div> | ||||||
{hasError && ( | ||||||
<div | ||||||
className="notification is-danger" | ||||||
data-cy="PostsLoadingError" | ||||||
> | ||||||
Something went wrong! | ||||||
</div> | ||||||
)} | ||||||
|
||||||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||||||
No posts yet | ||||||
</div> | ||||||
{userPosts.length === 0 && hasSelect && ( | ||||||
<div | ||||||
className="notification is-warning" | ||||||
data-cy="NoPostsYet" | ||||||
> | ||||||
No posts yet | ||||||
</div> | ||||||
)} | ||||||
|
||||||
<PostsList /> | ||||||
</div> | ||||||
{userPosts.length !== 0 && ( | ||||||
<PostsList | ||||||
userPosts={userPosts} | ||||||
setOpenPost={setOpenPost} | ||||||
openPost={openPost} | ||||||
setOpenForm={setOpenForm} | ||||||
/> | ||||||
)} | ||||||
</div> | ||||||
)} | ||||||
</div> | ||||||
</div> | ||||||
|
||||||
|
@@ -50,11 +97,15 @@ export const App: React.FC = () => { | |||||
'is-parent', | ||||||
'is-8-desktop', | ||||||
'Sidebar', | ||||||
'Sidebar--open', | ||||||
{ 'Sidebar--open': openPost }, | ||||||
)} | ||||||
> | ||||||
<div className="tile is-child box is-success "> | ||||||
<PostDetails /> | ||||||
<PostDetails | ||||||
openPost={openPost} | ||||||
setOpenForm={setOpenForm} | ||||||
openForm={openForm} | ||||||
/> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { User } from '../types/User'; | ||
import { Post } from '../types/Post'; | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getUsers = () => { | ||
return client.get<User[]>('/users'); | ||
}; | ||
|
||
export const getPosts = (id: number) => { | ||
return client.get<Post[]>(`/posts?userId=${id}`); | ||
}; | ||
|
||
export const getComment = (id: number) => { | ||
return client.get<Comment[]>(`/comments?postId=${id}`); | ||
}; | ||
|
||
export const addComment = (data: Omit<Comment, 'id'>) => { | ||
return client.post<Comment>('/comments', data); | ||
}; | ||
|
||
export const deleteComment = (id: number) => { | ||
return client.delete(`/comments/${id}`); | ||
}; |
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.