-
Notifications
You must be signed in to change notification settings - Fork 799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jetpack Modules redux store to shared utils #33397
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9ba7d40
update: use redux store to have consistent state over the app
andrii-lysenko da9bea4
add changelog
andrii-lysenko c00f4a6
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 90ab566
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko b564144
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 45ef413
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 5ef2476
update: fixup versions
andrii-lysenko 74b1ca1
update: use hook in AI assistant HOC
andrii-lysenko 6aed12b
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 45bc567
update lock file
andrii-lysenko ec0e276
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko ba7712b
remove commented code
andrii-lysenko 80ca90b
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 84b9fcf
Initialize JS globals.
sergeymitr b513148
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
sergeymitr 1d56b94
Add Connection initial state to the Search package tests.
sergeymitr 2f9b05f
Bump package versions.
sergeymitr 131ef63
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 6669283
fix: remove unnecesary select
andrii-lysenko e766fae
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 1ddd1cf
update: use proper action for first modules load
andrii-lysenko d35cc32
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko b8d5213
update: placeholder wording
andrii-lysenko bdc778a
fix namings and add Readme
andrii-lysenko b5cfe1d
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 2486741
updated lock file
andrii-lysenko 9afb558
fix: always enabled modules for Simple sites
andrii-lysenko 8145f7a
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 87cccdc
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 61e6359
fix pnpm file
andrii-lysenko 574e8b4
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko 8611e0f
update: changelog versions
andrii-lysenko d931ea9
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko e915a3e
update: jsdoc
andrii-lysenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/js-packages/shared-extension-utils/changelog/fix-ai-assistant-form-blocks-sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Adding redux store for jetpack modules data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
projects/js-packages/shared-extension-utils/src/modules-state/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { select } from '@wordpress/data'; | ||
import { isSimpleSite } from '../site-type-utils'; | ||
import { | ||
fetchJetpackModules, | ||
updateJetpackModuleStatus as updateJetpackModuleStatusControl, | ||
} from './controls'; | ||
import { JETPACK_MODULES_STORE_ID } from '.'; | ||
|
||
export const SET_JETPACK_MODULES = 'SET_JETPACK_MODULES'; | ||
|
||
/** | ||
* Yield actions to update module status | ||
* | ||
* @param {object} settings - Jetpack module settings. | ||
* @param {string} settings.name - Jetpack module name. | ||
* @param {boolean} settings.active - If the module is active or not. | ||
* @yields {object} - an action object. | ||
* @returns {object} - an action object. | ||
*/ | ||
export function* updateJetpackModuleStatus( settings ) { | ||
try { | ||
const originalData = select( JETPACK_MODULES_STORE_ID ).getJetpackModules(); | ||
yield setIsUpdating( true ); | ||
if ( originalData.data?.[ settings.name ]?.activated !== settings.active ) { | ||
yield setJetpackModules( originalData ); | ||
} | ||
yield updateJetpackModuleStatusControl( settings ); | ||
const data = yield fetchJetpackModules(); | ||
yield setJetpackModules( { data } ); | ||
return true; | ||
} catch ( e ) { | ||
const oldSettings = select( JETPACK_MODULES_STORE_ID ).getJetpackModules(); | ||
yield setJetpackModules( oldSettings ); | ||
return false; | ||
} finally { | ||
yield setIsUpdating( false ); | ||
} | ||
} | ||
|
||
/** | ||
* Yield actions to update module status | ||
* @yields {object} - an action object. | ||
* @returns {boolean} - if operation is successful or not. | ||
*/ | ||
export function* fetchModules() { | ||
// We don't fetch modules for Simple Site and aknowledge that all modules are active | ||
if ( isSimpleSite() ) { | ||
return true; | ||
} | ||
try { | ||
yield setIsLoading( true ); | ||
const data = yield fetchJetpackModules(); | ||
yield setJetpackModules( { data } ); | ||
return true; | ||
} catch ( e ) { | ||
const oldSettings = select( JETPACK_MODULES_STORE_ID ).getJetpackModules(); | ||
yield setJetpackModules( oldSettings ); | ||
return false; | ||
} finally { | ||
yield setIsLoading( false ); | ||
} | ||
} | ||
|
||
/** | ||
* Set modules as loading action | ||
* | ||
* @param {boolean} isLoading - If the modules are loading or not. | ||
* @returns {object} - an action object. | ||
*/ | ||
function setIsLoading( isLoading ) { | ||
return setJetpackModules( { isLoading } ); | ||
} | ||
|
||
/** | ||
* Set modules as updating action | ||
* | ||
* @param {boolean} isUpdating - If the modules are updating or not. | ||
* @returns {object} - an action object. | ||
*/ | ||
function setIsUpdating( isUpdating ) { | ||
return setJetpackModules( { isUpdating } ); | ||
} | ||
|
||
/** | ||
* Set Jetpack module action | ||
* | ||
* @param {object} options - Jetpack settings. | ||
* @param {object} options.modules - Jetpack modules. | ||
* @param {boolean} options.isLoading - If the modules are loading or not. | ||
* @returns {object} - an action object. | ||
*/ | ||
export function setJetpackModules( options ) { | ||
return { type: SET_JETPACK_MODULES, options }; | ||
} | ||
|
||
export default { updateJetpackModuleStatus, setJetpackModules, fetchModules }; |
48 changes: 48 additions & 0 deletions
48
projects/js-packages/shared-extension-utils/src/modules-state/controls.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
export const FETCH_JETPACK_MODULES = 'FETCH_JETPACK_MODULES'; | ||
export const UPDATE_JETPACK_MODULE_STATUS = 'UPDATE_JETPACK_MODULE_STATUS'; | ||
|
||
/** | ||
* fetchJetpackModules action | ||
* | ||
* @returns {object} - an action object. | ||
*/ | ||
export const fetchJetpackModules = () => { | ||
return { | ||
type: FETCH_JETPACK_MODULES, | ||
}; | ||
}; | ||
|
||
/** | ||
* Updating single module status action | ||
* | ||
* @param settings - Jetpack module settings. | ||
* @param {string} settings.name - Jetpack module name. | ||
* @param {boolean} settings.active - If the module is active or not. | ||
*/ | ||
|
||
export const updateJetpackModuleStatus = settings => { | ||
return { | ||
type: UPDATE_JETPACK_MODULE_STATUS, | ||
settings, | ||
}; | ||
}; | ||
|
||
export default { | ||
[ FETCH_JETPACK_MODULES ]: function () { | ||
return apiFetch( { | ||
path: `/jetpack/v4/module/all`, | ||
method: 'GET', | ||
} ); | ||
}, | ||
[ UPDATE_JETPACK_MODULE_STATUS ]: function ( { settings } ) { | ||
return apiFetch( { | ||
path: `/jetpack/v4/module/${ settings.name }/active`, | ||
method: 'POST', | ||
data: { | ||
active: settings.active, | ||
}, | ||
} ); | ||
}, | ||
}; |
17 changes: 17 additions & 0 deletions
17
projects/js-packages/shared-extension-utils/src/modules-state/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createReduxStore, register } from '@wordpress/data'; | ||
import actions from './actions'; | ||
import controls from './controls'; | ||
import reducer from './reducer'; | ||
import resolvers from './resolvers'; | ||
import selectors from './selectors'; | ||
|
||
export const JETPACK_MODULES_STORE_ID = 'jetpack-modules'; | ||
|
||
const store = createReduxStore( JETPACK_MODULES_STORE_ID, { | ||
reducer, | ||
actions, | ||
controls, | ||
resolvers, | ||
selectors, | ||
} ); | ||
register( store ); |
18 changes: 18 additions & 0 deletions
18
projects/js-packages/shared-extension-utils/src/modules-state/reducer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const defaultState = { | ||
isLoading: false, | ||
isUpdating: false, | ||
data: {}, | ||
}; | ||
|
||
const setModulesData = ( state = defaultState, action ) => { | ||
switch ( action.type ) { | ||
case 'SET_JETPACK_MODULES': | ||
return { | ||
...state, | ||
...action.options, | ||
}; | ||
} | ||
return state; | ||
}; | ||
|
||
export default setModulesData; |
30 changes: 30 additions & 0 deletions
30
projects/js-packages/shared-extension-utils/src/modules-state/resolvers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { setJetpackModules, fetchModules } from './actions'; | ||
import { fetchJetpackModules } from './controls'; | ||
|
||
/** | ||
* Yield actions to get the Jetpack modules. | ||
* | ||
* @yields {object} - an action object. | ||
* @returns {object} - an action object. | ||
*/ | ||
export function* getJetpackModules() { | ||
try { | ||
const data = yield fetchJetpackModules(); | ||
if ( data ) { | ||
return setJetpackModules( { data } ); | ||
} | ||
} catch ( e ) { | ||
console.error( e ); // eslint-disable-line no-console | ||
} | ||
} | ||
|
||
/** | ||
* When requesting data on particular module | ||
* we want to make sure to have the latest state | ||
* @returns {boolean} - if action was completed successfully. | ||
*/ | ||
export function isModuleActive() { | ||
return fetchModules(); | ||
} | ||
|
||
export default { getJetpackModules, isModuleActive }; |
13 changes: 13 additions & 0 deletions
13
projects/js-packages/shared-extension-utils/src/modules-state/selectors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { isSimpleSite } from '../site-type-utils'; | ||
|
||
const jetpackModulesSelectors = { | ||
getJetpackModules: state => state.data, | ||
// We consider simple sites to have all modules active | ||
// TODO: we would remove this when wrapping logic with hooks | ||
isModuleActive: ( state, moduleName ) => | ||
isSimpleSite() || ( state?.data?.[ moduleName ]?.activated ?? false ), | ||
areModulesLoading: state => state.isLoading ?? false, | ||
areModulesUpdating: state => state.isUpdating ?? false, | ||
}; | ||
|
||
export default jetpackModulesSelectors; |
4 changes: 4 additions & 0 deletions
4
projects/packages/forms/changelog/fix-ai-assistant-form-blocks-sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: changed | ||
|
||
Change useModuleActive to use redux store instead from Jetpack Modules store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it return an object instead, to match the expected return?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good catch. Though, it's error in JSDoc, it should
yield
object but returnsbool
.