[MM-57721] Convert ./components/channel_header_dropdown/menu_items/leave_channel/leave_channel.tsx from Class Component to Function Component (#26722)

* [MM-57721] Convert `./components/channel_header_dropdown/menu_items/leave_channel/leave_channel.tsx` from Class Component to Function Component

* fix failing tests

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Syed Ali Abbas Zaidi
2024-04-15 12:47:00 +05:00
коммит произвёл GitHub
родитель bca4535150
Коммит ede18a7c17
3 изменённых файлов: 31 добавлений и 35 удалений

Просмотреть файл

@@ -643,7 +643,7 @@ exports[`components/ChannelHeaderDropdown should match snapshot with no plugin i
text="Convert to Private Channel" text="Convert to Private Channel"
/> />
</Connect(Component)> </Connect(Component)>
<Connect(LeaveChannel) <Connect(Component)
channel={ channel={
Object { Object {
"create_at": 0, "create_at": 0,
@@ -1475,7 +1475,7 @@ exports[`components/ChannelHeaderDropdown should match snapshot with plugins 1`]
text="Convert to Private Channel" text="Convert to Private Channel"
/> />
</Connect(Component)> </Connect(Component)>
<Connect(LeaveChannel) <Connect(Component)
channel={ channel={
Object { Object {
"create_at": 0, "create_at": 0,

Просмотреть файл

@@ -26,7 +26,7 @@ describe('components/ChannelHeaderDropdown/MenuItem.LeaveChannel', () => {
}; };
it('should match snapshot', () => { it('should match snapshot', () => {
const wrapper = shallow<LeaveChannel>(<LeaveChannel {...baseProps}/>); const wrapper = shallow(<LeaveChannel {...baseProps}/>);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
@@ -35,7 +35,7 @@ describe('components/ChannelHeaderDropdown/MenuItem.LeaveChannel', () => {
...baseProps, ...baseProps,
isDefault: true, isDefault: true,
}; };
const wrapper = shallow<LeaveChannel>(<LeaveChannel {...props}/>); const wrapper = shallow(<LeaveChannel {...props}/>);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
@@ -59,7 +59,7 @@ describe('components/ChannelHeaderDropdown/MenuItem.LeaveChannel', () => {
channel: {...baseProps.channel}, channel: {...baseProps.channel},
actions: {...baseProps.actions}, actions: {...baseProps.actions},
}; };
const wrapper = shallow<LeaveChannel>(<LeaveChannel {...props}/>); const wrapper = shallow(<LeaveChannel {...props}/>);
wrapper.find(Menu.ItemAction).simulate('click', { wrapper.find(Menu.ItemAction).simulate('click', {
preventDefault: jest.fn(), preventDefault: jest.fn(),

Просмотреть файл

@@ -1,7 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import React from 'react'; import React, {memo, useCallback} from 'react';
import {useIntl} from 'react-intl';
import type {Channel} from '@mattermost/types/channels'; import type {Channel} from '@mattermost/types/channels';
@@ -9,7 +10,6 @@ import LeaveChannelModal from 'components/leave_channel_modal';
import Menu from 'components/widgets/menu/menu'; import Menu from 'components/widgets/menu/menu';
import {Constants, ModalIdentifiers} from 'utils/constants'; import {Constants, ModalIdentifiers} from 'utils/constants';
import {localizeMessage} from 'utils/utils';
import type {PropsFromRedux} from './index'; import type {PropsFromRedux} from './index';
@@ -36,22 +36,20 @@ interface Props extends PropsFromRedux {
id?: string; id?: string;
} }
export default class LeaveChannel extends React.PureComponent<Props> { const LeaveChannel = ({
static defaultProps = { isDefault = true,
isDefault: true, isGuestUser = false,
isGuestUser: false,
};
handleLeave = (e: Event) => {
e.preventDefault();
const {
channel, channel,
actions: { actions: {
leaveChannel, leaveChannel,
openModal, openModal,
}, },
} = this.props; id,
}: Props) => {
const intl = useIntl();
const handleLeave = useCallback((e: Event) => {
e.preventDefault();
if (channel.type === Constants.PRIVATE_CHANNEL) { if (channel.type === Constants.PRIVATE_CHANNEL) {
openModal({ openModal({
@@ -64,19 +62,17 @@ export default class LeaveChannel extends React.PureComponent<Props> {
} else { } else {
leaveChannel(channel.id); leaveChannel(channel.id);
} }
}; }, [channel, leaveChannel, openModal]);
render() {
const {channel, isDefault, isGuestUser, id} = this.props;
return ( return (
<Menu.ItemAction <Menu.ItemAction
id={id} id={id}
show={(!isDefault || isGuestUser) && channel.type !== Constants.DM_CHANNEL && channel.type !== Constants.GM_CHANNEL} show={(!isDefault || isGuestUser) && channel.type !== Constants.DM_CHANNEL && channel.type !== Constants.GM_CHANNEL}
onClick={this.handleLeave} onClick={handleLeave}
text={localizeMessage('channel_header.leave', 'Leave Channel')} text={intl.formatMessage({id: 'channel_header.leave', defaultMessage: 'Leave Channel'})}
isDangerous={true} isDangerous={true}
/> />
); );
} };
}
export default memo(LeaveChannel);