Files
freeCodeCamp/client/src/components/layouts/Default.js
Kristofer Koishigawa 77b27d79f6 fix: conditionally load MathJax (#37360)
* fix: Removed MathJax CDN from header and set it up to download whenever a user goes to a Rosetta Code challenge

* Reworked slightly so that MathJax CDN script is only rendered once

* Simplified further

* Resolved conflicts and updated MathJax fallback to work like the one on donate.js

* Escaped backticks in scriptLoaders.js

* refactor: remove reliance on state
2019-10-30 15:46:39 +03:00

233 lines
6.0 KiB
JavaScript

import React, { Fragment, Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { createSelector } from 'reselect';
import Helmet from 'react-helmet';
import fontawesome from '@fortawesome/fontawesome';
import ga from '../../analytics';
import {
fetchUser,
isSignedInSelector,
onlineStatusChange,
isOnlineSelector,
userSelector
} from '../../redux';
import { flashMessageSelector, removeFlashMessage } from '../Flash/redux';
import { isBrowser } from '../../../utils';
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';
import './fonts.css';
import './global.css';
import './variables.css';
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,
pathname: PropTypes.string.isRequired,
removeFlashMessage: PropTypes.func.isRequired,
showFooter: PropTypes.bool,
theme: PropTypes.string
};
const mapStateToProps = createSelector(
isSignedInSelector,
flashMessageSelector,
isOnlineSelector,
userSelector,
(isSignedIn, flashMessage, isOnline, user) => ({
isSignedIn,
flashMessage,
hasMessage: !!flashMessage.message,
isOnline,
theme: user.theme
})
);
const mapDispatchToProps = dispatch =>
bindActionCreators(
{ fetchUser, removeFlashMessage, onlineStatusChange },
dispatch
);
class DefaultLayout extends Component {
componentDidMount() {
const { isSignedIn, fetchUser, pathname } = this.props;
if (!isSignedIn) {
fetchUser();
}
ga.pageview(pathname);
window.addEventListener('online', this.updateOnlineStatus);
window.addEventListener('offline', this.updateOnlineStatus);
}
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,
isOnline,
isSignedIn,
removeFlashMessage,
showFooter = true,
theme = 'default'
} = this.props;
return (
<Fragment>
<Helmet
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 />
<div className={`default-layout`}>
<OfflineWarning isOnline={isOnline} isSignedIn={isSignedIn} />
{hasMessage && flashMessage ? (
<Flash flashMessage={flashMessage} onClose={removeFlashMessage} />
) : null}
{children}
{showFooter && <Footer />}
</div>
</WithInstantSearch>
</Fragment>
);
}
}
DefaultLayout.displayName = 'DefaultLayout';
DefaultLayout.propTypes = propTypes;
export default connect(
mapStateToProps,
mapDispatchToProps
)(DefaultLayout);