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

154 lines
4.8 KiB
React
Raw Normal View History

2018-11-05 09:46:17 +09:00
import {
Components,
registerComponent,
getSetting,
Strings,
runCallbacks,
detectLocale,
hasIntlFields,
} from 'meteor/vulcan:lib';
import React, { PureComponent, Fragment } from 'react';
2017-05-19 14:42:43 -06:00
import PropTypes from 'prop-types';
2018-05-10 09:38:25 +09:00
import { IntlProvider, intlShape } from 'meteor/vulcan:i18n';
import withCurrentUser from '../containers/withCurrentUser.js';
2018-06-02 18:49:53 +09:00
import withUpdate from '../containers/withUpdate.js';
2018-05-10 18:17:53 +09:00
import { withApollo } from 'react-apollo';
import { withCookies } from 'react-cookie';
2018-06-12 11:23:36 +09:00
import moment from 'moment';
2018-11-05 09:46:17 +09:00
const DummyErrorCatcher = ({ children }) => children;
2017-05-19 14:42:43 -06:00
class App extends PureComponent {
constructor(props) {
super(props);
if (props.currentUser) {
runCallbacks('events.identify', props.currentUser);
}
const { locale, localeMethod } = this.initLocale();
this.state = { locale, localeMethod };
2018-06-12 11:23:36 +09:00
moment.locale(locale);
}
initLocale = () => {
let userLocale = '';
let localeMethod = '';
const { currentUser, cookies, locale } = this.props;
const availableLocales = Object.keys(Strings);
const detectedLocale = detectLocale();
2018-11-05 09:46:17 +09:00
if (locale) {
// 1. locale is passed through SSR process
// TODO: currently SSR locale is passed through cookies as a hack
userLocale = locale;
localeMethod = 'SSR';
} else if (cookies && cookies.get('locale')) {
// 2. look for a cookie
userLocale = cookies.get('locale');
localeMethod = 'cookie';
} else if (currentUser && currentUser.locale) {
// 3. if user is logged in, check for their preferred locale
userLocale = currentUser.locale;
localeMethod = 'user';
2018-11-05 09:46:17 +09:00
} else if (detectedLocale) {
// 4. else, check for browser settings
userLocale = detectedLocale;
localeMethod = 'browser';
}
// if user locale is available, use it; else compare first two chars
// of user locale with first two chars of available locales
2018-11-05 09:46:17 +09:00
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`
if (availableLocale) {
2018-11-05 09:46:17 +09:00
return { locale: availableLocale, localeMethod };
} else {
2018-11-05 09:46:17 +09:00
return { locale: getSetting('locale', 'en-US'), localeMethod: 'setting' };
}
};
2016-03-24 18:17:35 +09:00
2018-11-05 09:46:17 +09:00
getLocale = truncate => {
return truncate ? this.state.locale.slice(0, 2) : this.state.locale;
};
2018-05-10 18:17:53 +09:00
setLocale = async locale => {
const { cookies, updateUser, client, currentUser } = this.props;
this.setState({ locale });
cookies.remove('locale', { path: '/' });
cookies.set('locale', locale, { path: '/' });
// if user is logged in, change their `locale` profile property
if (currentUser) {
2018-11-05 09:46:17 +09:00
await updateUser({ selector: { documentId: currentUser._id }, data: { locale } });
}
2018-06-12 11:23:36 +09:00
moment.locale(locale);
if (hasIntlFields) {
client.resetStore();
}
};
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,
getLocale: this.getLocale,
setLocale: this.setLocale,
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);
2018-11-05 09:46:17 +09:00
const LayoutComponent = currentRoute.layoutName ? Components[currentRoute.layoutName] : Components.Layout;
// if defined, use ErrorCatcher component to wrap layout contents
const ErrorCatcher = Components.ErrorCatcher ? Components.ErrorCatcher : DummyErrorCatcher;
return (
2018-11-05 09:46:17 +09:00
<IntlProvider locale={this.getLocale()} key={this.getLocale()} messages={Strings[this.getLocale()]}>
<div className={`locale-${this.getLocale()}`}>
<Components.HeadTags />
<Components.RouterHook currentRoute={currentRoute} />
<LayoutComponent {...this.props} currentRoute={currentRoute}>
{this.props.currentUserLoading ? (
<Components.Loading />
) : this.props.children ? (
2018-11-05 09:46:17 +09:00
<ErrorCatcher>{this.props.children}</ErrorCatcher>
) : (
<Components.Welcome />
)}
</LayoutComponent>
</div>
</IntlProvider>
2017-05-19 14:42:43 -06:00
);
}
}
App.propTypes = {
currentUserLoading: PropTypes.bool,
};
App.childContextTypes = {
intl: intlShape,
setLocale: PropTypes.func,
getLocale: PropTypes.func,
};
App.displayName = 'App';
2018-06-02 18:49:53 +09:00
const updateOptions = {
collectionName: 'Users',
fragmentName: 'UsersCurrent',
2018-11-05 09:46:17 +09:00
};
2018-06-02 18:49:53 +09:00
registerComponent('App', App, withCurrentUser, [withUpdate, updateOptions], withApollo, withCookies);
export default App;