Skip to content
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 7 commits into from Mar 23, 2018

Conversation

JasperVanEsveld
Copy link
Contributor

@JasperVanEsveld JasperVanEsveld commented Mar 22, 2018

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.]

  • Ran make check-style to check for style errors (required for all pull requests)
  • Ran make test to ensure unit and component tests passed
  • Added or updated unit tests

Migrate DeletePostModal to be pure and use Redux
@jasonblais jasonblais added the 2: Dev Review Requires review by a core commiter label Mar 22, 2018
Copy link
Member

@jwilander jwilander left a 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
*/
Copy link
Member

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);
Copy link
Member

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
}

Copy link
Contributor Author

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,
Copy link
Member

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

Copy link
Member

@saturninoabril saturninoabril left a 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
Copy link
Member

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 = {
Copy link
Member

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) {
Copy link
Member

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';
Copy link
Member

Choose a reason for hiding this comment

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

@JasperVanEsveld
Copy link
Contributor Author

@jwilander @saturninoabril
I've updated the pull request in my last commit according to your guys reviews
Please check if it is alright now

}

handleDelete = () => {
deletePost(
Copy link
Member

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:

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
@JasperVanEsveld
Copy link
Contributor Author

JasperVanEsveld commented Mar 22, 2018

@saturninoabril
I tried to add the functionality as it was in the previous action but I am not sure how to test that it works.
Don't know how to make the post.id === focusedPostId return true but it should be the same as it was

@saturninoabril
Copy link
Member

You may test it like:

    test('should call browserHistory.push on handleDelete with post.id === focusedPostId && channelName', async () => {
        browserHistory.push = jest.fn();
        const actions = {deletePost: jest.fn()};
        actions.deletePost.mockReturnValueOnce({data: true});
        const props = {...baseProps, actions, focusedPostId: '123', channelName: 'channel_name', currentTeamDetails: {name: 'team_name'}};
        const wrapper = shallow(
            <DeletePostModal {...props}/>
        );

        await wrapper.instance().handleDelete();
        expect(browserHistory.push).toHaveBeenCalledWith('/team_name/channels/channel_name');
    });

(Should replace currentTeamDetails object to teamName string)

return {
channelName,
focusedPostId,
currentTeamDetails: getCurrentTeam(state),
Copy link
Member

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,

@saturninoabril
Copy link
Member

@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
@JasperVanEsveld
Copy link
Contributor Author

JasperVanEsveld commented Mar 23, 2018

@saturninoabril Thanks :)
It should be alright now.
And thanks for helping me test @CoolTomatos 👍

Copy link
Member

@saturninoabril saturninoabril left a 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!

@jwilander jwilander added 4: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a core commiter labels Mar 23, 2018
@jwilander jwilander merged commit bc48cd4 into mattermost:master Mar 23, 2018
@jwilander
Copy link
Member

Great work on this guys!

@CoolTomatos CoolTomatos deleted the delete-post-modal branch March 23, 2018 12:20
@amyblais amyblais added Changelog/Not Needed Does not require a changelog entry Docs/Not Needed Does not require documentation labels Mar 23, 2018
@lindalumitchell lindalumitchell added the Tests/Not Needed Does not require new release tests label Mar 24, 2018
hmhealey pushed a commit that referenced this pull request Aug 28, 2020
hmhealey pushed a commit that referenced this pull request Mar 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4: Reviews Complete All reviewers have approved the pull request Changelog/Not Needed Does not require a changelog entry Docs/Not Needed Does not require documentation Tests/Not Needed Does not require new release tests
Projects
None yet
6 participants