Skip to content

Commit

Permalink
Use react-alert for backend message flashing (#3315)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch authored Aug 17, 2017
1 parent b9a2fa4 commit 59268e9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 10 deletions.
4 changes: 3 additions & 1 deletion superset/assets/javascripts/SqlLab/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class App extends React.PureComponent {
}
return (
<div className="App SqlLab">
<AlertsWrapper />
<AlertsWrapper initMessages={this.props.initMessages} />
<div className="container-fluid">
{content}
</div>
Expand All @@ -82,11 +82,13 @@ class App extends React.PureComponent {
App.propTypes = {
alerts: PropTypes.array,
actions: PropTypes.object,
initMessages: PropTypes.array,
};

function mapStateToProps(state) {
return {
alerts: state.alerts,
initMessages: state.flash_messages,
};
}
function mapDispatchToProps(dispatch) {
Expand Down
20 changes: 20 additions & 0 deletions superset/assets/javascripts/components/AlertsWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
/* global notify */
import React from 'react';
import AlertContainer from 'react-alert';
import PropTypes from 'prop-types';

const propTypes = {
initMessages: PropTypes.array,
};
const defaultProps = {
initMessages: [],
};

export default class AlertsWrapper extends React.PureComponent {
componentDidMount() {
this.props.initMessages.forEach((msg) => {
if (['info', 'error', 'success'].indexOf(msg[0]) >= 0) {
notify[msg[0]](msg[1]);
} else {
notify.show(msg[1]);
}
});
}
render() {
return (
<AlertContainer
Expand All @@ -16,3 +34,5 @@ export default class AlertsWrapper extends React.PureComponent {
/>);
}
}
AlertsWrapper.propTypes = propTypes;
AlertsWrapper.defaultProps = defaultProps;
8 changes: 6 additions & 2 deletions superset/assets/javascripts/dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const utils = require('../modules/utils');
appSetup();

export function getInitialState(boostrapData) {
const dashboard = Object.assign({}, utils.controllerInterface, boostrapData.dashboard_data);
const dashboard = Object.assign(
{},
utils.controllerInterface,
boostrapData.dashboard_data,
{ common: boostrapData.common });
dashboard.firstLoad = true;

dashboard.posDict = {};
Expand Down Expand Up @@ -62,7 +66,7 @@ function renderAlert() {
function initDashboardView(dashboard) {
render(
<div>
<AlertsWrapper />
<AlertsWrapper initMessages={dashboard.common.flash_messages} />
<Header dashboard={dashboard} />
</div>,
document.getElementById('dashboard-header'),
Expand Down
3 changes: 1 addition & 2 deletions superset/assets/javascripts/explore/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ const initState = {
const store = createStore(rootReducer, initState,
compose(applyMiddleware(thunk), initEnhancer(false)),
);

ReactDOM.render(
<Provider store={store}>
<div>
<ExploreViewContainer />
<AlertsWrapper />
<AlertsWrapper initMessages={bootstrappedState.common.flash_messages} />
</div>
</Provider>,
exploreViewContainer,
Expand Down
1 change: 0 additions & 1 deletion superset/templates/superset/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
{% endblock %}

{% block body %}
{% include 'superset/flash_wrapper.html' %}
<div id="app" data-bootstrap="{{ bootstrap_data }}" >
<img src="/static/assets/images/loading.gif" style="width: 50px; margin: 10px;">
</div>
Expand Down
2 changes: 0 additions & 2 deletions superset/templates/superset/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
class="dashboard container-fluid"
data-bootstrap="{{ bootstrap_data }}"
>
{% include 'superset/flash_wrapper.html' %}

<div id="dashboard-header"></div>

<!-- gridster class used for backwards compatibility -->
Expand Down
12 changes: 11 additions & 1 deletion superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import traceback

from flask import g, redirect, Response, flash, abort
from flask import g, redirect, Response, flash, abort, get_flashed_messages
from flask_babel import gettext as __
from flask_babel import lazy_gettext as _

Expand All @@ -17,6 +17,8 @@
from superset.connectors.connector_registry import ConnectorRegistry
from superset.connectors.sqla.models import SqlaTable

FRONTEND_CONF_KEYS = ('SUPERSET_WEBSERVER_TIMEOUT',)


def get_error_msg():
if conf.get("SHOW_STACKTRACE"):
Expand Down Expand Up @@ -186,6 +188,14 @@ def accessible_by_user(self, database, datasource_names, schema=None):
full_names = {d.full_name for d in user_datasources}
return [d for d in datasource_names if d in full_names]

def common_bootsrap_payload(self):
"""Common data always sent to the client"""
messages = get_flashed_messages(with_categories=True)
return {
'flash_messages': messages,
'conf': {k: conf.get(k) for k in FRONTEND_CONF_KEYS},
}


class SupersetModelView(ModelView):
page_size = 100
Expand Down
6 changes: 5 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ def explore(self, datasource_type, datasource_id):
"standalone": standalone,
"user_id": user_id,
"forced_height": request.args.get('height'),
'common': self.common_bootsrap_payload(),
}
table_name = datasource.table_name \
if datasource_type == 'table' \
Expand Down Expand Up @@ -1719,6 +1720,7 @@ def dashboard(**kwargs): # noqa
'user_id': g.user.get_id(),
'dashboard_data': dashboard_data,
'datasources': {ds.uid: ds.data for ds in datasources},
'common': self.common_bootsrap_payload(),
}

return self.render_template(
Expand Down Expand Up @@ -2268,7 +2270,8 @@ def profile(self, username):
'email': user.email,
'roles': roles,
'permissions': permissions,
}
},
'common': self.common_bootsrap_payload(),
}
return self.render_template(
'superset/basic.html',
Expand All @@ -2284,6 +2287,7 @@ def sqllab(self):
"""SQL Editor"""
d = {
'defaultDbId': config.get('SQLLAB_DEFAULT_DBID'),
'common': self.common_bootsrap_payload(),
}
return self.render_template(
'superset/basic.html',
Expand Down

0 comments on commit 59268e9

Please sign in to comment.