Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Responsive navigation menu and loader HOC #169

Open
wants to merge 8 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
12,913 changes: 12,913 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"prop-types": "^15.5.10",
"react": "^16.3.0",
"react-dom": "^16.3.0",
"react-hamburger-menu": "^1.2.1",
"react-loader-spinner": "^3.1.14",
"react-redux": "^5.0.7",
"react-router": "^4.1.2",
"react-router-dom": "^4.1.2",
Expand Down
10 changes: 5 additions & 5 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import ArticlePreview from './ArticlePreview';
import ListPagination from './ListPagination';
import React from 'react';
import WithLoader from "./hoc/withLoader";


const ArticleList = props => {
if (!props.articles) {
return (
<div className="article-preview">Loading...</div>
);
return <div />;
}

if (props.articles.length === 0) {
Expand Down Expand Up @@ -35,4 +35,4 @@ const ArticleList = props => {
);
};

export default ArticleList;
export default WithLoader(ArticleList);
36 changes: 32 additions & 4 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { Link } from 'react-router-dom';
import HamburgerMenu from 'react-hamburger-menu';
import '../main.css';

const LoggedOutView = props => {
if (!props.currentUser) {
Expand Down Expand Up @@ -70,18 +72,44 @@ const LoggedInView = props => {
};

class Header extends React.Component {
state = {
open: false,
}

handleClick() {
this.setState({
open: !this.state.open,
});
}

render() {
const {open} = this.state;
const customNavAdditionalClass = open ? '' : 'd-none';

return (
<nav className="navbar navbar-light">
<div className="container">
<div className="container top-menu">
<HamburgerMenu
isOpen={this.state.open}
menuClicked={this.handleClick.bind(this)}
width={30}
height={18}
strokeWidth={1}
rotate={0}
color='black'
borderRadius={0}
animationDuration={0.5}
className="hamburger-menu"
/>

<Link to="/" className="navbar-brand">
{this.props.appName.toLowerCase()}
</Link>

<LoggedOutView currentUser={this.props.currentUser} />

<LoggedInView currentUser={this.props.currentUser} />
<div className={`custom-nav ${customNavAdditionalClass}`}>
<LoggedOutView currentUser={this.props.currentUser} />
<LoggedInView currentUser={this.props.currentUser} />
</div>
</div>
</nav>
);
Expand Down
17 changes: 11 additions & 6 deletions src/components/Home/MainView.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import agent from '../../agent';
import { connect } from 'react-redux';
import { CHANGE_TAB } from '../../constants/actionTypes';
import LoadingContext from "../../context/loadingContext";

const YourFeedTab = props => {
if (props.token) {
Expand Down Expand Up @@ -83,12 +84,16 @@ const MainView = props => {
</ul>
</div>

<ArticleList
pager={props.pager}
articles={props.articles}
loading={props.loading}
articlesCount={props.articlesCount}
currentPage={props.currentPage} />
<LoadingContext.Provider value={{
loading: !Boolean(props.articles)
}}>
<ArticleList
pager={props.pager}
articles={props.articles}
loading={props.loading}
articlesCount={props.articlesCount}
currentPage={props.currentPage} />
</LoadingContext.Provider>
</div>
);
};
Expand Down
23 changes: 23 additions & 0 deletions src/components/hoc/withLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { useContext } from 'react';
import Loader from "react-loader-spinner"
import LoadingContext from "../../context/loadingContext";
import "react-loader-spinner/dist/loader/css/react-spinner-loader.css"

const WithLoader = (WrappedComponent) => {
return (props) => {
const { loading } = useContext(LoadingContext);

if (loading) {
return <Loader
type="ThreeDots"
color="#5cb85c"
height={50}
width={50}
/>;
}

return <WrappedComponent {...props} loading={loading} />;
}
};

export default WithLoader;
6 changes: 6 additions & 0 deletions src/context/loadingContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';

const LoadingContext = React.createContext();
LoadingContext.displayName = 'LoadingContext';

export default LoadingContext;
55 changes: 55 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.top-menu {
display: flex;
justify-content: space-between;
align-items: center;
height: 100%;
overflow: hidden;
}

.d-none {
display: none;
}

@media (max-width: 767px){
.hamburger-menu {
display: block;
}

.custom-nav {
position: absolute;
top: 100%;
left: 1%;
width: 98%;
background: #f8f9fa;
z-index: 2;
}

.custom-nav ul {
width: 100%;
display: flex;
flex-direction: column;
}

.custom-nav ul li {
text-align: center;
width: 100%;
margin: 0 !important;
border-bottom: 1px solid #cfcdcd;
z-index: 2;
}

.custom-nav ul li:hover {
background: #ebebeb;
border-left: 2px solid #5cb85c;
}
}

@media (min-width: 768px){
.hamburger-menu {
display: none;
}

.custom-nav {
display: block;
}
}