Skip to content

Commit 041d720

Browse files
committed
fix: only fetch settings once; use the fetchSettings util
1 parent a81cf9d commit 041d720

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/deprecated/v2/welcome/admin.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@wordpress/element'
88
import { send as ajaxSend } from '@wordpress/ajax'
99
import { Spinner } from '@wordpress/components'
10-
import { loadPromise, models } from '@wordpress/api'
10+
import { models } from '@wordpress/api'
1111

1212
/**
1313
* External dependencies
@@ -16,6 +16,7 @@ import {
1616
i18n,
1717
v2nonce as nonce,
1818
} from 'stackable'
19+
import { fetchSettings } from '~stackable/util/admin'
1920
import AdminToggleSetting from '~stackable/components/admin-toggle-setting'
2021

2122
export class BlockToggler extends Component {
@@ -116,11 +117,8 @@ export const OptimizationSettings = ( { searchSettings } ) => {
116117
const [ optimizeScriptLoad, setOptimizeScriptLoad ] = useState( false )
117118

118119
useEffect( () => {
119-
loadPromise.then( () => {
120-
const settings = new models.Settings()
121-
settings.fetch().then( response => {
122-
setOptimizeScriptLoad( !! response.stackable_optimize_script_load )
123-
} )
120+
fetchSettings().then( response => {
121+
setOptimizeScriptLoad( !! response.stackable_optimize_script_load )
124122
} )
125123
}, [] )
126124

src/welcome/admin.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import domReady from '@wordpress/dom-ready'
1717
import {
1818
Button, Flex, FlexItem, Spinner, CheckboxControl, Modal,
1919
} from '@wordpress/components'
20-
import { loadPromise, models } from '@wordpress/api'
20+
import { models } from '@wordpress/api'
2121
import { applyFilters } from '@wordpress/hooks'
2222

2323
/**
@@ -31,7 +31,7 @@ import {
3131
defaultBreakpoints,
3232
} from 'stackable'
3333
import classnames from 'classnames'
34-
import { importBlocks } from '~stackable/util/admin'
34+
import { fetchSettings, importBlocks } from '~stackable/util/admin'
3535
import { createRoot } from '~stackable/util/element'
3636
import AdminSelectSetting from '~stackable/components/admin-select-setting'
3737
import AdminToggleSetting from '~stackable/components/admin-toggle-setting'
@@ -309,11 +309,8 @@ const RestSettingsNotice = () => {
309309
const [ error, setError ] = useState( null )
310310

311311
useEffect( () => {
312-
loadPromise.then( () => {
313-
const settings = new models.Settings()
314-
settings.fetch().catch( error => {
315-
setError( error )
316-
} )
312+
fetchSettings().catch( error => {
313+
setError( error )
317314
} )
318315
}, [] )
319316

@@ -536,13 +533,10 @@ const Settings = () => {
536533
}, [ unsavedChanges, settings ] )
537534

538535
useEffect( () => {
539-
loadPromise.then( () => {
540-
const settings = new models.Settings()
541-
settings.fetch().then( response => {
542-
setSettings( response )
543-
// Should only be set initially since we have to reload after setting for it to work with the backend
544-
setHasV2Tab( hasV2Compatibility( response ) )
545-
} )
536+
fetchSettings().then( response => {
537+
setSettings( response )
538+
// Should only be set initially since we have to reload after setting for it to work with the backend
539+
setHasV2Tab( hasV2Compatibility( response ) )
546540
} )
547541
}, [] )
548542

@@ -830,12 +824,12 @@ const Responsiveness = props => {
830824
label={ __( 'Tablet Breakpoint', i18n ) }
831825
searchedSettings={ dynamicBreakpoints.children }
832826
type="number"
833-
value={ settings.stackable_dynamic_breakpoints.tablet || '' } // eslint-disable-line camelcase
827+
value={ settings.stackable_dynamic_breakpoints?.tablet || '' } // eslint-disable-line camelcase
834828
onChange={ value => {
835829
handleSettingsChange( {
836830
stackable_dynamic_breakpoints: { // eslint-disable-line camelcase
837831
tablet: value,
838-
mobile: settings.stackable_dynamic_breakpoints.mobile || '', // eslint-disable-line camelcase
832+
mobile: settings.stackable_dynamic_breakpoints?.mobile || '', // eslint-disable-line camelcase
839833
},
840834
} )
841835
} }
@@ -845,11 +839,11 @@ const Responsiveness = props => {
845839
label={ __( 'Mobile Breakpoint', i18n ) }
846840
searchedSettings={ dynamicBreakpoints.children }
847841
type="number"
848-
value={ settings.stackable_dynamic_breakpoints.mobile || '' } // eslint-disable-line camelcase
842+
value={ settings.stackable_dynamic_breakpoints?.mobile || '' } // eslint-disable-line camelcase
849843
onChange={ value => {
850844
handleSettingsChange( {
851845
stackable_dynamic_breakpoints: { // eslint-disable-line camelcase
852-
tablet: settings.stackable_dynamic_breakpoints.tablet || '', // eslint-disable-line camelcase
846+
tablet: settings.stackable_dynamic_breakpoints?.tablet || '', // eslint-disable-line camelcase
853847
mobile: value,
854848
},
855849
} )

0 commit comments

Comments
 (0)