Files
freeCodeCamp/client/src/components/layouts/Default.js

187 lines
4.4 KiB
JavaScript
Raw Normal View History

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';
import { createSelector } from 'reselect';
2018-03-26 13:01:24 +01:00
import Helmet from 'react-helmet';
import fontawesome from '@fortawesome/fontawesome';
2018-03-26 13:01:24 +01:00
import ga from '../../analytics';
import {
fetchUser,
isSignedInSelector,
onlineStatusChange,
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';
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';
import './night.css';
2018-03-26 13:01:24 +01:00
fontawesome.config = {
autoAddCss: false
};
const metaKeywords = [
'javascript',
'js',
'website',
'web',
'development',
'free',
'code',
'camp',
'course',
'courses',
'html',
'css',
'react',
'redux',
'api',
'front',
'back',
'end',
'learn',
'tutorial',
'programming'
];
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,
removeFlashMessage: PropTypes.func.isRequired,
2019-02-20 15:41:33 +03:00
showFooter: PropTypes.bool,
onGuide: PropTypes.bool
};
2018-06-29 19:00:05 +05:30
const mapStateToProps = createSelector(
isSignedInSelector,
flashMessagesSelector,
isOnlineSelector,
(isSignedIn, flashMessages, isOnline) => ({
isSignedIn,
flashMessages,
hasMessages: !!flashMessages.length,
isOnline
})
2018-06-29 19:00:05 +05:30
);
2018-05-24 19:45:38 +01:00
const mapDispatchToProps = dispatch =>
bindActionCreators(
{ fetchUser, removeFlashMessage, onlineStatusChange },
dispatch
);
2018-05-24 19:45:38 +01:00
class DefaultLayout extends Component {
constructor(props) {
super(props);
2018-05-24 19:45:38 +01:00
this.location = '';
}
componentDidMount() {
if (!this.props.isSignedIn) {
this.props.fetchUser();
}
const url = window.location.pathname + window.location.search;
ga.pageview(url);
window.addEventListener('online', this.updateOnlineStatus);
window.addEventListener('offline', this.updateOnlineStatus);
this.location = url;
}
componentDidUpdate() {
const url = window.location.pathname + window.location.search;
if (url !== this.location) {
ga.pageview(url);
this.location = url;
}
}
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;
};
render() {
const {
children,
disableSettings,
hasMessages,
flashMessages = [],
removeFlashMessage,
landingPage,
showFooter = true,
onGuide = false,
isOnline,
isSignedIn
} = this.props;
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(', ') }
]}
>
<style>{fontawesome.dom.css()}</style>
</Helmet>
2019-02-20 15:41:33 +03:00
<Header
disableSettings={disableSettings}
onGuide={onGuide}
2019-02-20 15:41:33 +03:00
/>
<div className={`default-layout ${landingPage ? 'landing-page' : ''}`}>
<OfflineWarning isOnline={isOnline} isSignedIn={isSignedIn} />
{hasMessages ? (
<Flash messages={flashMessages} onClose={removeFlashMessage} />
) : null}
{children}
</div>
{showFooter && <Footer />}
</Fragment>
);
}
}
2018-03-26 13:01:24 +01:00
DefaultLayout.displayName = 'DefaultLayout';
DefaultLayout.propTypes = propTypes;
2018-03-26 13:01:24 +01:00
export default connect(
mapStateToProps,
mapDispatchToProps
)(DefaultLayout);