This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
Migrate delete_post_modal.jsx to be pure and use Redux #989
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
58f2b09
Migrate DeletePostModal to be pure and use Redux
77bf8a3
Fix styling problem
319488d
Changes based on reviews
JasperVanEsveld 792d809
Removed old post action
JasperVanEsveld a144107
Remove unused imports from post_actions
JasperVanEsveld ffcd8a0
Add browserhistory test
JasperVanEsveld 2a47bd3
Fix styling problem
JasperVanEsveld File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,83 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import {Modal} from 'react-bootstrap'; | ||
import {FormattedMessage} from 'react-intl'; | ||
|
||
import {deletePost} from 'actions/post_actions.jsx'; | ||
import ModalStore from 'stores/modal_store.jsx'; | ||
import Constants from 'utils/constants.jsx'; | ||
import * as UserAgent from 'utils/user_agent.jsx'; | ||
|
||
var ActionTypes = Constants.ActionTypes; | ||
export default class DeletePostModal extends React.PureComponent { | ||
static propTypes = { | ||
|
||
export default class DeletePostModal extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
post: PropTypes.object.isRequired, | ||
commentCount: PropTypes.number.isRequired, | ||
|
||
this.state = { | ||
show: false, | ||
post: null, | ||
commentCount: 0, | ||
error: '', | ||
}; | ||
} | ||
/** | ||
* Does the post come from RHS mode | ||
*/ | ||
isRHS: PropTypes.bool.isRequired, | ||
|
||
componentDidMount() { | ||
ModalStore.addModalListener(ActionTypes.TOGGLE_DELETE_POST_MODAL, this.handleToggle); | ||
} | ||
/** | ||
* Function called when modal is dismissed | ||
*/ | ||
onHide: PropTypes.func.isRequired, | ||
|
||
componentWillUnmount() { | ||
ModalStore.removeModalListener(ActionTypes.TOGGLE_DELETE_POST_MODAL, this.handleToggle); | ||
} | ||
actions: PropTypes.shape({ | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
if (this.state.show && !prevState.show) { | ||
setTimeout(() => { | ||
if (this.deletePostBtn) { | ||
this.deletePostBtn.focus(); | ||
} | ||
}, 200); | ||
} | ||
/** | ||
* Function called for deleting post | ||
*/ | ||
deletePost: PropTypes.func.isRequired, | ||
}), | ||
} | ||
|
||
handleDelete = () => { | ||
deletePost( | ||
this.state.post.channel_id, | ||
this.state.post, | ||
() => { | ||
this.handleHide(); | ||
}, | ||
(err) => { | ||
this.setState({error: err.message}); | ||
} | ||
); | ||
constructor(props) { | ||
super(props); | ||
this.handleDelete = this.handleDelete.bind(this); | ||
this.onHide = this.onHide.bind(this); | ||
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. No action required but just a heads up if you use arrow syntax you don't need these binds (since it will autobind). e.g
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. Oh, that is good to know. Thanks! |
||
this.state = { | ||
show: true, | ||
}; | ||
} | ||
|
||
handleToggle = (value, args) => { | ||
this.setState({ | ||
show: value, | ||
post: args.post, | ||
isRHS: args.isRHS, | ||
commentCount: args.commentCount, | ||
error: '', | ||
}); | ||
handleDelete() { | ||
this.props.actions.deletePost(this.props.post); | ||
this.onHide(); | ||
} | ||
|
||
handleHide = () => { | ||
onHide() { | ||
this.setState({show: false}); | ||
|
||
if (!UserAgent.isMobile()) { | ||
if (this.state.isRHS) { | ||
document.getElementById('reply_textbox').focus(); | ||
var element; | ||
if (this.props.isRHS) { | ||
element = document.getElementById('reply_textbox'); | ||
} else { | ||
document.getElementById('post_textbox').focus(); | ||
element = document.getElementById('post_textbox'); | ||
} | ||
if (element) { | ||
element.focus(); | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
if (!this.state.post) { | ||
return null; | ||
} | ||
|
||
var error = null; | ||
if (this.state.error) { | ||
error = <div className='form-group has-error'><label className='control-label'>{this.state.error}</label></div>; | ||
} | ||
|
||
var commentWarning = ''; | ||
if (this.state.commentCount > 0) { | ||
if (this.props.commentCount > 0) { | ||
commentWarning = ( | ||
<FormattedMessage | ||
id='delete_post.warning' | ||
defaultMessage='This post has {count, number} {count, plural, one {comment} other {comments}} on it.' | ||
values={{ | ||
count: this.state.commentCount, | ||
count: this.props.commentCount, | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
const postTerm = this.state.post.root_id ? ( | ||
const postTerm = this.props.post.root_id ? ( | ||
<FormattedMessage | ||
id='delete_post.comment' | ||
defaultMessage='Comment' | ||
|
@@ -115,7 +92,8 @@ export default class DeletePostModal extends React.Component { | |
return ( | ||
<Modal | ||
show={this.state.show} | ||
onHide={this.handleHide} | ||
onHide={this.onHide} | ||
onExited={this.props.onHide} | ||
enforceFocus={false} | ||
> | ||
<Modal.Header closeButton={true}> | ||
|
@@ -140,7 +118,6 @@ export default class DeletePostModal extends React.Component { | |
<br/> | ||
<br/> | ||
{commentWarning} | ||
{error} | ||
</Modal.Body> | ||
<Modal.Footer> | ||
<button | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
import {bindActionCreators} from 'redux'; | ||
import {connect} from 'react-redux'; | ||
import {deletePost} from 'mattermost-redux/actions/posts'; | ||
|
||
import DeletePostModal from './delete_post_modal.jsx'; | ||
|
||
function mapDispatchToProps(dispatch) { | ||
return { | ||
actions: bindActionCreators({ | ||
deletePost, | ||
}, dispatch), | ||
}; | ||
} | ||
|
||
export default connect(null, mapDispatchToProps)(DeletePostModal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,30 +27,35 @@ export default class DotMenu extends Component { | |
|
||
actions: PropTypes.shape({ | ||
|
||
/* | ||
/** | ||
* Function flag the post | ||
*/ | ||
flagPost: PropTypes.func.isRequired, | ||
|
||
/* | ||
/** | ||
* Function to unflag the post | ||
*/ | ||
unflagPost: PropTypes.func.isRequired, | ||
|
||
/* | ||
* Function to set the edting post | ||
/** | ||
* Function to set the editing post | ||
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. Nice catch! |
||
*/ | ||
setEditingPost: PropTypes.func.isRequired, | ||
|
||
/* | ||
/** | ||
* Function to pin the post | ||
*/ | ||
pinPost: PropTypes.func.isRequired, | ||
|
||
/* | ||
/** | ||
* Function to unpin the post | ||
*/ | ||
unpinPost: PropTypes.func.isRequired, | ||
|
||
/** | ||
* Function to open a modal | ||
*/ | ||
openModal: PropTypes.func.isRequired, | ||
}).isRequired, | ||
} | ||
|
||
|
@@ -185,6 +190,9 @@ export default class DotMenu extends Component { | |
idCount={this.props.idCount} | ||
post={this.props.post} | ||
commentCount={type === 'Post' ? this.props.commentCount : 0} | ||
actions={{ | ||
openModal: this.props.actions.openModal, | ||
}} | ||
/> | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks @GitHubJasper!
Forgot to mention earlier:
deletePost
at https://github.com/mattermost/mattermost-webapp/blob/master/actions/post_actions.jsx#L205 is of no use now. It should be deleted but make sure to consider moving these codes, maybe like so:Then at
delete_post_modal/index.js
, need to generate those new props, like so:(I have not tested above code but more or less it should be like that. Feel free to test functionality :) )