Vulcan/packages/vulcan-core/lib/modules/components/App.jsx

86 lines
2 KiB
React
Raw Normal View History

import {
Components,
registerComponent,
registerSetting,
getSetting,
Strings,
runCallbacks,
} from 'meteor/vulcan:lib';
2017-05-19 14:42:43 -06:00
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { IntlProvider, intlShape } from 'meteor/vulcan:i18n';
import withCurrentUser from '../containers/withCurrentUser.js';
2017-05-19 14:42:43 -06:00
class App extends PureComponent {
constructor(props) {
super(props);
if (props.currentUser) {
runCallbacks('events.identify', props.currentUser);
}
}
2016-03-24 18:17:35 +09:00
2016-06-09 17:42:20 +09:00
getLocale() {
2017-05-19 14:42:43 -06:00
return getSetting('locale', 'en');
2016-06-09 17:42:20 +09:00
}
getChildContext() {
2016-12-12 15:10:53 +09:00
const messages = Strings[this.getLocale()] || {};
const intlProvider = new IntlProvider(
{ locale: this.getLocale() },
messages
);
2017-05-19 14:42:43 -06:00
const { intl } = intlProvider.getChildContext();
2016-03-24 18:17:35 +09:00
return {
intl: intl,
2016-03-24 18:17:35 +09:00
};
}
componentWillUpdate(nextProps) {
2017-12-17 18:00:48 +09:00
if (!this.props.currentUser && nextProps.currentUser) {
runCallbacks('events.identify', nextProps.currentUser);
}
}
render() {
const currentRoute = _.last(this.props.routes);
const LayoutComponent = currentRoute.layoutName
? Components[currentRoute.layoutName]
: Components.Layout;
return (
<IntlProvider
locale={this.getLocale()}
messages={Strings[this.getLocale()]}
>
<div>
<Components.HeadTags />
<Components.RouterHook currentRoute={currentRoute} />
<LayoutComponent {...this.props} currentRoute={currentRoute}>
{this.props.currentUserLoading ? (
<Components.Loading />
) : this.props.children ? (
this.props.children
) : (
<Components.Welcome />
)}
</LayoutComponent>
</div>
</IntlProvider>
2017-05-19 14:42:43 -06:00
);
}
}
App.propTypes = {
currentUserLoading: PropTypes.bool,
};
App.childContextTypes = {
intl: intlShape,
};
App.displayName = 'App';
registerComponent('App', App, withCurrentUser);
export default App;