From 52380534f360cbb0e3d7ab46889066cd2df47440 Mon Sep 17 00:00:00 2001 From: vera-liu Date: Tue, 1 Nov 2016 09:00:24 -0700 Subject: [PATCH] Moved ajax call for fetching table metadata from SqlEditorLeftBar to actions (#1494) --- caravel/assets/javascripts/SqlLab/actions.js | 36 +++++++++++++++++++ .../SqlLab/components/SqlEditorLeftBar.jsx | 31 ++-------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/caravel/assets/javascripts/SqlLab/actions.js b/caravel/assets/javascripts/SqlLab/actions.js index 6e46ae22851d6..fa7f6e346e463 100644 --- a/caravel/assets/javascripts/SqlLab/actions.js +++ b/caravel/assets/javascripts/SqlLab/actions.js @@ -202,6 +202,42 @@ export function mergeTable(table) { return { type: MERGE_TABLE, table }; } +export function addTable(query, tableName) { + return function (dispatch) { + let url = `/caravel/table/${query.dbId}/${tableName}/${query.schema}/`; + $.get(url, (data) => { + dispatch( + mergeTable(Object.assign(data, { + dbId: query.dbId, + queryEditorId: query.id, + schema: query.schema, + expanded: true, + })) + ); + }) + .fail(() => { + dispatch( + addAlert({ + msg: 'Error occurred while fetching metadata', + bsStyle: 'danger', + }) + ); + }); + + url = `/caravel/extra_table_metadata/${query.dbId}/${tableName}/${query.schema}/`; + $.get(url, (data) => { + const table = { + dbId: query.dbId, + queryEditorId: query.id, + schema: query.schema, + name: tableName, + }; + Object.assign(table, data); + dispatch(mergeTable(table)); + }); + }; +} + export function expandTable(table) { return { type: EXPAND_TABLE, table }; } diff --git a/caravel/assets/javascripts/SqlLab/components/SqlEditorLeftBar.jsx b/caravel/assets/javascripts/SqlLab/components/SqlEditorLeftBar.jsx index 92844ac1b472c..f61c59d700e74 100644 --- a/caravel/assets/javascripts/SqlLab/components/SqlEditorLeftBar.jsx +++ b/caravel/assets/javascripts/SqlLab/components/SqlEditorLeftBar.jsx @@ -87,37 +87,10 @@ class SqlEditorLeftBar extends React.PureComponent { changeTable(tableOpt) { const tableName = tableOpt.value; const qe = this.props.queryEditor; - let url = `/caravel/table/${qe.dbId}/${tableName}/${qe.schema}/`; this.setState({ tableLoading: true }); - $.get(url, (data) => { - this.props.actions.mergeTable(Object.assign(data, { - dbId: qe.dbId, - queryEditorId: qe.id, - schema: qe.schema, - expanded: true, - })); - this.setState({ tableLoading: false }); - }) - .fail(() => { - this.props.actions.addAlert({ - msg: 'Error occurred while fetching metadata', - bsStyle: 'danger', - }); - this.setState({ tableLoading: false }); - }); - - url = `/caravel/extra_table_metadata/${qe.dbId}/${tableName}/${qe.schema}/`; - $.get(url, (data) => { - const table = { - dbId: this.props.queryEditor.dbId, - queryEditorId: this.props.queryEditor.id, - schema: qe.schema, - name: tableName, - }; - Object.assign(table, data); - this.props.actions.mergeTable(table); - }); + this.props.actions.addTable(qe, tableName); + this.setState({ tableLoading: false }); } render() { let networkAlert = null;