Migrate delete_post_modal.jsx to be pure and use Redux #989
Conversation
Migrate DeletePostModal to be pure and use Redux
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.
Nice work! Just a few minor comments and then it LGTM
super(props); | ||
/** | ||
* post data | ||
*/ |
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.
We can probably drop some comments where the prop names are self-explanatory
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 comment
The 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
handleDelete = () => {
// stuff
}
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.
Oh, that is good to know. Thanks!
|
||
function mapStateToProps(state, ownProps) { | ||
return { | ||
...ownProps, |
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 our fault because we do it everywhere (results of a giant copy/paste mistake :P ), but you don't actually need the ...ownProps
here at all. The props will get merged with what is returned here automatically
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.
Awesome, just minor comments.
/* | ||
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
@@ -165,7 +165,18 @@ export default class EditPostModal extends React.PureComponent { | |||
const hasAttachment = editingPost.post.file_ids && editingPost.post.file_ids.length > 0; | |||
if (updatedPost.message.trim().length === 0 && !hasAttachment) { | |||
this.handleHide(false); | |||
GlobalActions.showDeletePostModal(Selectors.getPost(getState(), editingPost.postId), editingPost.commentsCount, editingPost.isRHS); | |||
|
|||
const modalData = { |
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.
Please change to deletePostModalData
just to be explicit.
} | ||
} | ||
} | ||
|
||
render() { | ||
if (!this.state.post) { | ||
if (!this.props.post) { |
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.
I think this is not necessary since post
is defined as required in propTypes. So either remove this or define post like:
post: PropTypes.object,
@@ -7,7 +7,7 @@ import {Route} from 'react-router-dom'; | |||
|
|||
import Pluggable from 'plugins/pluggable'; | |||
import AnnouncementBar from 'components/announcement_bar'; | |||
import DeletePostModal from 'components/delete_post_modal.jsx'; | |||
import DeletePostModal from 'components/delete_post_modal'; |
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 no longer needed here, and corresponding line https://github.com/mattermost/mattermost-webapp/blob/master/components/channel_layout/channel_controller.jsx#L67 can be deleted.
@jwilander @saturninoabril |
} | ||
|
||
handleDelete = () => { | ||
deletePost( |
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:
- the
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:
handleDelete = async () => {
const {
actions,
channelName,
focusedPostId,
post,
teamURL,
} = this.props;
const {data} = await actions.deletePost(post);
if (post.id === focusedPostId && channelName) {
browserHistory.push(teamURL + '/channels/' + channelName);
}
if (data) {
this.onHide();
}
}
Then at delete_post_modal/index.js
, need to generate those new props, like so:
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentTeamUrl} from 'mattermost-redux/selectors/entities/teams';
function mapStateToProps(state, ownProps) {
const channel = getChannel(state, ownProps.post.channel_id);
let channelName = '';
if (channel) {
channelName = channel.name;
}
const {focusedPostId} = state.views.channel;
return {
channelName,
focusedPostId,
teamURL: getCurrentTeamUrl(state),
};
}
(I have not tested above code but more or less it should be like that. Feel free to test functionality :) )
Added some functionality from the old action to the component
@saturninoabril |
You may test it like:
(Should replace |
return { | ||
channelName, | ||
focusedPostId, | ||
currentTeamDetails: getCurrentTeam(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.
Just pass a member not the whole object like: teamName: getCurrentTeam(state).name,
@GitHubJasper That's my last comment and I'm all good with your PR. Good job! |
Clean up test baseProps Pass teamname instead of team object
@saturninoabril 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.
Awesome @GitHubJasper and @CoolTomatos, looks good to me!
Great work on this guys! |
Summary
Migrate delete_post_modal.jsx to be pure and use Redux.
Updated unit tests for editing post and menu item.
Also added new unit tests for delete_post_modal
Ticket Link
mattermost/mattermost#7660
Checklist
[Place an '[x]' (no spaces) in all applicable fields. Please remove unrelated fields.]