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

Use Gutenberg store #226

Merged
merged 14 commits into from Nov 15, 2018
Merged

Use Gutenberg store #226

merged 14 commits into from Nov 15, 2018

Conversation

Tug
Copy link
Contributor

@Tug Tug commented Nov 7, 2018

This PR switches the current store with the one provided by gutenberg.

Only App and AppContainer are updated for now since those deal with the state. Some minor changes on BlockManager are necessary since the gb store already deals with merging blocks.

We'll also want to connect some of our components to the store and reuse existing components from the gutenberg repository (BlockList, BlockListBlock and BlockEdit)

Testing Instructions

  • git submodule update --init
  • yarn install
  • yarn start:reset
  • Check that there are no important regressions on android and ios (yarn ios and yarn android)
  • Run the tests bash ./.travis/travis-checks-js.sh

@Tug Tug added [Type] Enhancement Improves a current area of the editor [Status] Not Ready For Review labels Nov 7, 2018
@Tug Tug self-assigned this Nov 7, 2018
@Tug Tug requested a review from hypest November 7, 2018 14:09
@Tug Tug force-pushed the add/gb-store branch 4 times, most recently from 31926c6 to e4826ec Compare November 7, 2018 16:20
@@ -115,12 +117,14 @@ export default class BlockManager extends React.Component<PropsType, StateType>
}

