2018-05-10 09:38:25 +09:00
|
|
|
import { Components, registerComponent, getSetting, Strings, runCallbacks, detectLocale } from 'meteor/vulcan:lib';
|
2017-05-19 14:42:43 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-05-10 09:38:25 +09:00
|
|
|
import { IntlProvider, intlShape } from 'meteor/vulcan:i18n';
|
2016-12-21 12:04:43 +01:00
|
|
|
import withCurrentUser from '../containers/withCurrentUser.js';
|
2018-05-10 17:48:06 +09:00
|
|
|
import withEdit from '../containers/withEdit.js';
|
2016-02-14 12:27:20 +09:00
|
|
|
|
2017-05-19 14:42:43 -06:00
|
|
|
class App extends PureComponent {
|
2017-12-17 17:42:06 +09:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
if (props.currentUser) {
|
|
|
|
runCallbacks('events.identify', props.currentUser);
|
|
|
|
}
|
2018-04-21 17:58:25 +09:00
|
|
|
this.state = {
|
2018-05-04 12:01:12 +09:00
|
|
|
locale: this.initLocale(),
|
|
|
|
};
|
2018-04-21 17:58:25 +09:00
|
|
|
}
|
|
|
|
|
2018-05-04 12:01:12 +09:00
|
|
|
initLocale = () => {
|
|
|
|
let userLocale = '';
|
|
|
|
const { currentUser, cookies } = this.props;
|
|
|
|
const availableLocales = Object.keys(Strings);
|
|
|
|
|
|
|
|
if (currentUser && currentUser.locale) {
|
|
|
|
// 1. if user is logged in, check for their preferred locale
|
|
|
|
userLocale = currentUser;
|
|
|
|
} else if (cookies && cookies.get('locale')) {
|
|
|
|
// 2. else, look for a cookie
|
|
|
|
userLocale = cookies.get('locale');
|
|
|
|
} else if (detectLocale()) {
|
|
|
|
// 3. else, check for browser settings
|
|
|
|
userLocale = detectLocale();
|
|
|
|
}
|
|
|
|
// if user locale is available, use it; else compare first two chars
|
|
|
|
// of user locale with first two chars of available locales
|
|
|
|
const availableLocale = Strings[userLocale] ? userLocale : availableLocales.find(locale => locale.slice(0,2) === userLocale.slice(0,2));
|
|
|
|
|
|
|
|
// 4. if user-defined locale is available, use it; else default to setting or `en-US`
|
|
|
|
return availableLocale ? availableLocale : getSetting('locale', 'en-US');
|
|
|
|
};
|
2016-03-24 18:17:35 +09:00
|
|
|
|
2018-05-04 12:01:12 +09:00
|
|
|
getLocale = (truncate = false) => {
|
|
|
|
return truncate ? this.state.locale.slice(0,2) : this.state.locale;
|
|
|
|
};
|
|
|
|
|
|
|
|
setLocale = locale => {
|
2018-04-21 17:58:25 +09:00
|
|
|
this.setState({ locale });
|
2018-05-10 17:48:06 +09:00
|
|
|
// if user is logged in, change their `locale` profile property
|
|
|
|
if (this.props.currentUser) {
|
|
|
|
this.props.editMutation({ documentId: this.props.currentUser._id, set: { locale }});
|
|
|
|
}
|
2018-05-04 12:01:12 +09:00
|
|
|
};
|
2016-06-09 17:42:20 +09:00
|
|
|
|
2016-03-27 13:04:21 +09:00
|
|
|
getChildContext() {
|
2016-12-12 15:10:53 +09:00
|
|
|
const messages = Strings[this.getLocale()] || {};
|
2018-05-04 12:01:12 +09:00
|
|
|
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 {
|
2017-12-17 17:42:06 +09:00
|
|
|
intl: intl,
|
2018-04-21 17:58:25 +09:00
|
|
|
getLocale: this.getLocale,
|
|
|
|
setLocale: this.setLocale,
|
2016-03-24 18:17:35 +09:00
|
|
|
};
|
2016-03-27 13:04:21 +09:00
|
|
|
}
|
2016-02-14 12:27:20 +09:00
|
|
|
|
2017-12-17 17:42:06 +09:00
|
|
|
componentWillUpdate(nextProps) {
|
2017-12-17 18:00:48 +09:00
|
|
|
if (!this.props.currentUser && nextProps.currentUser) {
|
2017-12-17 17:42:06 +09:00
|
|
|
runCallbacks('events.identify', nextProps.currentUser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 12:27:20 +09:00
|
|
|
render() {
|
2017-06-14 09:53:42 +09:00
|
|
|
const currentRoute = _.last(this.props.routes);
|
2017-12-17 17:42:06 +09:00
|
|
|
const LayoutComponent = currentRoute.layoutName
|
|
|
|
? Components[currentRoute.layoutName]
|
|
|
|
: Components.Layout;
|
2017-06-14 09:53:42 +09:00
|
|
|
|
2016-06-16 10:43:25 +09:00
|
|
|
return (
|
2017-12-17 17:42:06 +09:00
|
|
|
<IntlProvider
|
|
|
|
locale={this.getLocale()}
|
|
|
|
messages={Strings[this.getLocale()]}
|
|
|
|
>
|
2018-04-21 17:58:25 +09:00
|
|
|
<div className={`locale-${this.getLocale()}`}>
|
2017-06-14 09:53:42 +09:00
|
|
|
<Components.HeadTags />
|
2017-12-17 17:42:06 +09:00
|
|
|
<Components.RouterHook currentRoute={currentRoute} />
|
2017-06-14 09:53:42 +09:00
|
|
|
<LayoutComponent {...this.props} currentRoute={currentRoute}>
|
2017-12-17 17:42:06 +09:00
|
|
|
{this.props.currentUserLoading ? (
|
|
|
|
<Components.Loading />
|
|
|
|
) : this.props.children ? (
|
|
|
|
this.props.children
|
|
|
|
) : (
|
|
|
|
<Components.Welcome />
|
|
|
|
)}
|
2017-06-14 09:53:42 +09:00
|
|
|
</LayoutComponent>
|
|
|
|
</div>
|
2016-06-16 10:43:25 +09:00
|
|
|
</IntlProvider>
|
2017-05-19 14:42:43 -06:00
|
|
|
);
|
2016-02-14 12:27:20 +09:00
|
|
|
}
|
2016-03-27 13:04:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
App.propTypes = {
|
2017-05-23 08:42:28 -06:00
|
|
|
currentUserLoading: PropTypes.bool,
|
2017-12-17 17:42:06 +09:00
|
|
|
};
|
2016-03-27 13:04:21 +09:00
|
|
|
|
|
|
|
App.childContextTypes = {
|
2016-11-03 14:39:27 +09:00
|
|
|
intl: intlShape,
|
2018-04-21 17:58:25 +09:00
|
|
|
setLocale: PropTypes.func,
|
|
|
|
getLocale: PropTypes.func,
|
2017-12-17 17:42:06 +09:00
|
|
|
};
|
2016-02-16 15:40:37 +09:00
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
App.displayName = 'App';
|
|
|
|
|
2018-05-10 17:48:06 +09:00
|
|
|
const editOptions = {
|
|
|
|
collectionName: 'Users',
|
|
|
|
fragmentName: 'UsersCurrent',
|
|
|
|
}
|
|
|
|
|
|
|
|
registerComponent('App', App, withCurrentUser, [withEdit, editOptions]);
|
2016-11-27 19:12:54 +09:00
|
|
|
|
2016-12-21 12:04:43 +01:00
|
|
|
export default App;
|