Skip to content

Instantly share code, notes, and snippets.

@rik
Created October 23, 2015 15:08
Show Gist options
  • Save rik/e3753b5ec7e0651d7642 to your computer and use it in GitHub Desktop.
Save rik/e3753b5ec7e0651d7642 to your computer and use it in GitHub Desktop.
Batch, not so ugly
containers/NavigationContainer.jsx | 37 ++++++++++++++++++++++++++++---------
1 file changed, 28 insertions(+), 9 deletions(-)
diff --git a/containers/NavigationContainer.jsx b/containers/NavigationContainer.jsx
index 2ae609f..071d35d 100644
--- a/containers/NavigationContainer.jsx
+++ b/containers/NavigationContainer.jsx
@@ -30,6 +30,10 @@ import TopNavigationModal from 'Components/Navigation/TopNavigationModal.jsx';
export default React.createClass({
mixins: [i18nContextMixin],
+ getInitialState() {
+ return {};
+ },
+
componentWillMount() {
this._componentCallbacks = {
editBasketOfferQuantity: this.editBasketOfferQuantity,
@@ -40,7 +44,7 @@ export default React.createClass({
};
PanelsManager.on('change', function() {
- this.setState({
+ this.setStateDefered({
visiblePanel: PanelsManager.getVisiblePanel(),
});
}.bind(this));
@@ -57,7 +61,7 @@ export default React.createClass({
});
AppChannel.vent.on('active:link', function(activeLink) {
- this.setState({
+ this.setStateDefered({
activeLink,
});
}.bind(this));
@@ -79,7 +83,7 @@ export default React.createClass({
$('body')
.toggleClass('layout-fixed', !isMobile)
.toggleClass('layout-fixed-mobile', isMobile);
- this.setState({
+ this.setStateDefered({
isMobile,
});
}
@@ -107,7 +111,7 @@ export default React.createClass({
this._model.unset('assembly');
}
- this.setState(state);
+ this.setStateDefered(state);
if (currentUser) {
this._model.set({
@@ -116,10 +120,21 @@ export default React.createClass({
}
},
+ _nextState: {},
+
+ setStateDefered(state) {
+ window.clearTimeout(this._timer);
+ this._nextState = Object.assign(this._nextState, state);
+ this._timer = setTimeout(() => {
+ this.setState(this._nextState);
+ this._nextState = {};
+ });
+ },
+
onAssemblyChange() {
const assembly = this._model.get('assembly');
- this.setState({
+ this.setStateDefered({
currentAssembly: assembly && assembly.toJSON(),
});
@@ -141,7 +156,7 @@ export default React.createClass({
onBasketChange() {
const basket = this._model.get('basket');
if (!basket) return;
- this.setState({
+ this.setStateDefered({
basket: basket.serialize(),
currentAssemblyId: this._model.get('assembly').id,
currentDistributionId: this._model.get('assembly').getCurrentDistributionId(),
@@ -153,7 +168,7 @@ export default React.createClass({
if (FeaturesService.isFeatureActive('show_previously_bought_products', this._model.get('user'))) {
categoriesOrderMethod = 'bySortedCategoriesIncludingAlreadyBought';
}
- this.setState({
+ this.setStateDefered({
productCategories: this._model.get('productTypes')[categoriesOrderMethod](),
currentAssemblyId: this._model.get('assembly').id,
currentDistributionId: this._model.get('assembly').getCurrentDistributionId(),
@@ -181,7 +196,7 @@ export default React.createClass({
state.userAssemblies = user.getHivesThatImNotHosting().toJSON();
}
- this.setState(state);
+ this.setStateDefered(state);
},
editBasketOfferQuantity(item, quantity) {
@@ -237,7 +252,11 @@ export default React.createClass({
},
render() {
- switch (this._model.get('context.type')) {
+ console.log('render');
+ if (!this.state.context) {
+ return <div/>;
+ }
+ switch (this.state.context.type) {
case 'profile':
return <VisitorAuthenticatedNavigation {...this.state} {...this._componentCallbacks} />;
case 'memberOpen':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment