Migrate RemoveFileSetting component from class based to function based component (#24797)

* migrate RemoveFileSetting comp to function comp

* code refactored

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Tanmay Vardhaman Thole
2023-10-19 15:58:11 +05:30
коммит произвёл GitHub
родитель 4ec573c3fb
Коммит c5b43df163

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

@@ -1,8 +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, {PureComponent} from 'react'; import React, {memo, useCallback, useState} from 'react';
import type {MouseEvent} from 'react'; import type {FC, MouseEvent} from 'react';
import Setting from './setting'; import Setting from './setting';
import type {Props as SettingsProps} from './setting'; import type {Props as SettingsProps} from './setting';
@@ -10,7 +10,7 @@ import type {Props as SettingsProps} from './setting';
type Props = SettingsProps & { type Props = SettingsProps & {
id: string; id: string;
label: React.ReactNode; label: React.ReactNode;
helptext?: React.ReactNode; helpText?: React.ReactNode;
removeButtonText: React.ReactNode; removeButtonText: React.ReactNode;
removingText?: React.ReactNode; removingText?: React.ReactNode;
fileName: string; fileName: string;
@@ -18,54 +18,53 @@ type Props = SettingsProps & {
disabled?: boolean; disabled?: boolean;
} }
type State = { const RemoveFileSetting: FC<Props> = ({
removing: boolean; id,
} label,
helpText,
removeButtonText,
removingText,
fileName,
onSubmit,
disabled,
}) => {
const [removing, setRemoving] = useState(false);
export default class RemoveFileSetting extends PureComponent<Props, State> { const handleRemove = useCallback((e: MouseEvent<HTMLButtonElement>) => {
constructor(props: Props) {
super(props);
this.state = {
removing: false,
};
}
handleRemove = (e: MouseEvent<HTMLButtonElement>) => {
e.preventDefault(); e.preventDefault();
this.setState({removing: true}); setRemoving(true);
this.props.onSubmit(this.props.id, () => { onSubmit(id, () => {
this.setState({removing: false}); setRemoving(false);
}); });
}; }, [id, onSubmit]);
render() { return (
return ( <Setting
<Setting label={label}
label={this.props.label} helpText={helpText}
helpText={this.props.helpText} inputId={id}
inputId={this.props.id} >
> <div>
<div> <div className='help-text remove-filename'>
<div className='help-text remove-filename'> {fileName}
{this.props.fileName}
</div>
<button
type='button'
className='btn btn-danger'
onClick={this.handleRemove}
disabled={this.props.disabled}
>
{this.state.removing && (
<>
<span className='glyphicon glyphicon-refresh glyphicon-refresh-animate'/>
{this.props.removingText}
</>)}
{!this.state.removing && this.props.removeButtonText}
</button>
</div> </div>
</Setting> <button
); type='button'
} className='btn btn-danger'
} onClick={handleRemove}
disabled={disabled}
>
{removing && (
<>
<span className='glyphicon glyphicon-refresh glyphicon-refresh-animate'/>
{removingText}
</>)}
{!removing && removeButtonText}
</button>
</div>
</Setting>
);
};
export default memo(RemoveFileSetting);