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

[RNMobile] Refactor simplify media flow redux store changes #30123

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
60328e3
Moved the last block inserted actions from editor to the block-editor
jd-alexander Mar 19, 2021
42592ba
Moved the last block inserted reducer from editor to the block-editor
jd-alexander Mar 19, 2021
b19dc1f
Moved the last block inserted selector from editor to the block-editor
jd-alexander Mar 19, 2021
9c6e0e6
Fixed es-lint error.
jd-alexander Mar 19, 2021
0eac8e9
Moved last block inserted actions test from editor to the block-editor
jd-alexander Mar 19, 2021
1798f52
Moved last block inserted reducer test from editor to the block-editor
jd-alexander Mar 19, 2021
aa8d426
Moved last block inserted selector test from editor to the block-editor
jd-alexander Mar 19, 2021
e872c82
Moved all calls to last block inserted from editor to block-editor
jd-alexander Mar 19, 2021
286fc73
Merge branch 'rnmobile/simplify_image_insertion_flow-redux-changes' i…
jd-alexander Mar 19, 2021
a035571
last block inserter usage in menu native migrated : editor to block-e…
jd-alexander Mar 19, 2021
9fdba04
[RNMobile] Refactor: Simplify media flow redux migration (#30238)
fluiddot Mar 26, 2021
317cd03
Removed add/clear last block inserted actions and tests due to refactor
jd-alexander Mar 27, 2021
4665b5f
Removed addLastBlockInserted from the insert menu's onPress.
jd-alexander Mar 27, 2021
27ad05d
rewrote the tests for the lastBlockInserted reducer.
jd-alexander Mar 27, 2021
f7894f3
rewrote tests for wasBlockJustInserted selector.
jd-alexander Mar 29, 2021
69856b1
optimized clientId
jd-alexander Mar 29, 2021
770a61a
removed unneeded clientId.
jd-alexander Mar 29, 2021
e8aefa8
put the expectedSource inside the test meta object.
jd-alexander Mar 29, 2021
ab46c9d
used expectedState insted {} for state related tests.
jd-alexander Mar 29, 2021
371ab67
used expectedState instead {} for state related tests.
jd-alexander Mar 29, 2021
f4b5960
Merge remote-tracking branch 'origin/rnmobile/simplify-media-flow-red…
jd-alexander Mar 29, 2021
ae46525
removed parentheses from describe name.
jd-alexander Mar 30, 2021
5c1d9fc
return the same state instead of empty state.
jd-alexander Mar 30, 2021
9307bf6
made changes to test name so its intent is clearer.
jd-alexander Mar 30, 2021
0d0164c
made the insertion source optional.
jd-alexander Mar 30, 2021
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
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/inserter/menu.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ function InserterMenu( {
removeBlock,
resetBlocks,
insertDefaultBlock,
addLastBlockInserted,
} = useDispatch( blockEditorStore );

const { addLastBlockInserted } = useDispatch( 'core/editor' );

const {
items,
destinationRootClientId,
Expand Down
25 changes: 25 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1380,3 +1380,28 @@ export function setHasControlledInnerBlocks(
clientId,
};
}

/**
* Returns an action object to track the last block that was inserted.
*
* @param {Object} clientId The client id of the block.
*
* @return {Object} Action object.
*/
export function addLastBlockInserted( clientId ) {
return {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId,
};
}

/**
* Returns an action object to clear the last block that was inserted.
*
* @return {Object} Action object.
*/
export function clearLastBlockInserted() {
return {
type: 'CLEAR_LAST_BLOCK_INSERTED',
};
}
20 changes: 20 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,25 @@ export function highlightedBlock( state, action ) {
return state;
}

/**
* Reducer returning the block insertion event list state.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function lastBlockInserted( state = {}, action ) {
switch ( action.type ) {
case 'ADD_LAST_BLOCK_INSERTED':
return { ...state, clientId: action.clientId };

case 'CLEAR_LAST_BLOCK_INSERTED':
return {};
}
return state;
}

export default combineReducers( {
blocks,
isTyping,
Expand All @@ -1748,4 +1767,5 @@ export default combineReducers( {
hasBlockMovingClientId,
automaticChangeStatus,
highlightedBlock,
lastBlockInserted,
} );
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2084,3 +2084,14 @@ export const __experimentalGetActiveBlockIdByBlockNames = createSelector(
validBlockNames,
]
);

/**
* Tells if the block with the passed clientId was just inserted.
*
* @param {Object} state Global application state.
* @param {Object} clientId client id of the block.
* @return {boolean} If the client id exists within the lastBlockInserted state then the block was just inserted.
*/
export function wasBlockJustInserted( state, clientId ) {
return state.lastBlockInserted.clientId === clientId;
}
22 changes: 22 additions & 0 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const {
updateSettings,
selectionChange,
validateBlocksToTemplate,
addLastBlockInserted,
clearLastBlockInserted,
} = actions;

describe( 'actions', () => {
Expand Down Expand Up @@ -1508,4 +1510,24 @@ describe( 'actions', () => {
expect( result ).toEqual( false );
} );
} );

describe( 'addLastBlockInserted', () => {
it( 'should return the ADD_LAST_BLOCK_INSERTED action', () => {
const expectedClientId = 1;
const result = addLastBlockInserted( expectedClientId );
expect( result ).toEqual( {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId: expectedClientId,
} );
} );
} );

describe( 'clearLastBlockInserted', () => {
it( 'should return the CLEAR_LAST_BLOCK_INSERTED action', () => {
const result = clearLastBlockInserted();
expect( result ).toEqual( {
type: 'CLEAR_LAST_BLOCK_INSERTED',
} );
} );
} );
} );
24 changes: 24 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
template,
blockListSettings,
lastBlockAttributesChange,
lastBlockInserted,
} from '../reducer';

