| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
const { __ } = wp.i18n; |
| 5 |
|
| 6 |
const { apiRequest } = wp; |
| 7 |
|
| 8 |
const { |
| 9 |
Button, |
| 10 |
ExternalLink |
| 11 |
} = wp.components; |
| 12 |
|
| 13 |
const { |
| 14 |
Component, |
| 15 |
Fragment |
| 16 |
} = wp.element; |
| 17 |
|
| 18 |
|
| 19 |
class SQLEditor extends Component { |
| 20 |
constructor() { |
| 21 |
super( ...arguments ); |
| 22 |
|
| 23 |
this.onSave = this.onSave.bind( this ); |
| 24 |
|
| 25 |
this.state = { |
| 26 |
isLoading: false, |
| 27 |
success: false, |
| 28 |
query: '', |
| 29 |
name: '', |
| 30 |
series: {}, |
| 31 |
data: [] |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
componentDidMount() { |
| 36 |
const editor = wp.CodeMirror || CodeMirror; |
| 37 |
const textarea = document.querySelector( '.visualizer-db-query' ); |
| 38 |
const cm = editor.fromTextArea( textarea, { |
| 39 |
autofocus: true, |
| 40 |
mode: 'text/x-mysql', |
| 41 |
lineWrapping: true, |
| 42 |
dragDrop: false, |
| 43 |
matchBrackets: true, |
| 44 |
autoCloseBrackets: true, |
| 45 |
extraKeys: { 'Shift-Space': 'autocomplete' }, |
| 46 |
hintOptions: { tables: visualizerLocalize.sqlTable } |
| 47 |
}); |
| 48 |
|
| 49 |
cm.on( 'inputRead', () => { |
| 50 |
cm.save(); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
async onSave() { |
| 55 |
const textarea = document.querySelector( '.visualizer-db-query' ).value; |
| 56 |
const result = document.querySelector( '#visualizer-db-query-table' ); |
| 57 |
result.innerHTML = ''; |
| 58 |
|
| 59 |
await this.setState({ isLoading: true }); |
| 60 |
|
| 61 |
let response = await apiRequest({ |
| 62 |
path: '/visualizer/v1/get-query-data', |
| 63 |
data: { |
| 64 |
query: textarea |
| 65 |
}, |
| 66 |
method: 'GET' |
| 67 |
}); |
| 68 |
|
| 69 |
await this.setState({ |
| 70 |
isLoading: false, |
| 71 |
success: response.success, |
| 72 |
query: textarea, |
| 73 |
name: response.data.name || '', |
| 74 |
series: response.data.series || {}, |
| 75 |
data: response.data.data || [] |
| 76 |
}); |
| 77 |
|
| 78 |
result.innerHTML = response.data.table || response.data.msg; |
| 79 |
|
| 80 |
if ( this.state.success ) { |
| 81 |
jQuery( '#results' ).DataTable({ |
| 82 |
paging: false |
| 83 |
}); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
render() { |
| 88 |
return ( |
| 89 |
<Fragment> |
| 90 |
<textarea |
| 91 |
className="visualizer-db-query" |
| 92 |
placeholder={ __( 'Your query goes here…' ) } |
| 93 |
> |
| 94 |
{ this.props.chart['visualizer-db-query'] } |
| 95 |
</textarea> |
| 96 |
|
| 97 |
<div className="visualizer-db-query-actions"> |
| 98 |
<Button |
| 99 |
isLarge |
| 100 |
variant="secondary" |
| 101 |
isBusy={ this.state.isLoading } |
| 102 |
onClick={ this.onSave } |
| 103 |
> |
| 104 |
{ __( 'Show Results' ) } |
| 105 |
</Button> |
| 106 |
|
| 107 |
<Button |
| 108 |
isLarge |
| 109 |
isPrimary |
| 110 |
disabled={ ! this.state.success } |
| 111 |
onClick={ () => this.props.save( this.state.query, this.state.name, this.state.series, this.state.data ) } |
| 112 |
> |
| 113 |
{ __( 'Save' ) } |
| 114 |
</Button> |
| 115 |
</div> |
| 116 |
|
| 117 |
<ul> |
| 118 |
<li> |
| 119 |
<ExternalLink href="https://docs.themeisle.com/article/970-visualizer-sample-queries-to-generate-charts"> |
| 120 |
{ __( 'Examples of queries and links to resources that you can use with this feature.' ) } |
| 121 |
</ExternalLink> |
| 122 |
</li> |
| 123 |
<li>{ __( 'Use Control+Space for autocompleting keywords or table names.' ) }</li> |
| 124 |
</ul> |
| 125 |
|
| 126 |
<div |
| 127 |
id="visualizer-db-query-table" |
| 128 |
className={ ! this.state.success && 'db-wizard-error' } |
| 129 |
> |
| 130 |
</div> |
| 131 |
</Fragment> |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
export default SQLEditor; |
| 137 |
|