| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
const { __ } = wp.i18n; |
| 5 |
|
| 6 |
const { apiFetch } = wp; |
| 7 |
|
| 8 |
const { |
| 9 |
Component, |
| 10 |
Fragment |
| 11 |
} = wp.element; |
| 12 |
|
| 13 |
const { |
| 14 |
Button, |
| 15 |
PanelBody, |
| 16 |
Placeholder, |
| 17 |
SelectControl, |
| 18 |
Spinner |
| 19 |
} = wp.components; |
| 20 |
|
| 21 |
class ChartImport extends Component { |
| 22 |
constructor() { |
| 23 |
super( ...arguments ); |
| 24 |
|
| 25 |
this.state = { |
| 26 |
id: '', |
| 27 |
charts: [] |
| 28 |
}; |
| 29 |
} |
| 30 |
|
| 31 |
async componentDidMount() { |
| 32 |
let charts = await apiFetch({ path: 'wp/v2/visualizer/?per_page=100' }); |
| 33 |
let id; |
| 34 |
|
| 35 |
charts = charts.map( ( i, index ) => { |
| 36 |
let label = i['chart_data']['visualizer-settings'].title ? i['chart_data']['visualizer-settings'].title : `#${i.id}`; |
| 37 |
if ( 'object' === typeof label ) { |
| 38 |
label = `#${i.id}`; |
| 39 |
} |
| 40 |
if ( 0 === index ) { |
| 41 |
id = i.id; |
| 42 |
} |
| 43 |
|
| 44 |
return { |
| 45 |
value: i.id, |
| 46 |
label |
| 47 |
}; |
| 48 |
}); |
| 49 |
|
| 50 |
this.setState({ |
| 51 |
id, |
| 52 |
charts |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
render() { |
| 57 |
return ( |
| 58 |
( 'community' !== visualizerLocalize.isPro ) ? |
| 59 |
<PanelBody |
| 60 |
title={ __( 'Import from other chart' ) } |
| 61 |
initialOpen={ false } |
| 62 |
> |
| 63 |
|
| 64 |
{ 1 <= ( this.state.charts ).length ? |
| 65 |
<Fragment> |
| 66 |
|
| 67 |
<SelectControl |
| 68 |
label={ __( 'You can import here data from your previously created charts.' ) } |
| 69 |
value={ this.state.id } |
| 70 |
options={ this.state.charts } |
| 71 |
onChange={ id => this.setState({ id }) } |
| 72 |
/> |
| 73 |
|
| 74 |
<Button |
| 75 |
isPrimary |
| 76 |
isLarge |
| 77 |
isBusy={ 'getChartData' === this.props.isLoading } |
| 78 |
onClick={ () => this.props.getChartData( this.state.id ) } |
| 79 |
> |
| 80 |
{ __( 'Import Chart' ) } |
| 81 |
</Button> |
| 82 |
|
| 83 |
</Fragment> : |
| 84 |
<Placeholder> |
| 85 |
<Spinner /> |
| 86 |
</Placeholder> |
| 87 |
} |
| 88 |
|
| 89 |
</PanelBody> : |
| 90 |
<PanelBody |
| 91 |
title={ __( 'Import from other chart' ) } |
| 92 |
icon="lock" |
| 93 |
initialOpen={ false } |
| 94 |
> |
| 95 |
|
| 96 |
<p>{ __( 'Enable this feature in PRO version!' ) }</p> |
| 97 |
|
| 98 |
<Button |
| 99 |
isPrimary |
| 100 |
href={ visualizerLocalize.proTeaser } |
| 101 |
target="_blank" |
| 102 |
> |
| 103 |
{ __( 'Upgrade Now' ) } |
| 104 |
</Button> |
| 105 |
|
| 106 |
</PanelBody> |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
export default ChartImport; |
| 112 |
|