From 60aa526e6205d3b1bad5fd73c64b171c610fd7ab Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Tue, 2 Nov 2021 18:00:04 +0100 Subject: [PATCH] Refactored `UploadMenu` component from class-based to functional --- src/menu/upload_menu.tsx | 68 +++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/src/menu/upload_menu.tsx b/src/menu/upload_menu.tsx index 91b9387..140a9bb 100644 --- a/src/menu/upload_menu.tsx +++ b/src/menu/upload_menu.tsx @@ -1,11 +1,11 @@ import * as queryString from 'query-string'; -import * as React from 'react'; import md5 from 'md5'; import {analyticsEvent} from '../util/analytics'; import {Dropdown, Icon, Menu} from 'semantic-ui-react'; import {FormattedMessage} from 'react-intl'; import {MenuType} from './menu_item'; import {RouteComponentProps} from 'react-router-dom'; +import {SyntheticEvent} from 'react'; function loadFileAsText(file: File): Promise { return new Promise((resolve) => { @@ -27,8 +27,8 @@ interface Props { } /** Displays and handles the "Open file" menu. */ -export class UploadMenu extends React.Component { - private async handleUpload(event: React.SyntheticEvent) { +export function UploadMenu(props: RouteComponentProps & Props) { + async function handleUpload(event: SyntheticEvent) { const files = (event.target as HTMLInputElement).files; if (!files || !files.length) { return; @@ -68,11 +68,9 @@ export class UploadMenu extends React.Component { // Use history.replace() when reuploading the same file and history.push() when loading // a new file. - const search = queryString.parse(this.props.location.search); + const search = queryString.parse(props.location.search); const historyPush = - search.file === hash - ? this.props.history.replace - : this.props.history.push; + search.file === hash ? props.history.replace : props.history.push; historyPush({ pathname: '/view', @@ -81,33 +79,31 @@ export class UploadMenu extends React.Component { }); } - render() { - const content = ( - <> - - - - ); - return ( - <> - {this.props.menuType === MenuType.Menu ? ( - - ) : ( - - {content} - - )} - this.handleUpload(e)} - /> - - ); - } + const content = ( + <> + + + + ); + return ( + <> + {props.menuType === MenuType.Menu ? ( + + ) : ( + + {content} + + )} + + + ); }