describe( 'state', () => {
Expand Down Expand Up @@ -2944,4 +2945,27 @@ describe( 'state', () => {
expect( state ).toBe( null );
} );
} );

describe( 'lastBlockInserted()', () => {
fluiddot marked this conversation as resolved.
Show resolved Hide resolved
it( 'should return client id of last block inserted', () => {
const expectedClientId = 1;
const action = {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId: expectedClientId,
};

expect(
lastBlockInserted( { clientId: expectedClientId }, action )
.clientId
).toBe( expectedClientId );
} );

it( 'should return empty state if last block has been cleared', () => {
const action = {
type: 'CLEAR_LAST_BLOCK_INSERTED',
};

expect( lastBlockInserted( {}, action ) ).toStrictEqual( {} );
} );
} );
} );
30 changes: 30 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const {
__experimentalGetParsedReusableBlock,
__experimentalGetAllowedPatterns,
__experimentalGetScopedBlockPatterns,
wasBlockJustInserted,
} = selectors;

describe( 'selectors', () => {
Expand Down Expand Up @@ -3450,6 +3451,35 @@ describe( 'selectors', () => {
);
} );
} );

describe( 'wasBlockJustInserted', () => {
it( 'should return true if the client id passed to wasBlockJustInserted is found within the state', () => {
const expectedClientId = 1;
const state = {
lastBlockInserted: {
clientId: expectedClientId,
},
};

expect( wasBlockJustInserted( state, expectedClientId ) ).toBe(
true
);
} );

it( 'should return false if the client id passed to wasBlockJustInserted is not found within the state', () => {
const expectedClientId = 1;
const unexpectedClientId = 0;
const state = {
lastBlockInserted: {
clientId: unexpectedClientId,
},
};

expect( wasBlockJustInserted( state, expectedClientId ) ).toBe(
false
);
} );
} );
} );

