2018-07-26 14:37:10 +01:00
|
|
|
import React, { Fragment, Component } from 'react';
|
2018-03-26 13:01:24 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-05-24 19:45:38 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
2018-09-30 11:37:19 +01:00
|
|
|
import { createSelector } from 'reselect';
|
2018-03-26 13:01:24 +01:00
|
|
|
import Helmet from 'react-helmet';
|
2019-01-24 20:33:13 +03:00
|
|
|
import fontawesome from '@fortawesome/fontawesome';
|
2018-03-26 13:01:24 +01:00
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
import ga from '../../analytics';
|
2018-07-26 14:37:10 +01:00
|
|
|
import {
|
|
|
|
fetchUser,
|
2018-09-30 11:37:19 +01:00
|
|
|
isSignedInSelector,
|
2018-07-26 14:37:10 +01:00
|
|
|
onlineStatusChange,
|
2018-09-30 11:37:19 +01:00
|
|
|
isOnlineSelector
|
|
|
|
} from '../../redux';
|
|
|
|
import { flashMessagesSelector, removeFlashMessage } from '../Flash/redux';
|
|
|
|
|
|
|
|
import { isBrowser } from '../../../utils';
|
|
|
|
|
|
|
|
import OfflineWarning from '../OfflineWarning';
|
|
|
|
import Flash from '../Flash';
|
|
|
|
import Header from '../Header';
|
2019-01-14 16:28:00 +03:00
|
|
|
import Footer from '../Footer';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
2018-05-30 10:20:58 +01:00
|
|
|
import './global.css';
|
2018-04-06 14:51:52 +01:00
|
|
|
import './layout.css';
|
2018-09-30 11:37:19 +01:00
|
|
|
import './night.css';
|
2018-03-26 13:01:24 +01:00
|
|
|
|
2019-01-24 20:33:13 +03:00
|
|
|
fontawesome.config = {
|
|
|
|
autoAddCss: false
|
|
|
|
};
|
|
|
|
|
2018-05-18 19:07:32 +01:00
|
|
|
const metaKeywords = [
|
|
|
|
'javascript',
|
|
|
|
'js',
|
|
|
|
'website',
|
|
|
|
'web',
|
|
|
|
'development',
|
|
|
|
'free',
|
|
|
|
'code',
|
|
|
|
'camp',
|
|
|
|
'course',
|
|
|
|
'courses',
|
|
|
|
'html',
|
|
|
|
'css',
|
|
|
|
'react',
|
|
|
|
'redux',
|
|
|
|
'api',
|
|
|
|
'front',
|
|
|
|
'back',
|
|
|
|
'end',
|
|
|
|
'learn',
|
|
|
|
'tutorial',
|
|
|
|
'programming'
|
|
|
|
];
|
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
const propTypes = {
|
|
|
|
children: PropTypes.node.isRequired,
|
|
|
|
disableSettings: PropTypes.bool,
|
|
|
|
fetchUser: PropTypes.func.isRequired,
|
|
|
|
flashMessages: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
|
|
|
message: PropTypes.string
|
|
|
|
})
|
|
|
|
),
|
|
|
|
hasMessages: PropTypes.bool,
|
|
|
|
isOnline: PropTypes.bool.isRequired,
|
|
|
|
isSignedIn: PropTypes.bool,
|
|
|
|
landingPage: PropTypes.bool,
|
|
|
|
onlineStatusChange: PropTypes.func.isRequired,
|
2019-02-17 14:47:20 +03:00
|
|
|
removeFlashMessage: PropTypes.func.isRequired,
|
2019-02-20 15:41:33 +03:00
|
|
|
showFooter: PropTypes.bool,
|
2019-02-21 14:07:01 +03:00
|
|
|
onGuide: PropTypes.bool
|
2018-09-30 11:37:19 +01:00
|
|
|
};
|
|
|
|
|
2018-06-29 19:00:05 +05:30
|
|
|
const mapStateToProps = createSelector(
|
2018-07-26 14:37:10 +01:00
|
|
|
isSignedInSelector,
|
2018-09-30 11:37:19 +01:00
|
|
|
flashMessagesSelector,
|
2018-07-26 14:37:10 +01:00
|
|
|
isOnlineSelector,
|
2018-09-30 11:37:19 +01:00
|
|
|
(isSignedIn, flashMessages, isOnline) => ({
|
2018-07-26 14:37:10 +01:00
|
|
|
isSignedIn,
|
2018-09-30 11:37:19 +01:00
|
|
|
flashMessages,
|
|
|
|
hasMessages: !!flashMessages.length,
|
2018-07-26 14:37:10 +01:00
|
|
|
isOnline
|
|
|
|
})
|
2018-06-29 19:00:05 +05:30
|
|
|
);
|
2018-05-24 19:45:38 +01:00
|
|
|
const mapDispatchToProps = dispatch =>
|
2018-09-30 11:37:19 +01:00
|
|
|
bindActionCreators(
|
|
|
|
{ fetchUser, removeFlashMessage, onlineStatusChange },
|
|
|
|
dispatch
|
|
|
|
);
|
2018-05-24 19:45:38 +01:00
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
class DefaultLayout extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-05-24 19:45:38 +01:00
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
this.location = '';
|
|
|
|
}
|
2018-07-26 14:37:10 +01:00
|
|
|
|
2018-05-18 19:07:32 +01:00
|
|
|
componentDidMount() {
|
2018-09-30 11:37:19 +01:00
|
|
|
if (!this.props.isSignedIn) {
|
|
|
|
this.props.fetchUser();
|
|
|
|
}
|
2018-05-18 19:07:32 +01:00
|
|
|
const url = window.location.pathname + window.location.search;
|
|
|
|
ga.pageview(url);
|
2018-07-26 14:37:10 +01:00
|
|
|
|
|
|
|
window.addEventListener('online', this.updateOnlineStatus);
|
|
|
|
window.addEventListener('offline', this.updateOnlineStatus);
|
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
this.location = url;
|
2018-05-18 19:07:32 +01:00
|
|
|
}
|
2018-07-26 14:37:10 +01:00
|
|
|
|
2018-05-18 19:07:32 +01:00
|
|
|
componentDidUpdate() {
|
|
|
|
const url = window.location.pathname + window.location.search;
|
2018-09-30 11:37:19 +01:00
|
|
|
if (url !== this.location) {
|
2018-05-18 19:07:32 +01:00
|
|
|
ga.pageview(url);
|
2018-09-30 11:37:19 +01:00
|
|
|
this.location = url;
|
2018-05-18 19:07:32 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-26 14:37:10 +01:00
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('online', this.updateOnlineStatus);
|
|
|
|
window.removeEventListener('offline', this.updateOnlineStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOnlineStatus = () => {
|
|
|
|
const { onlineStatusChange } = this.props;
|
|
|
|
const isOnline =
|
|
|
|
isBrowser() && 'navigator' in window ? window.navigator.onLine : null;
|
|
|
|
return typeof isOnline === 'boolean' ? onlineStatusChange(isOnline) : null;
|
|
|
|
};
|
|
|
|
|
2018-05-18 19:07:32 +01:00
|
|
|
render() {
|
2018-09-30 11:37:19 +01:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
disableSettings,
|
|
|
|
hasMessages,
|
|
|
|
flashMessages = [],
|
|
|
|
removeFlashMessage,
|
|
|
|
landingPage,
|
2019-02-17 14:47:20 +03:00
|
|
|
showFooter = true,
|
2019-02-21 14:07:01 +03:00
|
|
|
onGuide = false,
|
2018-09-30 11:37:19 +01:00
|
|
|
isOnline,
|
|
|
|
isSignedIn
|
|
|
|
} = this.props;
|
2018-05-18 19:07:32 +01:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Helmet
|
|
|
|
meta={[
|
|
|
|
{
|
|
|
|
name: 'description',
|
|
|
|
content:
|
|
|
|
'Learn to code with free online courses, programming ' +
|
|
|
|
'projects, and interview preparation for developer jobs.'
|
|
|
|
},
|
|
|
|
{ name: 'keywords', content: metaKeywords.join(', ') }
|
|
|
|
]}
|
2019-02-19 01:59:12 +03:00
|
|
|
>
|
2019-01-24 20:33:13 +03:00
|
|
|
<style>{fontawesome.dom.css()}</style>
|
|
|
|
</Helmet>
|
2019-02-20 15:41:33 +03:00
|
|
|
<Header
|
|
|
|
disableSettings={disableSettings}
|
2019-02-21 14:07:01 +03:00
|
|
|
onGuide={onGuide}
|
2019-02-20 15:41:33 +03:00
|
|
|
/>
|
2018-10-24 00:24:48 +01:00
|
|
|
<div className={`default-layout ${landingPage ? 'landing-page' : ''}`}>
|
2018-07-26 14:37:10 +01:00
|
|
|
<OfflineWarning isOnline={isOnline} isSignedIn={isSignedIn} />
|
2018-09-30 11:37:19 +01:00
|
|
|
{hasMessages ? (
|
|
|
|
<Flash messages={flashMessages} onClose={removeFlashMessage} />
|
|
|
|
) : null}
|
|
|
|
{children}
|
2018-10-04 14:47:55 +01:00
|
|
|
</div>
|
2019-02-18 19:32:49 +00:00
|
|
|
{showFooter && <Footer />}
|
2018-05-18 19:07:32 +01:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 13:01:24 +01:00
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
DefaultLayout.displayName = 'DefaultLayout';
|
|
|
|
DefaultLayout.propTypes = propTypes;
|
2018-03-26 13:01:24 +01:00
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(DefaultLayout);
|