From 0386655fe093e7b642156f8bbcf7ea59d4895355 Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Tue, 2 Nov 2021 12:50:41 +0100 Subject: [PATCH] Refactored `TopBar` component from class-based to functional --- src/menu/top_bar.tsx | 167 +++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 93 deletions(-) diff --git a/src/menu/top_bar.tsx b/src/menu/top_bar.tsx index 15d62c4..7aba441 100644 --- a/src/menu/top_bar.tsx +++ b/src/menu/top_bar.tsx @@ -1,5 +1,4 @@ import * as queryString from 'query-string'; -import * as React from 'react'; import {Dropdown, Icon, Menu} from 'semantic-ui-react'; import {FormattedMessage} from 'react-intl'; import {IndiInfo, JsonGedcomData} from 'topola'; @@ -38,32 +37,32 @@ interface Props { showWikiTreeMenus: boolean; } -export class TopBar extends React.Component { - private changeView(view: string) { - const location = this.props.location; +export function TopBar(props: RouteComponentProps & Props) { + function changeView(view: string) { + const location = props.location; const search = queryString.parse(location.search); if (search.view !== view) { search.view = view; location.search = queryString.stringify(search); - this.props.history.push(location); + props.history.push(location); } } - private chartMenus(screenSize: ScreenSize) { - if (!this.props.showingChart) { + function chartMenus(screenSize: ScreenSize) { + if (!props.showingChart) { return null; } const chartTypeItems = ( <> - this.changeView('hourglass')}> + changeView('hourglass')}> - {this.props.allowAllRelativesChart ? ( - this.changeView('relatives')}> + {props.allowAllRelativesChart ? ( + changeView('relatives')}> { /> ) : null} - this.changeView('fancy')}> + changeView('fancy')}> { case ScreenSize.LARGE: return ( <> - this.props.eventHandlers.onPrint()}> + @@ -102,25 +101,19 @@ export class TopBar extends React.Component { className="item" > - this.props.eventHandlers.onDownloadPdf()} - > + - this.props.eventHandlers.onDownloadPng()} - > + - this.props.eventHandlers.onDownloadSvg()} - > + { {chartTypeItems} ); @@ -151,34 +144,28 @@ export class TopBar extends React.Component { case ScreenSize.SMALL: return ( <> - this.props.eventHandlers.onPrint()}> + - this.props.eventHandlers.onDownloadPdf()} - > + - this.props.eventHandlers.onDownloadPng()} - > + - this.props.eventHandlers.onDownloadSvg()} - > + { } } - private title() { + function title() { return ( Topola Genealogy @@ -202,16 +189,16 @@ export class TopBar extends React.Component { ); } - private fileMenus(screenSize: ScreenSize) { + function fileMenus(screenSize: ScreenSize) { // In standalone WikiTree mode, show only the "Select WikiTree ID" menu. - if (!this.props.standalone && this.props.showWikiTreeMenus) { + if (!props.standalone && props.showWikiTreeMenus) { switch (screenSize) { case ScreenSize.LARGE: - return ; + return ; case ScreenSize.SMALL: return ( <> - + ); @@ -219,7 +206,7 @@ export class TopBar extends React.Component { } // Don't show "open" menus in non-standalone mode. - if (!this.props.standalone) { + if (!props.standalone) { return null; } @@ -227,7 +214,7 @@ export class TopBar extends React.Component { case ScreenSize.LARGE: // Show dropdown if chart is shown, otherwise show individual menu // items. - const menus = this.props.showingChart ? ( + const menus = props.showingChart ? ( @@ -238,16 +225,16 @@ export class TopBar extends React.Component { className="item" > - - - + + + ) : ( <> - - - + + + ); return menus; @@ -255,17 +242,17 @@ export class TopBar extends React.Component { case ScreenSize.SMALL: return ( <> - - - + + + ); } } - private wikiTreeLoginMenu(screenSize: ScreenSize) { - if (!this.props.showWikiTreeMenus) { + function wikiTreeLoginMenu(screenSize: ScreenSize) { + if (!props.showWikiTreeMenus) { return null; } return ( @@ -274,14 +261,14 @@ export class TopBar extends React.Component { menuType={ screenSize === ScreenSize.SMALL ? MenuType.Dropdown : MenuType.Menu } - {...this.props} + {...props} /> {screenSize === ScreenSize.SMALL ? : null} ); } - private mobileMenus() { + function mobileMenus() { return ( <> { icon={null} > - {this.fileMenus(ScreenSize.SMALL)} - {this.chartMenus(ScreenSize.SMALL)} - {this.wikiTreeLoginMenu(ScreenSize.SMALL)} + {fileMenus(ScreenSize.SMALL)} + {chartMenus(ScreenSize.SMALL)} + {wikiTreeLoginMenu(ScreenSize.SMALL)} { - {this.props.standalone ? ( - {this.title()} - ) : ( - this.title() - )} + {props.standalone ? {title()} : title()} ); } - private desktopMenus() { + function desktopMenus() { return ( <> - {this.props.standalone ? {this.title()} : null} - {this.fileMenus(ScreenSize.LARGE)} - {this.chartMenus(ScreenSize.LARGE)} + {props.standalone ? {title()} : null} + {fileMenus(ScreenSize.LARGE)} + {chartMenus(ScreenSize.LARGE)} - {this.wikiTreeLoginMenu(ScreenSize.LARGE)} + {wikiTreeLoginMenu(ScreenSize.LARGE)} { ); } - render() { - return ( - <> - - {this.desktopMenus()} - - - {this.mobileMenus()} - - - ); - } + return ( + <> + + {desktopMenus()} + + + {mobileMenus()} + + + ); }