blocks
4 years ago
components
4 years ago
shared
4 years ago
utils
4 years ago
blocks.js
5 years ago
dashboard.js
4 years ago
dashboard.scss
5 years ago
dashboard.js
219 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { |
| 5 | __, |
| 6 | } from '@wordpress/i18n'; |
| 7 | |
| 8 | import { |
| 9 | BaseControl, |
| 10 | Button, |
| 11 | PanelBody, |
| 12 | PanelRow, |
| 13 | Placeholder, |
| 14 | Spinner, |
| 15 | ToggleControl, |
| 16 | SelectControl, |
| 17 | } from '@wordpress/components'; |
| 18 | |
| 19 | import { |
| 20 | render, |
| 21 | Component, |
| 22 | Fragment, |
| 23 | } from '@wordpress/element'; |
| 24 | |
| 25 | import apiFetch from '@wordpress/api-fetch'; |
| 26 | |
| 27 | import { |
| 28 | applyFilters, |
| 29 | } from '@wordpress/hooks'; |
| 30 | |
| 31 | /** |
| 32 | * Internal dependencies |
| 33 | */ |
| 34 | import './dashboard.scss'; |
| 35 | |
| 36 | class App extends Component { |
| 37 | constructor() { |
| 38 | super( ...arguments ); |
| 39 | |
| 40 | this.state = { |
| 41 | isAPILoaded: false, |
| 42 | isAPISaving: false, |
| 43 | isRegeneratingCSS: false, |
| 44 | settings: generateBlocksSettings.settings, |
| 45 | }; |
| 46 | |
| 47 | this.getSetting = this.getSetting.bind( this ); |
| 48 | this.updateSettings = this.updateSettings.bind( this ); |
| 49 | } |
| 50 | |
| 51 | componentDidMount() { |
| 52 | this.setState( { |
| 53 | isAPILoaded: true, |
| 54 | } ); |
| 55 | } |
| 56 | |
| 57 | getSetting( name, defaultVal ) { |
| 58 | let result = defaultVal; |
| 59 | |
| 60 | if ( 'undefined' !== typeof this.state.settings[ name ] ) { |
| 61 | result = this.state.settings[ name ]; |
| 62 | } |
| 63 | |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | updateSettings( e ) { |
| 68 | this.setState( { isAPISaving: true } ); |
| 69 | const message = e.target.nextElementSibling; |
| 70 | |
| 71 | apiFetch( { |
| 72 | path: '/generateblocks/v1/settings', |
| 73 | method: 'POST', |
| 74 | data: { |
| 75 | settings: this.state.settings, |
| 76 | }, |
| 77 | } ).then( ( result ) => { |
| 78 | this.setState( { isAPISaving: false } ); |
| 79 | message.classList.add( 'gblocks-action-message--show' ); |
| 80 | message.textContent = result.response; |
| 81 | |
| 82 | if ( ! result.success || ! result.response ) { |
| 83 | message.classList.add( 'gblocks-action-message--error' ); |
| 84 | } else { |
| 85 | setTimeout( function() { |
| 86 | message.classList.remove( 'gblocks-action-message--show' ); |
| 87 | }, 3000 ); |
| 88 | } |
| 89 | } ); |
| 90 | } |
| 91 | |
| 92 | render() { |
| 93 | if ( ! this.state.isAPILoaded ) { |
| 94 | return ( |
| 95 | <Placeholder className="gblocks-settings-placeholder"> |
| 96 | <Spinner /> |
| 97 | </Placeholder> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return ( |
| 102 | <Fragment> |
| 103 | <div className="generateblocks-settings-main"> |
| 104 | { applyFilters( 'generateblocks.dashboard.beforeSettings', '', this ) } |
| 105 | |
| 106 | <PanelBody |
| 107 | title={ __( 'Settings', 'generateblocks' ) } |
| 108 | > |
| 109 | <div className="gblocks-dashboard-panel-row-wrapper"> |
| 110 | <PanelRow className="gblocks-css-print-method"> |
| 111 | <SelectControl |
| 112 | label={ __( 'CSS Print Method', 'generateblocks' ) } |
| 113 | help={ __( 'Generating your CSS in external files is better for overall performance.', 'generateblocks' ) } |
| 114 | value={ this.getSetting( 'css_print_method' ) } |
| 115 | options={ [ |
| 116 | { label: __( 'External File', 'generateblocks' ), value: 'file' }, |
| 117 | { label: __( 'Inline Embedding', 'generateblocks' ), value: 'inline' }, |
| 118 | ] } |
| 119 | onChange={ ( value ) => { |
| 120 | this.setState( { |
| 121 | settings: { |
| 122 | ...this.state.settings, |
| 123 | css_print_method: value, |
| 124 | }, |
| 125 | } ); |
| 126 | } } |
| 127 | /> |
| 128 | </PanelRow> |
| 129 | |
| 130 | { 'file' === this.getSetting( 'css_print_method' ) && |
| 131 | <PanelRow> |
| 132 | <BaseControl |
| 133 | id="gblocks-regenerate-css" |
| 134 | className="gblocks-regenerate-css" |
| 135 | help={ __( 'Force your external CSS files to regenerate next time their page is loaded.', 'generateblocks' ) } |
| 136 | > |
| 137 | <div className="gblocks-action-button"> |
| 138 | <Button |
| 139 | isSecondary |
| 140 | onClick={ ( e ) => { |
| 141 | this.setState( { isRegeneratingCSS: true } ); |
| 142 | const message = e.target.nextElementSibling; |
| 143 | |
| 144 | apiFetch( { |
| 145 | path: '/generateblocks/v1/regenerate_css_files', |
| 146 | method: 'POST', |
| 147 | } ).then( ( result ) => { |
| 148 | this.setState( { isRegeneratingCSS: false } ); |
| 149 | message.classList.add( 'gblocks-action-message--show' ); |
| 150 | message.textContent = result.response; |
| 151 | |
| 152 | if ( ! result.success || ! result.response ) { |
| 153 | message.classList.add( 'gblocks-action-message--error' ); |
| 154 | } else { |
| 155 | setTimeout( function() { |
| 156 | message.classList.remove( 'gblocks-action-message--show' ); |
| 157 | }, 3000 ); |
| 158 | } |
| 159 | } ); |
| 160 | } } |
| 161 | > |
| 162 | { this.state.isRegeneratingCSS && <Spinner /> } |
| 163 | { ! this.state.isRegeneratingCSS && __( 'Regenerate CSS Files', 'generateblocks' ) } |
| 164 | </Button> |
| 165 | |
| 166 | <span className="gblocks-action-message"></span> |
| 167 | </div> |
| 168 | </BaseControl> |
| 169 | </PanelRow> |
| 170 | } |
| 171 | |
| 172 | <PanelRow> |
| 173 | <ToggleControl |
| 174 | label={ __( 'Sync Responsive Previews', 'generateblocks' ) } |
| 175 | help={ __( 'Sync our responsive preview controls with the editor responsive previews.', 'generateblocks' ) } |
| 176 | checked={ this.getSetting( 'sync_responsive_previews' ) } |
| 177 | onChange={ ( value ) => { |
| 178 | this.setState( { |
| 179 | settings: { |
| 180 | ...this.state.settings, |
| 181 | sync_responsive_previews: value, |
| 182 | }, |
| 183 | } ); |
| 184 | } } |
| 185 | /> |
| 186 | </PanelRow> |
| 187 | |
| 188 | { applyFilters( 'generateblocks.dashboard.settings', '', this ) } |
| 189 | |
| 190 | <div className="gblocks-action-button"> |
| 191 | <Button |
| 192 | isPrimary |
| 193 | disabled={ this.state.isAPISaving } |
| 194 | onClick={ ( e ) => this.updateSettings( e ) } |
| 195 | > |
| 196 | { this.state.isAPISaving && <Spinner /> } |
| 197 | { ! this.state.isAPISaving && __( 'Save' ) } |
| 198 | </Button> |
| 199 | |
| 200 | <span className="gblocks-action-message"></span> |
| 201 | </div> |
| 202 | </div> |
| 203 | </PanelBody> |
| 204 | |
| 205 | { applyFilters( 'generateblocks.dashboard.afterSettings', '', this ) } |
| 206 | </div> |
| 207 | </Fragment> |
| 208 | ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // eslint-disable-next-line @wordpress/no-global-event-listener |
| 213 | window.addEventListener( 'DOMContentLoaded', () => { |
| 214 | render( |
| 215 | <App />, |
| 216 | document.getElementById( 'gblocks-block-default-settings' ) |
| 217 | ); |
| 218 | } ); |
| 219 |