diff options
Diffstat (limited to 'plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store')
17 files changed, 389 insertions, 0 deletions
diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/index.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/index.js new file mode 100644 index 00000000..51cfcd8b --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/index.js @@ -0,0 +1,12 @@ +/** + * Internal dependencies + */ +import siteSettingActions from './jetpack-settings'; +import noticeActions from 'components/global-notices/store/actions'; + +const actions = { + ...siteSettingActions, + ...noticeActions, +}; + +export default actions; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/jetpack-settings.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/jetpack-settings.js new file mode 100644 index 00000000..35b07ac5 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/jetpack-settings.js @@ -0,0 +1,81 @@ +/** + * External dependencies + */ +/*eslint lodash/import-scope: [2, "method"]*/ +import pick from 'lodash/pick'; +import { select } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import { + fetchWordAdsSettings, + updateWordAdsSettings as updateJetpackSettingsControl, +} from '../controls'; +import { + removeUpdatingNotice, + updatingNotice, + errorNotice, + successNotice, +} from 'components/global-notices/store/actions'; +import { STORE_ID } from '../../store'; + +export const SET_WORDADS_SETTINGS = 'SET_WORDADS_SETTINGS'; +export const TOGGLE_WORDADS_MODULE = 'TOGGLE_WORDADS_MODULE'; + +/** + * Yield actions to update WordAds Settings + * + * @param {object} settings - settings to apply. + * @yields {object} - an action object. + * @returns {object} - an action object. + */ +export function* updateJetpackSettings( settings ) { + try { + yield updatingNotice(); + yield setUpdatingJetpackSettings(); + yield setJetpackSettings( settings ); + yield updateJetpackSettingsControl( settings ); + const updatedSettings = yield fetchWordAdsSettings(); + yield setJetpackSettings( updatedSettings ); + return successNotice( __( 'Updated settings.', 'jetpack-wordads' ) ); + } catch ( e ) { + const oldSettings = pick( select( STORE_ID ).getWordAdsModuleStatus(), [ 'module_active' ] ); + yield setJetpackSettings( oldSettings ); + return errorNotice( __( 'Error Update settings…', 'jetpack-wordads' ) ); + } finally { + yield removeUpdatingNotice(); + yield setUpdatingJetpackSettingsDone(); + } +} + +/** + * Set state updating action + * + * @returns {object} - an action object. + */ +export function setUpdatingJetpackSettings() { + return setJetpackSettings( { is_updating: true } ); +} + +/** + * Set state updating finished + * + * @returns {object} - an action object. + */ +export function setUpdatingJetpackSettingsDone() { + return setJetpackSettings( { is_updating: false } ); +} + +/** + * Set Jetpack settings action + * + * @param {object} options - Jetpack settings. + * @returns {object} - an action object. + */ +export function setJetpackSettings( options ) { + return { type: SET_WORDADS_SETTINGS, options }; +} + +export default { updateJetpackSettings, setJetpackSettings }; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/test/jetpack-settings.test.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/test/jetpack-settings.test.js new file mode 100644 index 00000000..b0f3e302 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/actions/test/jetpack-settings.test.js @@ -0,0 +1,31 @@ +/** + * @jest-environment jsdom + */ +/** + * Internal dependencies + */ +import { updateJetpackSettings } from '../jetpack-settings'; + +describe( 'Jetpack Settings updateJetpackSettings action', () => { + const action = updateJetpackSettings( undefined, undefined ); + test( 'yield setJetpackSettings state to new one', () => { + // Create notice 'Updating'. + expect( action.next().value.type ).toEqual( 'CREATE_NOTICE' ); + // Set state updating flag. + expect( action.next().value.type ).toEqual( 'SET_WORDADS_SETTINGS' ); + // Set state to the target state. + expect( action.next().value.type ).toEqual( 'SET_WORDADS_SETTINGS' ); + // Post new settings to API. + expect( action.next().value.type ).toEqual( 'UPDATE_WORDADS_SETTINGS' ); + // Fetch settings from API. + expect( action.next().value.type ).toEqual( 'FETCH_WORDADS_SETTINGS' ); + // Set fetched setting from above step. + expect( action.next().value.type ).toEqual( 'SET_WORDADS_SETTINGS' ); + // Remove 'Updating' notice. + expect( action.next().value.type ).toEqual( 'REMOVE_NOTICE' ); + // Remove state updating flag. + expect( action.next().value.type ).toEqual( 'SET_WORDADS_SETTINGS' ); + // Create success notice. + expect( action.next().value.type ).toEqual( 'CREATE_NOTICE' ); + } ); +} ); diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/controls.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/controls.js new file mode 100644 index 00000000..a74f0a79 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/controls.js @@ -0,0 +1,40 @@ +/** + * Internal dependencies + */ +import restApi from '@automattic/jetpack-api'; + +export const FETCH_WORDADS_SETTINGS = 'FETCH_WORDADS_SETTINGS'; +export const UPDATE_WORDADS_SETTINGS = 'UPDATE_WORDADS_SETTINGS'; + +/** + * fetchJetpackSettings action + * + * @returns {object} - an action object. + */ +export const fetchWordAdsSettings = () => { + return { + type: FETCH_WORDADS_SETTINGS, + }; +}; + +/** + * updateJetpackSettings action + * + * @param {*} settings - Jetpack settings object. + * @returns {object} - an action object. + */ +export const updateWordAdsSettings = settings => { + return { + type: UPDATE_WORDADS_SETTINGS, + settings, + }; +}; + +export default { + [ FETCH_WORDADS_SETTINGS ]: function () { + return restApi.fetchWordAdsSettings(); + }, + [ UPDATE_WORDADS_SETTINGS ]: function ( action ) { + return restApi.updateWordAdsSettings( action.settings ); + }, +}; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/index.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/index.js new file mode 100644 index 00000000..bc2f5bd8 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/index.js @@ -0,0 +1,18 @@ +/** + * Internal dependencies + */ +import reducer from './reducer'; +import actions from './actions'; +import selectors from './selectors'; +import resolvers from './resolvers'; +import controls from './controls'; + +export const STORE_ID = 'jetpack-wordads-plugin'; +export const storeConfig = { + reducer, + actions, + selectors, + resolvers, + controls, + initialState: window.WORDADS_DASHBOARD_INITIAL_STATE || {}, +}; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/feature.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/feature.js new file mode 100644 index 00000000..f48b1801 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/feature.js @@ -0,0 +1,5 @@ +const features = ( state = [] ) => { + return state; +}; + +export default features; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/index.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/index.js new file mode 100644 index 00000000..0c4f50a1 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/index.js @@ -0,0 +1,23 @@ +/** + * WordPress dependencies + */ +import { combineReducers } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import siteData from './site-data'; +import userData from './user-data'; +import jetpackSettings from './jetpack-settings'; +import features from './feature'; +import notices from 'components/global-notices/store/reducer'; + +const reducer = combineReducers( { + siteData, + jetpackSettings, + userData, + features, + notices, +} ); + +export default reducer; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/jetpack-settings.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/jetpack-settings.js new file mode 100644 index 00000000..407d5e5b --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/jetpack-settings.js @@ -0,0 +1,22 @@ +/** + * Internal dependencies + */ +import { SET_WORDADS_SETTINGS } from '../actions/jetpack-settings'; + +const jetpackSettings = ( state = {}, action ) => { + switch ( action.type ) { + case SET_WORDADS_SETTINGS: + return { + ...state, + ...action.options, + is_toggling_module: + state.module_active !== action.options.module_active && !! action.options.is_updating, + is_toggling_instant_search: + state.instant_search_enabled !== action.options.instant_search_enabled && + !! action.options.is_updating, + }; + } + return state; +}; + +export default jetpackSettings; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/site-data.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/site-data.js new file mode 100644 index 00000000..8e1a578d --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/site-data.js @@ -0,0 +1,5 @@ +const siteData = ( state = {} ) => { + return state; +}; + +export default siteData; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/test/jetpack-settings.test.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/test/jetpack-settings.test.js new file mode 100644 index 00000000..61472997 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/test/jetpack-settings.test.js @@ -0,0 +1,66 @@ +/** + * @jest-environment jsdom + */ +/** + * Internal dependencies + */ +import { setJetpackSettings } from '../../actions/jetpack-settings'; +import reducer from '../jetpack-settings'; + +describe( 'Jetpack Settings Reducer', () => { + const initState = { + module_active: true, + instant_search_enabled: false, + is_updating: false, + }; + test( 'defaults to empty', () => { + const state = reducer( undefined, {} ); + expect( state ).toEqual( {} ); + } ); + test( 'init set jetpackSettings', () => { + const expected = { + module_active: true, + instant_search_enabled: false, + is_toggling_instant_search: false, + is_toggling_module: false, + }; + const newSettings = { + module_active: true, + instant_search_enabled: false, + }; + const state = reducer( undefined, setJetpackSettings( newSettings ) ); + expect( state ).toEqual( expected ); + } ); + test( 'toggle instant search', () => { + const newSettings = { + module_active: true, + instant_search_enabled: true, + is_updating: true, + }; + const expected = { + module_active: true, + instant_search_enabled: true, + is_toggling_instant_search: true, + is_toggling_module: false, + is_updating: true, + }; + const state = reducer( initState, setJetpackSettings( newSettings ) ); + expect( state ).toEqual( expected ); + } ); + test( 'toggle search', () => { + const newSettings = { + module_active: false, + instant_search_enabled: false, + is_updating: true, + }; + const expected = { + module_active: false, + instant_search_enabled: false, + is_toggling_instant_search: false, + is_toggling_module: true, + is_updating: true, + }; + const state = reducer( initState, setJetpackSettings( newSettings ) ); + expect( state ).toEqual( expected ); + } ); +} ); diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/user-data.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/user-data.js new file mode 100644 index 00000000..17af1898 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/reducer/user-data.js @@ -0,0 +1,5 @@ +const userData = ( state = {} ) => { + return state; +}; + +export default userData; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/resolvers.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/resolvers.js new file mode 100644 index 00000000..ed7fefdd --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/resolvers.js @@ -0,0 +1,30 @@ +/** + * External dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import { fetchWordAdsSettings } from './controls'; +import { setJetpackSettings } from './actions/jetpack-settings'; +import { errorNotice } from '../components/global-notices/store/actions'; + +/** + * Yield actions to get Search Module Status + * + * @yields {object} - an action object. + * @returns {object} - an action object. + */ +export function* getWordAdsModuleStatus() { + try { + const settings = yield fetchWordAdsSettings(); + if ( settings ) { + return setJetpackSettings( settings ); + } + } catch ( e ) { + return errorNotice( __( 'Error fetching settings…', 'jetpack-wordads' ) ); + } +} + +export default { getWordAdsModuleStatus }; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/feature.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/feature.js new file mode 100644 index 00000000..b9beed07 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/feature.js @@ -0,0 +1,7 @@ +const featureSelectors = { + isFeatureEnabled: ( state, feature ) => { + return Array.isArray( state.features ) && state.features.includes( feature ); + }, +}; + +export default featureSelectors; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/index.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/index.js new file mode 100644 index 00000000..6c0b30e1 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/index.js @@ -0,0 +1,18 @@ +/** + * Internal dependencies + */ +import siteDataSelectors from './site-data'; +import jetpackSettingSelectors from './jetpack-settings'; +import userDataSelectors from './user-data'; +import noticeSelectors from 'components/global-notices/store/selectors'; +import featureSelectors from './feature'; + +const selectors = { + ...siteDataSelectors, + ...jetpackSettingSelectors, + ...userDataSelectors, + ...noticeSelectors, + ...featureSelectors, +}; + +export default selectors; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/jetpack-settings.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/jetpack-settings.js new file mode 100644 index 00000000..546a5c67 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/jetpack-settings.js @@ -0,0 +1,10 @@ +const jetpackSettingSelectors = { + getWordAdsModuleStatus: state => state.jetpackSettings, + isModuleEnabled: state => state.jetpackSettings.module_active, + isInstantSearchEnabled: state => state.jetpackSettings.instant_search_enabled, + isUpdatingJetpackSettings: state => state.jetpackSettings.is_updating, + isTogglingModule: state => state.jetpackSettings.is_toggling_module, + isTogglingInstantSearch: state => state.jetpackSettings.is_toggling_instant_search, +}; + +export default jetpackSettingSelectors; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/site-data.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/site-data.js new file mode 100644 index 00000000..6bd46a8a --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/site-data.js @@ -0,0 +1,11 @@ +const siteDataSelectors = { + getAPIRootUrl: state => state.siteData?.WP_API_root ?? null, + getAPINonce: state => state.siteData?.WP_API_nonce ?? null, + getRegistrationNonce: state => state.siteData?.registrationNonce ?? null, + getSiteAdminUrl: state => state.siteData?.adminUrl ?? null, + getBlogId: state => state.siteData?.blogId ?? 0, + getVersion: state => state.siteData?.version ?? 'development', + getCalypsoSlug: state => state.siteData?.calypsoSlug, +}; + +export default siteDataSelectors; diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/user-data.js b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/user-data.js new file mode 100644 index 00000000..6e5ac9d3 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-wordads/src/dashboard/store/selectors/user-data.js @@ -0,0 +1,5 @@ +const userDataSelectors = { + getWpcomUser: state => state.userData?.currentUser?.wpcomUser, +}; + +export default userDataSelectors; |