-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
State: Optimistically update the saved post #1093
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
import { BEGIN, COMMIT, REVERT } from 'redux-optimist'; | ||
import { get, uniqueId } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -27,17 +28,30 @@ export default { | |
...edits, | ||
content: serialize( getBlocks( state ) ), | ||
}; | ||
const transactionId = uniqueId(); | ||
|
||
if ( ! isNew ) { | ||
toSend.id = postId; | ||
} | ||
|
||
dispatch( { type: 'CLEAR_POST_EDITS' } ); | ||
dispatch( { | ||
type: 'CLEAR_POST_EDITS', | ||
optimist: { type: BEGIN, id: transactionId }, | ||
} ); | ||
dispatch( { | ||
type: 'UPDATE_POST', | ||
edits: toSend, | ||
optimist: { id: transactionId }, | ||
} ); | ||
new wp.api.models.Post( toSend ).save().done( ( newPost ) => { | ||
dispatch( { | ||
type: 'REQUEST_POST_UPDATE_SUCCESS', | ||
post: newPost, | ||
isNew, | ||
optimist: { type: COMMIT, id: transactionId }, | ||
} ); | ||
dispatch( { | ||
type: 'UPDATE_POST', | ||
edits: newPost, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem like something we should merge into our representation of a post, per the behavior of the reducer, but instead reset / replace with. Maybe a separate https://github.com/WordPress/gutenberg/blob/6dea0df/editor/index.js#L49-L53 Maybe instead: store.dispatch( { type: 'RESET_POST', post } );
store.dispatch( { type: 'RESET_BLOCKS', blocks: wp.blocks.parse( post.content.raw ) } ); Or maybe we don't need (Forgive my spew of brain-thoughts 😄 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the "block" state is more close to "edits" than it is to "currentPost". Thus, we shouldn't reset its value after a save success, the user could have updated it. |
||
} ); | ||
} ).fail( ( err ) => { | ||
dispatch( { | ||
|
@@ -47,6 +61,7 @@ export default { | |
message: __( 'An unknown error occurred.' ), | ||
} ), | ||
edits, | ||
optimist: { type: REVERT, id: transactionId }, | ||
} ); | ||
} ); | ||
}, | ||
|
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.
Removing this is causing an error when save completes: