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

233 lines
6.0 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,
2019-08-19 18:54:04 +03:00
isOnlineSelector,
userSelector
} from '../../redux';
import { flashMessageSelector, removeFlashMessage } from '../Flash/redux';
import { isBrowser } from '../../../utils';
2019-02-28 15:49:23 +00:00
import WithInstantSearch from '../search/WithInstantSearch';
import OfflineWarning from '../OfflineWarning';
import Flash from '../Flash';
import Header from '../Header';
import Footer from '../Footer';
// preload common fonts
import latoLightURL from '../../../static/fonts/lato/Lato-Light.woff';
import latoRegularURL from '../../../static/fonts/lato/Lato-Regular.woff';
import latoBoldURL from '../../../static/fonts/lato/Lato-Bold.woff';
// eslint-disable-next-line max-len
import robotoRegularURL from '../../../static/fonts/roboto-mono/RobotoMono-Regular.woff';
// eslint-disable-next-line max-len
import robotoBoldURL from '../../../static/fonts/roboto-mono/RobotoMono-Bold.woff';
// eslint-disable-next-line max-len
import robotoItalicURL from '../../../static/fonts/roboto-mono/RobotoMono-Italic.woff';
2018-04-06 14:51:52 +01:00
2019-10-16 13:02:26 +03:00
import './fonts.css';
2018-05-30 10:20:58 +01:00
import './global.css';
2019-08-19 18:54:04 +03:00
import './variables.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,
fetchUser: PropTypes.func.isRequired,
flashMessage: PropTypes.shape({
id: PropTypes.string,
type: PropTypes.string,
message: PropTypes.string
}),
hasMessage: PropTypes.bool,
isOnline: PropTypes.bool.isRequired,
isSignedIn: PropTypes.bool,
onlineStatusChange: PropTypes.func.isRequired,
2019-02-28 15:49:23 +00:00
pathname: PropTypes.string.isRequired,
removeFlashMessage: PropTypes.func.isRequired,
2019-08-19 18:54:04 +03:00
showFooter: PropTypes.bool,
theme: PropTypes.string
};
2018-06-29 19:00:05 +05:30
const mapStateToProps = createSelector(
isSignedInSelector,
flashMessageSelector,
isOnlineSelector,
2019-08-19 18:54:04 +03:00
userSelector,
(isSignedIn, flashMessage, isOnline, user) => ({
isSignedIn,
flashMessage,
hasMessage: !!flashMessage.message,
2019-08-19 18:54:04 +03:00
isOnline,
theme: user.theme
})
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 {
componentDidMount() {
2019-02-28 15:49:23 +00:00
const { isSignedIn, fetchUser, pathname } = this.props;
if (!isSignedIn) {
fetchUser();
}
2019-02-28 15:49:23 +00:00
ga.pageview(pathname);
window.addEventListener('online', this.updateOnlineStatus);
window.addEventListener('offline', this.updateOnlineStatus);
}
2019-02-28 15:49:23 +00:00
componentDidUpdate(prevProps) {
const { pathname } = this.props;
const { pathname: prevPathname } = prevProps;
if (pathname !== prevPathname) {
ga.pageview(pathname);
}
}
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,
hasMessage,
flashMessage,
2019-02-28 15:49:23 +00:00
isOnline,
isSignedIn,
removeFlashMessage,
2019-08-19 18:54:04 +03:00
showFooter = true,
2019-10-07 23:05:31 +03:00
theme = 'default'
} = this.props;
return (
<Fragment>
<Helmet
2019-08-19 18:54:04 +03:00
bodyAttributes={{
class: `${theme === 'default' ? 'light-palette' : 'dark-palette'}`
}}
meta={[
{
name: 'description',
content:
'Learn to code with free online courses, programming ' +
'projects, and interview preparation for developer jobs.'
},
{ name: 'keywords', content: metaKeywords.join(', ') }
]}
>
<link
as='font'
crossOrigin='anonymous'
href={latoRegularURL}
rel='preload'
type='font/woff'
/>
<link
as='font'
crossOrigin='anonymous'
href={latoLightURL}
rel='preload'
type='font/woff'
/>
<link
as='font'
crossOrigin='anonymous'
href={latoBoldURL}
rel='preload'
type='font/woff'
/>
<link
as='font'
crossOrigin='anonymous'
href={robotoRegularURL}
rel='preload'
type='font/woff'
/>
<link
as='font'
crossOrigin='anonymous'
href={robotoBoldURL}
rel='preload'
type='font/woff'
/>
<link
as='font'
crossOrigin='anonymous'
href={robotoItalicURL}
rel='preload'
type='font/woff'
/>
<style>{fontawesome.dom.css()}</style>
</Helmet>
<WithInstantSearch>
<Header />
2019-09-01 17:20:53 +03:00
<div className={`default-layout`}>
2019-02-28 15:49:23 +00:00
<OfflineWarning isOnline={isOnline} isSignedIn={isSignedIn} />
{hasMessage && flashMessage ? (
<Flash flashMessage={flashMessage} onClose={removeFlashMessage} />
2019-02-28 15:49:23 +00:00
) : null}
{children}
{showFooter && <Footer />}
2019-02-28 15:49:23 +00:00
</div>
</WithInstantSearch>
</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);