Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Convert AppBar to use useDispatch, useSelector, useWidth #3595

Merged
merged 3 commits into from
Aug 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions packages/ra-ui-materialui/src/layout/AppBar.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React, { Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import classNames from 'classnames';
import MuiAppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import MenuIcon from '@material-ui/icons/Menu';
import withWidth from '@material-ui/core/withWidth';
import compose from 'recompose/compose';
import { toggleSidebar as toggleSidebarAction } from 'ra-core';
import useMediaQuery from '@material-ui/core/useMediaQuery';
import { toggleSidebar } from 'ra-core';

import LoadingIndicator from './LoadingIndicator';
import DefaultUserMenu from './UserMenu';
Expand Down Expand Up @@ -54,24 +53,31 @@ const AppBar = ({
logout,
open,
title,
toggleSidebar,
userMenu,
width,
...rest
}) => {
const classes = useStyles({ classes: classesOverride });
const dispatch = useDispatch();
const locale = useSelector(state => state.i18n.locale);
const isXSmall = useMediaQuery(theme => theme.breakpoints.down('xs'));

return (
<HideOnScroll>
<MuiAppBar className={className} color="secondary" {...rest}>
<MuiAppBar
className={className}
color="secondary"
locale={locale}
{...rest}
>
<Toolbar
disableGutters
variant={width === 'xs' ? 'regular' : 'dense'}
variant={isXSmall ? 'regular' : 'dense'}
className={classes.toolbar}
>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={toggleSidebar}
onClick={() => dispatch(toggleSidebar())}
className={classNames(classes.menuButton)}
>
<MenuIcon
Expand Down Expand Up @@ -106,25 +112,11 @@ AppBar.propTypes = {
className: PropTypes.string,
logout: PropTypes.element,
open: PropTypes.bool,
toggleSidebar: PropTypes.func.isRequired,
userMenu: PropTypes.element,
width: PropTypes.string,
};

AppBar.defaultProps = {
userMenu: <DefaultUserMenu />,
};

const enhance = compose(
connect(
state => ({
locale: state.i18n.locale, // force redraw on locale change
}),
{
toggleSidebar: toggleSidebarAction,
}
),
withWidth()
);

export default enhance(AppBar);
export default AppBar;