describe( '__experimentalGetParsedReusableBlock', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ export default compose( [
withDispatch( ( dispatch, { clientId }, { select } ) => {
return {
wasBlockJustInserted() {
const { clearLastBlockInserted } = dispatch( 'core/editor' );
const { wasBlockJustInserted } = select( 'core/editor' );
const { clearLastBlockInserted } = dispatch( blockEditorStore );
const { wasBlockJustInserted } = select( blockEditorStore );

const result = wasBlockJustInserted( clientId );

jd-alexander marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ export default compose( [
withDispatch( ( dispatch, { clientId }, { select } ) => {
return {
wasBlockJustInserted() {
const { clearLastBlockInserted } = dispatch( 'core/editor' );
const { wasBlockJustInserted } = select( 'core/editor' );
const { clearLastBlockInserted } = dispatch( blockEditorStore );
const { wasBlockJustInserted } = select( blockEditorStore );

const result = wasBlockJustInserted( clientId );

Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/video/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
VIDEO_ASPECT_RATIO,
VideoPlayer,
InspectorControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { isURL, getProtocol } from '@wordpress/url';
Expand Down Expand Up @@ -374,8 +375,8 @@ export default compose( [
withDispatch( ( dispatch, { clientId }, { select } ) => {
return {
wasBlockJustInserted() {
const { clearLastBlockInserted } = dispatch( 'core/editor' );
const { wasBlockJustInserted } = select( 'core/editor' );
const { clearLastBlockInserted } = dispatch( blockEditorStore );
const { wasBlockJustInserted } = select( blockEditorStore );

const result = wasBlockJustInserted( clientId );

Expand Down
25 changes: 0 additions & 25 deletions packages/editor/src/store/actions.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,3 @@ export function togglePostTitleSelection( isSelected = true ) {
export function* autosave() {
RNReactNativeGutenbergBridge.editorDidAutosave();
}

/**
* Returns an action object to track the last block that was inserted.
*
* @param {Object} clientId The client id of the block.
*
* @return {Object} Action object.
*/
export function addLastBlockInserted( clientId ) {
return {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId,
};
}

/**
* Returns an action object to clear the last block that was inserted.
*
* @return {Object} Action object.
*/
export function clearLastBlockInserted() {
return {
type: 'CLEAR_LAST_BLOCK_INSERTED',
};
}
20 changes: 0 additions & 20 deletions packages/editor/src/store/reducer.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,6 @@ export function notices( state = [], action ) {
return state;
}

/**
* Reducer returning the block insertion event list state.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function lastBlockInserted( state = {}, action ) {
switch ( action.type ) {
case 'ADD_LAST_BLOCK_INSERTED':
return { ...state, clientId: action.clientId };

case 'CLEAR_LAST_BLOCK_INSERTED':
return {};
}
return state;
}

export default combineReducers( {
postId,
postType,
Expand All @@ -112,5 +93,4 @@ export default combineReducers( {
editorSettings,
clipboard,
notices,
lastBlockInserted,
} );
11 changes: 0 additions & 11 deletions packages/editor/src/store/selectors.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,3 @@ export const isEditedPostAutosaveable = createRegistrySelector(
return false;
}
);

/**
* Tells if the block with the passed clientId was just inserted.
*
* @param {Object} state Global application state.
* @param {Object} clientId client id of the block.
* @return {boolean} If the client id exists within the lastBlockInserted state then the block was just inserted.
*/
export function wasBlockJustInserted( state, clientId ) {
return state.lastBlockInserted.clientId === clientId;
}
26 changes: 1 addition & 25 deletions packages/editor/src/store/test/actions.native.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* Internal dependencies
*/
import {
togglePostTitleSelection,
addLastBlockInserted,
clearLastBlockInserted,
} from '../actions';
import { togglePostTitleSelection } from '../actions';

describe( 'actions native', () => {
describe( 'togglePostTitleSelection', () => {
Expand All @@ -17,24 +13,4 @@ describe( 'actions native', () => {
} );
} );
} );

describe( 'addLastBlockInserted', () => {
it( 'should return the ADD_LAST_BLOCK_INSERTED action', () => {
const expectedClientId = 1;
const result = addLastBlockInserted( expectedClientId );
expect( result ).toEqual( {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId: expectedClientId,
} );
} );
} );

describe( 'clearLastBlockInserted', () => {
it( 'should return the CLEAR_LAST_BLOCK_INSERTED action', () => {
const result = clearLastBlockInserted();
expect( result ).toEqual( {
type: 'CLEAR_LAST_BLOCK_INSERTED',
} );
} );
} );
} );
25 changes: 1 addition & 24 deletions packages/editor/src/store/test/reducer.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { postTitle, lastBlockInserted } from '../reducer';
import { postTitle } from '../reducer';

describe( 'state native', () => {
describe( 'postTitle', () => {
Expand Down Expand Up @@ -32,28 +32,5 @@ describe( 'state native', () => {
).toBe( true );
} );
} );

describe( 'lastBlockInserted()', () => {
it( 'should return client id of last block inserted', () => {
const expectedClientId = 1;
const action = {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId: expectedClientId,
};

expect(
lastBlockInserted( { clientId: expectedClientId }, action )
.clientId
).toBe( expectedClientId );
} );

it( 'should return empty state if last block has been cleared', () => {
const action = {
type: 'CLEAR_LAST_BLOCK_INSERTED',
};

expect( lastBlockInserted( {}, action ) ).toStrictEqual( {} );
} );
} );
} );
} );
Loading