static getDerivedStateFromProps( props: PropsType, state: StateType ) {
if ( props.fullparse === true ) {
if ( state.blocks !== props.blocks ) {
Copy link
Contributor Author

@Tug Tug Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might impact performance as the list of blocks will be recreated on any block change. I'll see if we can do something about it.

let blocks = state.blocks;
let refresh = !! ownProps.refresh;

const newBlocks = ownProps.blocks.map( ( block ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hack to set the focus property on each block. Once we use the isSelected() selector in our components we can simplify this

@@ -263,8 +250,8 @@ export default class BlockManager extends React.Component<PropsType, StateType>
list = (
<FlatList
style={ styles.list }
data={ this.props.blocks }
extraData={ this.props.refresh, this.state.inspectBlocks }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how it was working before but this looks like it's equivalent with extraData={ this.state.inspectBlocks }


const mapStateToProps = ( state, ownProps ) => {
let blocks = state.blocks;
let refresh = !! ownProps.refresh;
Copy link
Contributor

@hypest hypest Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 @Tug , I'm trying to figure out how refresh ends up being a field in ownProps, and I can't find it.

At his point in the code, I understand that ownProps is the props passed to the AppContainer component. The two HOCs composed over it (withDispatch and withSelect) don't seem to add the refresh field.

Can you point me to where this refresh is added/set? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, let me see I understand this. mapStateToProps will be called for every component down the line so, BlockManager will get the refresh prop from the mapStateToProps() call on the component above BlockManager?

Plus, ownProps.refresh will be undefined for AppContainer?

Copy link
Contributor Author

@Tug Tug Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really for every component, connect is just an HOC that will re-render the inside component (MainApp in this case) on state change (meaning it will bind listeners on this.props.store given by the provider in App).
So every properties returned in mapStateToProps are added to the props of MainApp and as you can see in MainApp.js https://github.com/wordpress-mobile/gutenberg-mobile/blob/master/src/app/MainApp.js#L33 this component passes all its props down to BlockManager.
In this case refresh is a hack for FlatList, we're just flipping its value whenever we detect a change in blocks. Later we'll probably want to remove it and rely on blocks only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So every properties returned in mapStateToProps are added to the props of MainApp and as you can see in MainApp.js

Cool, that's what I've understood so far, thanks. So, I'm back to the original question: where is the value of ownProps.refresh set? My impression is that it's always undefined since AppContainer's props never get augmented with refresh. Right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it actually isn't set.
Yeah we should just be using false instead.

@hypest
Copy link
Contributor

hypest commented Nov 8, 2018

Had a look @Tug and I tried to follow the code paths and data flow. This is not exactly a review pass but more like a check to see if I have understood things. Here's what I got:

  1. The "old" store is there but the "old" reducer is essentially vestigial now, it's not used
  2. There is no new reducer atm, its functionality is now embedded in the mapDispatchToProps, right? Examples: the parseBlocksAction and serializeToNativeAction mappings.
  3. mapStateToProps is essentially bringing in the blocks list from the data package
  4. on Android, animations no longer seem to work

Can you verify that those "findings" above are accurate? Thanks!

@Tug
Copy link
Contributor Author

Tug commented Nov 8, 2018

  1. The "old" store is there but the "old" reducer is essentially vestigial now, it's not used

Yes. We're not completely removing the store in this PR as we're still using setupStore and reusing the data. Also it will require a bit more changes, I think Types will be affected as well.

  1. There is no new reducer atm, its functionality is now embedded in the mapDispatchToProps, right? Examples: the parseBlocksAction and serializeToNativeAction mappings.

There is. We're now using the gutenberg editor reducer with this change (See all the selectors and actions we're getting from select( 'core/editor' ) and dispatch( 'core/editor' )).
And serializeToNativeAction is a bit of an exception as it doesn't change the store, it just reads from it.

  1. mapStateToProps is essentially bringing in the blocks list from the data package

Yeah mapStateToProps was simply dumping the whole store state into the MainApp props. Now we're injecting the blocks prop from the gutenberg store using withSelect and picking it (using ownProps.blocks) in mapDispatchToProps instead of state.blocks

  1. on Android, animations no longer seem to work

I haven't noticed but I'll investigate 👍

@hypest
Copy link
Contributor

hypest commented Nov 8, 2018

Thanks for confirming @Tug !

On a different thing now: Since the actions are dispatched to the GB store, how is the old store changed and the change propagating down? Apparently it's propagated but, trying to connect the dots there. Any pointers?

@Tug
Copy link
Contributor Author

Tug commented Nov 8, 2018

It's not propagated anymore, I removed the dispatch() calls in e4826ec

So basically the gb-mobile store is just initialized with the data from initial-html.js then we retrieve the blocks to initialize the gb store and we don't read or write from the gb-mobile store ever again.

export default class BlockHolder extends React.Component<PropsType, StateType> {
constructor( props: PropsType ) {
super( props );
this.state = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this state isn't used anywhere afaics.

package.json Outdated
@@ -13,6 +13,7 @@
"@babel/plugin-transform-runtime": "^7.1.0",
"@wordpress/babel-preset-default": "^1.1.2",
"@wordpress/block-serialization-spec-parser": "^1.0.0",
"@wordpress/scripts": "^2.4.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what is wrong with including @wordpress/scripts as a dep (yarn install fails with error Package "" refers to a non-existing file '"<path to project root>/babel-preset-default"') but, do we really need to add this dep? Building and running the app seems fine even without it.

@Tug Tug force-pushed the add/gb-store branch 4 times, most recently from fad9a01 to 37faa04 Compare November 12, 2018 09:56
package.json Outdated
@@ -81,6 +81,7 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"@wordpress/jest-preset-default": "^3.0.2",
Copy link
Contributor

@hypest hypest Nov 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just upgrade the devDependencies one or do we indeed need a runtime dep here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds about right 👍

@Tug Tug force-pushed the add/gb-store branch 3 times, most recently from 4b546df to 1eca84c Compare November 13, 2018 14:19
@Tug
Copy link
Contributor Author

Tug commented Nov 13, 2018

This PR requires the following gutenberg patch WordPress/gutenberg#11777.
An alternative approach could be taken so we don't get blocked by this issue.

@Tug Tug force-pushed the add/gb-store branch 4 times, most recently from f274656 to d8c9850 Compare November 13, 2018 19:32
Copy link
Contributor

@hypest hypest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Thanks for the hard work and iteration on this one @Tug, feel free to merge!

@Tug Tug merged commit 5c2e876 into master Nov 15, 2018
@Tug Tug deleted the add/gb-store branch November 15, 2018 08:58
@Tug
Copy link
Contributor Author

Tug commented Nov 15, 2018

Thanks for those excellent review @hypest! You spotted all the mistake I did here :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement Improves a current area of the editor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants