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={this.props.label} label={label}
helpText={this.props.helpText} helpText={helpText}
inputId={this.props.id} inputId={id}
> >
<div> <div>
<div className='help-text remove-filename'> <div className='help-text remove-filename'>
{this.props.fileName} {fileName}
</div> </div>
<button <button
type='button' type='button'
className='btn btn-danger' className='btn btn-danger'
onClick={this.handleRemove} onClick={handleRemove}
disabled={this.props.disabled} disabled={disabled}
> >
{this.state.removing && ( {removing && (
<> <>
<span className='glyphicon glyphicon-refresh glyphicon-refresh-animate'/> <span className='glyphicon glyphicon-refresh glyphicon-refresh-animate'/>
{this.props.removingText} {removingText}
</>)} </>)}
{!this.state.removing && this.props.removeButtonText} {!removing && removeButtonText}
</button> </button>
</div> </div>
</Setting> </Setting>
); );
} };
}
export default memo(RemoveFileSetting);