From 8837a3605f2ecc70ba6fc1224466bd6c3bdb0242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eckhart=20W=C3=B6rner?= Date: Mon, 23 Sep 2024 21:06:31 +0200 Subject: [PATCH] Refactor Navbar to functional component --- src/components/navbar/Navbar.tsx | 140 +++++++++++++------------------ 1 file changed, 58 insertions(+), 82 deletions(-) diff --git a/src/components/navbar/Navbar.tsx b/src/components/navbar/Navbar.tsx index 45fd975..1f467a5 100644 --- a/src/components/navbar/Navbar.tsx +++ b/src/components/navbar/Navbar.tsx @@ -1,8 +1,7 @@ -import React from 'react' -import { generatePath } from 'react-router' -import { RouteComponentProps, withRouter, NavLink } from 'react-router-dom' -import { Location } from 'history' -import { withTranslation, WithTranslationProps } from 'react-i18next' +import { FC, useEffect, useState } from 'react' +import { generatePath, useHistory, useLocation } from 'react-router' +import { NavLink } from 'react-router-dom' +import { useTranslation } from 'react-i18next' import LoginInNavbar from '../login/login-in-navbar' import SearchInput from '../search-input' @@ -12,97 +11,74 @@ import SVGSearch from '../../assets/img/icons/search.svg?react' import './Navbar.less' -export class Navbar extends React.Component { - constructor(props: NavBarProps) { - super(props) - - this.state = { - mobileSearchBarActive: false, +const Navbar: FC = ({ className }) => { + const [mobileSearchBarActive, setMobileSearchBarActive] = useState(false) + const { t } = useTranslation() + const history = useHistory() + const location = useLocation() + const mobileSearchBarActiveClassName = mobileSearchBarActive ? 'active' : '' + const insideSearch = location.pathname.startsWith('/search/') + useEffect(() => { + if (!insideSearch) { + setMobileSearchBarActive(false) } + }, [insideSearch]) - const { history } = this.props - history.listen(this.locationListen.bind(this)) - } - - locationListen(location: Location) { - // Hide Mobile search bar when navigating away from search + const callSearch = (value: string) => { + const trimmedValue = value.trim() - if (!location.pathname.startsWith('/search/')) { - const { mobileSearchBarActive } = this.state - if (mobileSearchBarActive) { - this.setState({ mobileSearchBarActive: false }) - } + if (trimmedValue !== '') { + const path = generatePath('/search/:term', { term: trimmedValue }) + history.push(path) } } - toggleMobileSeachBar() { - const { mobileSearchBarActive } = this.state - this.setState({ mobileSearchBarActive: !mobileSearchBarActive }) + const toggleMobileSeachBar = () => { + setMobileSearchBarActive(!mobileSearchBarActive) } - render() { - const { history, className, i18n } = this.props - - const callSearch = (value: string) => { - const trimmedValue = encodeURIComponent(value.trim()) - - if (trimmedValue !== '') { - const path = generatePath('/search/:term', { term: trimmedValue }) - history.push(path) - } - } - - const { mobileSearchBarActive } = this.state - const mobileSearchBarActiveClassName = mobileSearchBarActive ? 'active' : '' - - return ( - <> -
- -
-   -
-
- -
-
+
+ +
+   +
+
+ +
+
+ -
-
- -
- + +
-
- {mobileSearchBarActive && ( -
- +
+
- )} - - ) - } + +
+
+ {mobileSearchBarActive && ( +
+ +
+ )} + + ) } export type NavBarProps = { className?: string -} & RouteComponentProps & - WithTranslationProps - -export interface NavBarState { - mobileSearchBarActive: Boolean } -export default withRouter(withTranslation()(Navbar)) +export default Navbar