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
Use Gutenberg store #226
Conversation
31926c6
to
e4826ec
Compare
@@ -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 ) { |
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.
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.
src/app/AppContainer.js
Outdated
let blocks = state.blocks; | ||
let refresh = !! ownProps.refresh; | ||
|
||
const newBlocks = ownProps.blocks.map( ( block ) => { |
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.
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 } |
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.
Not sure how it was working before but this looks like it's equivalent with extraData={ this.state.inspectBlocks }
src/app/AppContainer.js
Outdated
|
||
const mapStateToProps = ( state, ownProps ) => { | ||
let blocks = state.blocks; | ||
let refresh = !! ownProps.refresh; |
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.
👋 @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!
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.
It's given as props to BlockManager a few lines below https://github.com/wordpress-mobile/gutenberg-mobile/pull/226/files#diff-0b657b049f5ed3fc0862ff8bf5c9e4d4R29
Then to FlatList
here https://github.com/wordpress-mobile/gutenberg-mobile/pull/226/files#diff-2d50daf729e068ecebcce113d1ce7f9aR254
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.
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
?
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.
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.
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.
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?
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.
You are right, it actually isn't set.
Yeah we should just be using false
instead.
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:
Can you verify that those "findings" above are accurate? Thanks! |
Yes. We're not completely removing the store in this PR as we're still using
There is. We're now using the gutenberg editor reducer with this change (See all the selectors and actions we're getting from
Yeah
I haven't noticed but I'll investigate 👍 |
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? |
It's not propagated anymore, I removed the So basically the gb-mobile store is just initialized with the data from |
export default class BlockHolder extends React.Component<PropsType, StateType> { | ||
constructor( props: PropsType ) { | ||
super( props ); | ||
this.state = { |
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.
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", |
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.
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.
fad9a01
to
37faa04
Compare
package.json
Outdated
@@ -81,6 +81,7 @@ | |||
}, | |||
"dependencies": { | |||
"@babel/runtime": "^7.0.0", | |||
"@wordpress/jest-preset-default": "^3.0.2", |
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.
Can we just upgrade the devDependencies
one or do we indeed need a runtime dep here?
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.
Sounds about right 👍
4b546df
to
1eca84c
Compare
This PR requires the following gutenberg patch WordPress/gutenberg#11777. |
f274656
to
d8c9850
Compare
…h dataSource and use react lifecycle methods to update instead
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.
Looks good to me!
Thanks for the hard work and iteration on this one @Tug, feel free to merge!
Thanks for those excellent review @hypest! You spotted all the mistake I did here :) |
This PR switches the current store with the one provided by gutenberg.
Only
App
andAppContainer
are updated for now since those deal with the state. Some minor changes onBlockManager
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
andBlockEdit
)Testing Instructions
git submodule update --init
yarn install
yarn start:reset
yarn ios
andyarn android
)bash ./.travis/travis-checks-js.sh