Skip to content

Commit 33edb88

Browse files
emeaguiarravichdev
authored andcommitted
Toggle auto updates from component
1 parent 23a9b65 commit 33edb88

File tree

7 files changed

+63
-24
lines changed

7 files changed

+63
-24
lines changed

assets/src/settings/components/integrations/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ import { useContext } from '@wordpress/element';
1313
* Internal dependencies
1414
*/
1515
import SettingsContext from '../../context';
16-
import { UPDATERS, ACTIONS } from '../../constants';
16+
import { UPDATERS } from '../../constants';
1717
import Updater from './updater';
1818
import Api from './api';
1919

2020
const Integrations = () => {
21-
const { state, dispatch } = useContext( SettingsContext );
22-
23-
const handleUpdateChange = type => {
24-
dispatch( { type: ACTIONS.TOGGLE_UPDATES, payload: { type } } );
25-
};
21+
const { state } = useContext( SettingsContext );
2622

2723
return (
2824
<div className="material-settings__integrations">
@@ -66,9 +62,6 @@ const Integrations = () => {
6662
checked={ state.updaters[ key ].autoUpdates }
6763
lastUpdated={ state.updaters[ key ].lastUpdated }
6864
type={ UPDATERS[ key ].type }
69-
onChange={ () => {
70-
handleUpdateChange( UPDATERS[ key ].type );
71-
} }
7265
/>
7366
) ) }
7467
</div>

assets/src/settings/components/integrations/updater.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@ import { ACTIONS } from '../../constants';
1717
import SettingsContext from '../../context';
1818
import Switch from './switch';
1919
import Button from '../../../wizard/components/navigation/button';
20-
import { update } from '../../utils';
20+
import { update, toggleAutoUpdate } from '../../utils';
2121

22-
const Updater = ( {
23-
title,
24-
lastUpdated,
25-
needsKey,
26-
checked,
27-
type,
28-
onChange,
29-
} ) => {
22+
const Updater = ( { title, lastUpdated, needsKey, checked, type } ) => {
3023
const [ id ] = useState( _uniqueId( 'updater-' ) );
3124
const { state, dispatch } = useContext( SettingsContext );
3225
const isDisabled = needsKey && 'ok' !== state.apiStatus;
@@ -54,6 +47,11 @@ const Updater = ( {
5447

5548
};
5649

50+
const handleAutoUpdateToggle = () => {
51+
dispatch( { type: ACTIONS.TOGGLE_UPDATES, payload: { type } } );
52+
toggleAutoUpdate( type );
53+
};
54+
5755
return (
5856
<div className="material-settings__updater">
5957
<div className="mdc-layout-grid">
@@ -91,7 +89,11 @@ const Updater = ( {
9189

9290
<div className="mdc-layout-grid__cell mdc-layout-grid__cell--span-2 mdc-layout-grid__cell--align-middle material-settings__cell--justify-end">
9391
{ ! isDisabled && (
94-
<Switch checked={ checked } onChange={ onChange } id={ id } />
92+
<Switch
93+
checked={ checked }
94+
onChange={ handleAutoUpdateToggle }
95+
id={ id }
96+
/>
9597
) }
9698
</div>
9799

assets/src/settings/constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ export const UPDATERS = {
2323
lastUpdated: parseInt( getConfig( 'fontsLastUpdated' ), 10 ),
2424
needsKey: true,
2525
updateAvailable: false,
26-
autoUpdates: false,
26+
autoUpdates: parseInt( getConfig( 'fontsAutoUpdate' ), 10 ),
2727
},
2828
ICONS: {
2929
title: __( 'Material Icons', 'material-design' ),
3030
type: 'ICONS',
3131
lastUpdated: parseInt( getConfig( 'iconsLastUpdated' ), 10 ),
3232
needsKey: false,
3333
updateAvailable: true,
34-
autoUpdates: false,
34+
autoUpdates: parseInt( getConfig( 'iconsAutoUpdate' ), 10 ),
3535
},
3636
};
3737

assets/src/settings/context.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ const initialState = {
2525
error: {},
2626
};
2727

28-
initialState.updaters[ UPDATERS.FONTS.type ].autoUpdates = true;
29-
initialState.updaters[ UPDATERS.ICONS.type ].autoUpdates = false;
30-
3128
/**
3229
* Creates a wrapper in order to use our custom reducer
3330
*

assets/src/settings/utils.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ const updateIcons = () => {
5858
} );
5959
};
6060

61+
/**
62+
* Save API Key in database
63+
*
64+
* @param {string} key API
65+
* @return {Promise} Request response
66+
*/
6167
export const setApiKey = key => {
6268
return new Promise( ( resolve, reject ) => {
6369
apiFetch( {
@@ -72,3 +78,24 @@ export const setApiKey = key => {
7278
.catch( reject );
7379
} );
7480
};
81+
82+
/**
83+
* Turn auto updates on/off
84+
*
85+
* @param {string} type Item to toggle updates
86+
* @return {Promise} Request response
87+
*/
88+
export const toggleAutoUpdate = type => {
89+
return new Promise( ( resolve, reject ) => {
90+
apiFetch( {
91+
path: getConfig( 'assetsRestPath' ) + 'toggle-auto-updates',
92+
method: 'POST',
93+
data: { type },
94+
headers: {
95+
'X-WP-Nonce': getConfig( 'nonce' ),
96+
},
97+
} )
98+
.then( resolve )
99+
.catch( reject );
100+
} );
101+
};

plugin/php/class-admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ public function enqueue_assets() {
303303
'apiStatus' => esc_html( $this->plugin->assets_rest_controller->get_api_status() ),
304304
'fontsLastUpdated' => esc_html( $this->plugin->assets_rest_controller->get_fonts_last_updated() ),
305305
'iconsLastUpdated' => esc_html( $this->plugin->assets_rest_controller->get_icons_last_updated() ),
306+
'fontsAutoUpdate' => esc_html( $this->plugin->assets_rest_controller->get_fonts_auto_update() ),
307+
'iconsAutoUpdate' => esc_html( $this->plugin->assets_rest_controller->get_icons_auto_update() ),
306308
]
307309
);
308310
}

plugin/php/class-design-assets-rest-controller.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,24 @@ public function get_icons_last_updated() {
297297
return absint( Update_Icons::get_last_updated() ) * 1000;
298298
}
299299

300+
/**
301+
* Return auto update from database
302+
*
303+
* @return int Auto update option
304+
*/
305+
public function get_fonts_auto_update() {
306+
return absint( get_option( Update_Fonts::AUTO_UPDATE_SLUG ) );
307+
}
308+
309+
/**
310+
* Return auto update from database
311+
*
312+
* @return int Auto update option
313+
*/
314+
public function get_icons_auto_update() {
315+
return absint( get_option( Update_Icons::AUTO_UPDATE_SLUG ) );
316+
}
317+
300318
/**
301319
* Look for API in database.
302320
*

0 commit comments

Comments
 (0)