Skip to content

Commit b570fe2

Browse files
Dropdown button in query manager (ToolJet#3313)
1 parent 8f7a1ba commit b570fe2

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

frontend/src/Editor/QueryManager/QueryManager.jsx

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import DataSourceLister from './DataSourceLister';
1414
import { allSvgs } from '@tooljet/plugins/client';
1515
// import { Confirm } from '../Viewer/Confirm';
1616
import _, { isEmpty, isEqual } from 'lodash';
17+
import { Button, ButtonGroup, Dropdown } from 'react-bootstrap';
1718

1819
const queryNameRegex = new RegExp('^[A-Za-z0-9_-]*$');
1920

@@ -39,9 +40,11 @@ let QueryManager = class QueryManager extends React.Component {
3940
showSaveConfirmation: false,
4041
restArrayValuesChanged: false,
4142
nextProps: null,
43+
buttonText: '',
4244
};
4345

4446
this.previewPanelRef = React.createRef();
47+
this.buttonConfig = JSON.parse(localStorage.getItem('queryManagerButtonConfig'));
4548
}
4649

4750
setStateFromProps = (props) => {
@@ -90,6 +93,14 @@ let QueryManager = class QueryManager extends React.Component {
9093
base0E: '#d381c3',
9194
base0F: '#be643c',
9295
},
96+
buttonText:
97+
props.mode === 'edit'
98+
? this.buttonConfig?.editMode?.text ?? 'Save & Run'
99+
: this.buttonConfig?.createMode?.text ?? 'Create & Run',
100+
shouldRunQuery:
101+
props.mode === 'edit'
102+
? this.buttonConfig?.editMode?.shouldRunQuery ?? true
103+
: this.buttonConfig?.createMode?.shouldRunQuery ?? true,
93104
},
94105
() => {
95106
if (this.props.mode === 'edit') {
@@ -154,7 +165,7 @@ let QueryManager = class QueryManager extends React.Component {
154165
const prevLoading = this.state.currentState?.queries[query.name]
155166
? this.state.currentState?.queries[query.name]?.isLoading
156167
: false;
157-
if (!isEqual(this.state.selectedQuery, nextProps.selectedQuery)) {
168+
if (!isEmpty(nextProps.selectedQuery) && !isEqual(this.state.selectedQuery, nextProps.selectedQuery)) {
158169
if (query && !isLoading && !prevLoading) {
159170
this.props.runQuery(query.id, query.name);
160171
}
@@ -247,7 +258,7 @@ let QueryManager = class QueryManager extends React.Component {
247258
};
248259

249260
createOrUpdateDataQuery = () => {
250-
const { appId, options, selectedDataSource, mode, queryName } = this.state;
261+
const { appId, options, selectedDataSource, mode, queryName, shouldRunQuery } = this.state;
251262
const appVersionId = this.props.editingVersionId;
252263
const kind = selectedDataSource.kind;
253264
const dataSourceId = selectedDataSource.id === 'null' ? null : selectedDataSource.id;
@@ -264,10 +275,10 @@ let QueryManager = class QueryManager extends React.Component {
264275
.update(this.state.selectedQuery.id, queryName, options)
265276
.then((data) => {
266277
this.setState({
267-
isUpdating: selectedDataSource?.kind !== 'runjs' ? true : false,
278+
isUpdating: shouldRunQuery ? true : false,
268279
isFieldsChanged: false,
269280
restArrayValuesChanged: false,
270-
updatedQuery: selectedDataSource?.kind !== 'runjs' ? { ...data, updateQuery: true } : {},
281+
updatedQuery: shouldRunQuery ? { ...data, updateQuery: true } : {},
271282
});
272283
this.props.dataQueriesChanged();
273284
this.props.setStateOfUnsavedQueries(false);
@@ -284,10 +295,10 @@ let QueryManager = class QueryManager extends React.Component {
284295
.then((data) => {
285296
toast.success('Query Added');
286297
this.setState({
287-
isCreating: selectedDataSource?.kind !== 'runjs' ? true : false,
298+
isCreating: shouldRunQuery ? true : false,
288299
isFieldsChanged: false,
289300
restArrayValuesChanged: false,
290-
updatedQuery: selectedDataSource?.kind !== 'runjs' ? { ...data, updateQuery: false } : {},
301+
updatedQuery: shouldRunQuery ? { ...data, updateQuery: false } : {},
291302
});
292303
this.props.dataQueriesChanged();
293304
this.props.setStateOfUnsavedQueries(false);
@@ -358,6 +369,17 @@ let QueryManager = class QueryManager extends React.Component {
358369
this.optionchanged('events', events);
359370
};
360371

372+
updateButtonText = (text, shouldRunQuery) => {
373+
if (this.state.mode === 'edit') {
374+
this.buttonConfig = { ...this.buttonConfig, editMode: { text: text, shouldRunQuery: shouldRunQuery } };
375+
localStorage.setItem('queryManagerButtonConfig', JSON.stringify(this.buttonConfig));
376+
} else {
377+
this.buttonConfig = { ...this.buttonConfig, createMode: { text: text, shouldRunQuery: shouldRunQuery } };
378+
localStorage.setItem('queryManagerButtonConfig', JSON.stringify(this.buttonConfig));
379+
}
380+
this.setState({ buttonText: text, shouldRunQuery: shouldRunQuery });
381+
};
382+
361383
render() {
362384
const {
363385
dataSources,
@@ -384,8 +406,7 @@ let QueryManager = class QueryManager extends React.Component {
384406
ElementToRender = allSources[sourcecomponentName];
385407
}
386408

387-
let buttonText = mode === 'edit' ? 'Save' : 'Create';
388-
buttonText = selectedDataSource?.kind !== 'runjs' ? buttonText + ' & Run' : buttonText;
409+
let dropDownButtonText = mode === 'edit' ? 'Save' : 'Create';
389410
const buttonDisabled = isUpdating || isCreating;
390411
const mockDataQueryComponent = this.mockDataQueryAsComponent();
391412
const Icon = allSvgs[this?.state?.selectedDataSource?.kind];
@@ -468,16 +489,39 @@ let QueryManager = class QueryManager extends React.Component {
468489
</button>
469490
)}
470491
{selectedDataSource && (addingQuery || editingQuery) && (
471-
<button
472-
onClick={this.createOrUpdateDataQuery}
473-
disabled={buttonDisabled}
474-
className={`btn btn-primary m-1 float-right ${isUpdating || isCreating ? 'btn-loading' : ''} ${
475-
this.state.selectedDataSource ? '' : 'disabled'
476-
}`}
477-
style={{ width: '102px', height: '28px' }}
478-
>
479-
{buttonText}
480-
</button>
492+
<Dropdown as={ButtonGroup} className={'m-1 float-right'} style={{ display: 'initial', height: '28px' }}>
493+
<Button
494+
className={`btn btn-primary ${isUpdating || isCreating ? 'btn-loading' : ''} ${
495+
this.state.selectedDataSource ? '' : 'disabled'
496+
}`}
497+
style={{ width: '100px', height: '28px' }}
498+
onClick={this.createOrUpdateDataQuery}
499+
disabled={buttonDisabled}
500+
>
501+
{this.state.buttonText}
502+
</Button>
503+
<Dropdown.Toggle
504+
split
505+
className="btn btn-primary d-none d-lg-inline"
506+
style={{ height: '28px', paddingTop: '5px' }}
507+
/>
508+
<Dropdown.Menu className="import-lg-position">
509+
<Dropdown.Item
510+
onClick={() => {
511+
this.updateButtonText(dropDownButtonText, false);
512+
}}
513+
>
514+
{dropDownButtonText}
515+
</Dropdown.Item>
516+
<Dropdown.Item
517+
onClick={() => {
518+
this.updateButtonText(`${dropDownButtonText} & Run`, true);
519+
}}
520+
>
521+
{`${dropDownButtonText} & Run`}
522+
</Dropdown.Item>
523+
</Dropdown.Menu>
524+
</Dropdown>
481525
)}
482526
<span onClick={this.props.toggleQueryEditor} className="cursor-pointer m-3" data-tip="Hide query editor">
483527
<svg width="18" height="10" viewBox="0 0 18 10" fill="none" xmlns="http://www.w3.org/2000/svg">

0 commit comments

Comments
 (0)