Skip to content
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 34 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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 Sep 29, 2023
da9bea4
add changelog
andrii-lysenko Sep 29, 2023
c00f4a6
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Sep 29, 2023
90ab566
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 11, 2023
b564144
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 11, 2023
45ef413
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 11, 2023
5ef2476
update: fixup versions
andrii-lysenko Oct 11, 2023
74b1ca1
update: use hook in AI assistant HOC
andrii-lysenko Oct 11, 2023
6aed12b
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 11, 2023
45bc567
update lock file
andrii-lysenko Oct 11, 2023
ec0e276
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 12, 2023
ba7712b
remove commented code
andrii-lysenko Oct 13, 2023
80ca90b
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 13, 2023
84b9fcf
Initialize JS globals.
sergeymitr Oct 16, 2023
b513148
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
sergeymitr Oct 16, 2023
1d56b94
Add Connection initial state to the Search package tests.
sergeymitr Oct 16, 2023
2f9b05f
Bump package versions.
sergeymitr Oct 16, 2023
131ef63
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 17, 2023
6669283
fix: remove unnecesary select
andrii-lysenko Oct 17, 2023
e766fae
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 17, 2023
1ddd1cf
update: use proper action for first modules load
andrii-lysenko Oct 18, 2023
d35cc32
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 18, 2023
b8d5213
update: placeholder wording
andrii-lysenko Oct 18, 2023
bdc778a
fix namings and add Readme
andrii-lysenko Oct 19, 2023
b5cfe1d
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Oct 27, 2023
2486741
updated lock file
andrii-lysenko Oct 27, 2023
9afb558
fix: always enabled modules for Simple sites
andrii-lysenko Nov 2, 2023
8145f7a
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Nov 2, 2023
87cccdc
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Nov 6, 2023
61e6359
fix pnpm file
andrii-lysenko Nov 6, 2023
574e8b4
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Nov 8, 2023
8611e0f
update: changelog versions
andrii-lysenko Nov 8, 2023
d931ea9
Merge branch 'trunk' into fix/ai-assistant-form-blocks-sync
andrii-lysenko Nov 9, 2023
e915a3e
update: jsdoc
andrii-lysenko Nov 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions projects/js-packages/shared-extension-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,64 @@ of the Jetpack plugin, so that plugins can share it. To begin with, we moving
the code used by the Publicize editor extension, but the goal is to bring over
all the shared code.

## Fetching modules data from the store
The package relies on [controls](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#controls)
and [resolvers](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#resolvers)
to pull modules data from the API, and put it into the package's Redux store.

### Basic Usage

In order to have all modules related data synced within different packages, let's use this Redux store as a source of truth, for both, getting and updating the data.


Use [`withSelect`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#withselect), `withDispatch` higher-order component to pull the data or [`useSelect`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#useselect) hook to pull the data from the store to pull directly in component. Example:


```jsx
// Imports.
import { withSelect, withDispatch } from '@wordpress/data';
import { JETPACK_MODULES_STORE_ID } from '@automattic/jetpack-shared-extension-utils';

const SampleComponent = props => {
const { isModuleActive, isLoadingModules, isChangingStatus, updateJetpackModuleStatus } = props;

if ( isModuleActive ) {
return <div>Module is active</div>;
}

if ( isLoadingModules ) {
return <div>Loading modules...</div>;
}

if ( !isModuleActive ) {
return <button onClick={ () => updateJetpackModuleStatus( 'contact-form', true ) }>
Activate module
</button>;
}

return <div>Active contact form module</div>;
}

// We wrap `SampleComponent` into the composition of `withSelect` and `withDispatch` HOCs,
// which will pull the data from the store and pass as a parameter into the component.
// Jetpack modules will be pulled after first selection `isModuleActive`.
export default compose( [
withSelect( ( select, props ) => {
const { isModuleActive, areModulesLoading, areModulesUpdating } = select( 'jetpack-modules' );
return {
isModuleActive: isModuleActive( 'contact-form' ),
isLoadingModules: areModulesLoading(),
isChangingStatus: areModulesUpdating(),
};
} ),
withDispatch( dispatch => {
const { updateJetpackModuleStatus } = dispatch( 'jetpack-modules' );
return { updateJetpackModuleStatus };
} ),
] )( ( SampleComponent ) );
```


## How to install shared-extension-utils

### Installation From Git Repo
Expand Down
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
2 changes: 2 additions & 0 deletions projects/js-packages/shared-extension-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export { default as useAnalytics } from './src/hooks/use-analytics';
export { default as useModuleStatus } from './src/hooks/use-module-status';
export { default as JetpackEditorPanelLogo } from './src/components/jetpack-editor-panel-logo';
export { getBlockIconComponent, getBlockIconProp } from './src/get-block-icon-from-metadata';

export * from './src/modules-state';
3 changes: 2 additions & 1 deletion projects/js-packages/shared-extension-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@automattic/jetpack-shared-extension-utils",
"version": "0.12.6",
"version": "0.13.0-alpha",
"description": "Utility functions used by the block editor extensions",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
"bugs": {
Expand All @@ -22,6 +22,7 @@
"@automattic/jetpack-connection": "workspace:*",
"@wordpress/api-fetch": "6.41.0",
"@wordpress/compose": "6.21.0",
"@wordpress/data": "9.13.0",
"@wordpress/element": "5.21.0",
"@wordpress/i18n": "4.44.0",
"@wordpress/plugins": "6.12.0",
Expand Down
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;
Copy link
Member

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?

Copy link
Contributor Author

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 returns bool.

}
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 };
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,
},
} );
},
};
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 );
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;
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 };
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;
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
2 changes: 1 addition & 1 deletion projects/packages/forms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"link-template": "https://github.com/automattic/jetpack-forms/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.22.x-dev"
"dev-trunk": "0.23.x-dev"
},
"textdomain": "jetpack-forms",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/forms/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-forms",
"version": "0.22.6",
"version": "0.23.0-alpha",
"description": "Jetpack Forms",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/forms/#readme",
"bugs": {
Expand Down
Loading
Loading