Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kyemets committed Jun 26, 2023
1 parent 7cd93f5 commit 68f360d
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 96 deletions.
7 changes: 3 additions & 4 deletions src/API/PostService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios'

export default class PostService {

static async getAll(limit = 10, page = 1) {
const response = await axios(`https://jsonplaceholder.typicode.com/posts`, {
params: {
Expand All @@ -12,14 +13,12 @@ export default class PostService {
}

static async getById(id) {
const response = await axios(`https://jsonplaceholder.typicode.com/posts/${id}`
)
const response = await axios(`https://jsonplaceholder.typicode.com/posts/${id}`)
return response;
}

static async getCommentsByPostId(id) {
const response = await axios(`https://jsonplaceholder.typicode.com/posts/${id}/comments`
)
const response = await axios(`https://jsonplaceholder.typicode.com/posts/${id}/comments`)
return response;
}
}
2 changes: 1 addition & 1 deletion src/components/PostItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const PostItem = (props) => {
<div className="post__btns">
<MyButton onClick={() => props.remove(props.post)}>Delete</MyButton>
</div>
</div>
</div>
)
}

Expand Down
29 changes: 18 additions & 11 deletions src/components/UI/modal/MyModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import classes from './MyModal.module.css';

const MyModal = ({children, visible, setVisible}) => {

const rootClasses = [classes.myModal];
const rootClasses = [classes.myModal];

if (visible) {
rootClasses.push(classes.active);
}
if (visible) {
rootClasses.push(classes.active);
}

return (
<div className={rootClasses.join(' ')} onClick={() => setVisible(false)}>
<div className={classes.myModalContent} onClick={(e) => e.stopPropagation()}>
{children}
</div>
</div>
)

return (
<div
className={rootClasses.join(' ')}
onClick={() => setVisible(false)}
>
<div
className={classes.myModalContent}
onClick={(e) => e.stopPropagation()}
>
{children}
</div>
</div>
)
}

export default MyModal;
29 changes: 15 additions & 14 deletions src/components/UI/navbar/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import React, {useContext} from 'react';
import '../../../styles/App.css';
import React, {useContext} from 'react';
import {Link} from 'react-router-dom'
import MyButton from '../button/MyButton';
import {AuthContext} from '../../../context/index';


const Navbar = () => {
const {isAuth, setIsAuth} = useContext(AuthContext);
const {setIsAuth} = useContext(AuthContext);

const logout = () => {
setIsAuth(false);
localStorage.removeItem('auth');
}


return (
<div className="navbar">
<MyButton
onClick={logout}
>
Logout
</MyButton>
<div className='navbar__links'>
<Link to='/about'>About app</Link>
<Link to='/posts'>Posts</Link>
</div>
return (
<div className="navbar">
<MyButton
onClick={logout}
>
Logout
</MyButton>
<div className='navbar__links'>
<Link to='/about'>About app</Link>
<Link to='/posts'>Posts</Link>
</div>
)
</div>
)
}

export default Navbar;
1 change: 0 additions & 1 deletion src/context/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createContext } from'react';


export const AuthContext = createContext(null)
3 changes: 1 addition & 2 deletions src/hooks/useObserver.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export const useObserver = (ref, canLoad, isLoading, callback) => {
if (isLoading) return;
if (observer.current) observer.current.disconnect();

var cb = function(entries, observer) {
var cb = function(entries) {
if (entries[0].isIntersecting && canLoad) {
callback()
}
}
observer.current = new IntersectionObserver(cb);
observer.current.observe(ref.current);
}, [isLoading])

}
14 changes: 7 additions & 7 deletions src/hooks/usePosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {useMemo} from 'react';

export const useSortedPosts = (posts, sort) => {

const sortedPosts = useMemo(() => {
if (sort) {
return [...posts].sort((a, b) => a[sort].localeCompare(b[sort]))
}
return posts;
}, [sort, posts])
const sortedPosts = useMemo(() => {
if (sort) {
return [...posts].sort((a, b) => a[sort].localeCompare(b[sort]))
}
return posts;
}, [sort, posts])

return sortedPosts;
return sortedPosts;
}

export const usePosts = (posts, sort, query) => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Error.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';

const Error = () => {


const Error = () => {

return (
<div>
<h1 style={{color: 'red'}}>The page does not exist!</h1>
Expand Down
17 changes: 14 additions & 3 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {useContext} from 'react';
import {AuthContext} from '../context/index'
import MyInput from '../components/UI/input/MyInput';
import MyButton from '../components/UI/button/MyButton';
import {AuthContext} from '../context/index'


const Login = () => {
const {setIsAuth} = useContext(AuthContext);
Expand All @@ -16,8 +17,18 @@ const Login = () => {
<div>
<h1>Login page</h1>
<form onSubmit={login}>
<MyInput type="text" name="username" placeholder="Login"/>
<MyInput type="password" name="password" placeholder="Password"/>
<MyInput
type="text"
name="username"
value="root"
placeholder="Login"
/>
<MyInput
type="password"
name="password"
value="root"
placeholder="Password"
/>
<MyButton>Login</MyButton>
</form>
</div>
Expand Down
7 changes: 3 additions & 4 deletions src/pages/PostIdPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const PostIdPage = () => {
const [post, setPost] = useState({});
const [comments, setComments] = useState([]);

const [fetchPostById, isLoading, error] = useFetching( async(id) => {
const [fetchPostById, isLoading] = useFetching( async(id) => {
const response = await PostService.getById(id);
setPost(response.data);
})

const [fetchComments, isComLoading, comError] = useFetching( async(id) => {
const [fetchComments, isComLoading] = useFetching( async(id) => {
const response = await PostService.getCommentsByPostId(id);
setComments(response.data);
})
Expand All @@ -38,11 +38,10 @@ const PostIdPage = () => {
? <Loader />
: <div>
{comments.map(comm =>
<div key={comm.id} style={{marginTop: "15px"}}>
<div key={comm.id} style={{marginTop: "15px"}}>
<h5>{comm.email}</h5>
<div>{comm.body}</div>
</div>

)}
</div>
}
Expand Down
99 changes: 51 additions & 48 deletions src/pages/Posts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import MyModal from '.././components/UI/modal/MyModal';
import MyButton from '../components/UI/button/MyButton';
import Loader from '../components/UI/loader/Loader';
import MyPagination from '../components/UI/pagination/Pagination';
import { usePosts } from '../hooks/usePosts';
import MySelect from '../components/UI/select/MySelect';
import PostService from '../API/PostService';
import { useFetching } from '../hooks/useFetching';
import { getPageCount } from '../utils/pages';
import { usePosts } from '../hooks/usePosts';
import { useFetching } from '../hooks/useFetching';
import { useObserver } from '../hooks/useObserver';
import MySelect from '../components/UI/select/MySelect';


function Posts () {
Expand Down Expand Up @@ -55,52 +55,55 @@ function Posts () {

return (
<div className="App">
<MyButton
style={{margin: '15px 0'}}
onClick={() => setModal(true)}
>
Create post
</MyButton>
<MyModal
visible={modal}
setVisible={setModal}
>
<PostForm create={createPost} />
</MyModal>
<hr style={{margin: '15px 0'}}/>
<PostFilter
filter={filter}
setFilter={setFilter}
/>
<MySelect
value={limit}
onChange={value => setLimit(value)}
defaultValue="Number of posts per page"
options={[
{value: 5, name: '5'},
{value: 10, name: '10'},
{value: 25, name: '25'},
{value: -1, name: 'all'},

]}
/>
{postError &&
<h1>Error {postError}</h1>
}
<PostList posts={sortedAndSearchedPosts} remove={removePost} title="List of JavaScript" />
<div ref={lastElement} style={{height: 20, background: 'red'}} />
<MyButton
style={{margin: '15px 0'}}
onClick={() => setModal(true)}
>
Create post
</MyButton>
<MyModal
visible={modal}
setVisible={setModal}
>
<PostForm create={createPost} />
</MyModal>

<hr style={{margin: '15px 0'}}/>

<PostFilter
filter={filter}
setFilter={setFilter}
/>
<MySelect
value={limit}
onChange={value => setLimit(value)}
defaultValue="Number of posts per page"
options={[
{value: 5, name: '5'},
{value: 10, name: '10'},
{value: 25, name: '25'},
{value: -1, name: 'all'},
]}
/>
{postError &&
<h1>Error {postError}</h1>
}
<PostList posts={sortedAndSearchedPosts} remove={removePost} title="List of mockdata" />
<div ref={lastElement} style={{height: 20, background: 'red'}} />

{isPostsLoading &&
<div style={{display: 'flex', justifyContent: 'center', marginTop: '50px'}}><Loader /></div>
}

<MyPagination
page={page}
changePage={changePage}
totalPages={totalPages}
/>
{isPostsLoading &&
<div style={{display: 'flex', justifyContent: 'center', marginTop: '50px'}}>
<Loader />
</div>
}

<MyPagination
page={page}
changePage={changePage}
totalPages={totalPages}
/>
</div>
);
);
}

export default Posts;
export default Posts;

0 comments on commit 68f360d

Please sign in to comment.