saleor-dashboard/src/components/AppLayout/AppLayout.tsx

561 lines
17 KiB
TypeScript
Raw Normal View History

import saleorDarkLogoSmall from "@assets/images/logo-dark-small.svg";
import saleorDarkLogo from "@assets/images/logo-dark.svg";
import menuArrowIcon from "@assets/images/menu-arrow-icon.svg";
import Avatar from "@material-ui/core/Avatar";
import Chip from "@material-ui/core/Chip";
import ClickAwayListener from "@material-ui/core/ClickAwayListener";
import Grow from "@material-ui/core/Grow";
import Hidden from "@material-ui/core/Hidden";
2019-06-19 14:40:52 +00:00
import LinearProgress from "@material-ui/core/LinearProgress";
import MenuItem from "@material-ui/core/MenuItem";
import Menu from "@material-ui/core/MenuList";
import Paper from "@material-ui/core/Paper";
import Popper from "@material-ui/core/Popper";
import { makeStyles } from "@material-ui/core/styles";
2019-10-09 15:14:23 +00:00
import { createConfigurationMenu } from "@saleor/configuration";
2019-11-14 14:10:52 +00:00
import useAppState from "@saleor/hooks/useAppState";
import useLocalStorage from "@saleor/hooks/useLocalStorage";
2019-06-19 14:40:52 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useTheme from "@saleor/hooks/useTheme";
import useUser from "@saleor/hooks/useUser";
import ArrowDropdown from "@saleor/icons/ArrowDropdown";
2019-10-25 12:54:14 +00:00
import { staffMemberDetailsUrl } from "@saleor/staff/urls";
import classNames from "classnames";
import React from "react";
import SVG from "react-inlinesvg";
import { FormattedMessage, useIntl } from "react-intl";
import useRouter from "use-react-router";
2019-06-19 14:40:52 +00:00
import Container from "../Container";
2019-11-14 14:10:52 +00:00
import ErrorPage from "../ErrorPage";
2020-08-08 12:59:09 +00:00
import Navigator from "../Navigator";
2020-08-10 12:43:24 +00:00
import NavigatorButton from "../NavigatorButton/NavigatorButton";
2019-06-19 14:40:52 +00:00
import AppActionContext from "./AppActionContext";
import AppHeaderContext from "./AppHeaderContext";
import { appLoaderHeight, drawerWidth, drawerWidthExpanded } from "./consts";
import MenuList from "./MenuList";
2019-08-29 10:55:56 +00:00
import createMenuStructure from "./menuStructure";
import ResponsiveDrawer from "./ResponsiveDrawer";
2019-06-19 14:40:52 +00:00
import ThemeSwitch from "./ThemeSwitch";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(
theme => ({
2019-06-19 14:40:52 +00:00
appAction: {
[theme.breakpoints.down("sm")]: {
left: 0,
width: "100%"
},
2019-06-19 14:40:52 +00:00
bottom: 0,
gridColumn: 2,
2019-10-23 15:10:46 +00:00
position: "sticky",
zIndex: 10
2019-06-19 14:40:52 +00:00
},
2020-10-14 08:17:23 +00:00
appActionDocked: {
position: "static"
},
2019-06-19 14:40:52 +00:00
appLoader: {
height: appLoaderHeight,
marginBottom: theme.spacing(2),
2019-06-19 14:40:52 +00:00
zIndex: 1201
},
2019-10-23 15:10:46 +00:00
appLoaderPlaceholder: {
height: appLoaderHeight,
marginBottom: theme.spacing(2)
2019-10-23 15:10:46 +00:00
},
arrow: {
marginLeft: theme.spacing(2),
transition: theme.transitions.duration.standard + "ms"
},
avatar: {
"&&": {
height: 32,
width: 32
}
},
2019-06-19 14:40:52 +00:00
content: {
[theme.breakpoints.down("sm")]: {
paddingLeft: 0
},
paddingLeft: drawerWidthExpanded,
transition: "padding-left 0.5s ease",
width: "100%"
},
contentToggle: {
[theme.breakpoints.down("sm")]: {
paddingLeft: 0
},
paddingLeft: drawerWidth
2019-06-19 14:40:52 +00:00
},
darkThemeSwitch: {
2019-11-12 16:28:21 +00:00
[theme.breakpoints.down("sm")]: {
marginRight: -theme.spacing(1.5)
2019-11-12 16:28:21 +00:00
},
2019-10-28 16:16:49 +00:00
marginRight: theme.spacing(2)
2019-06-19 14:40:52 +00:00
},
header: {
2019-11-12 16:28:21 +00:00
[theme.breakpoints.down("sm")]: {
height: 88,
marginBottom: 0
2019-11-12 16:28:21 +00:00
},
2019-06-19 14:40:52 +00:00
display: "flex",
height: 40,
marginBottom: theme.spacing(3)
2019-06-19 14:40:52 +00:00
},
isMenuSmall: {
"& path": {
fill: theme.palette.primary.main
},
"& span": {
margin: "0 8px"
},
"& svg": {
marginTop: 8,
transform: "rotate(180deg)"
},
"&:hover": {
background: "#E6F3F3"
},
background: theme.palette.background.paper,
border: `solid 1px #EAEAEA`,
borderRadius: "50%",
cursor: "pointer",
height: 32,
position: "absolute",
right: -16,
top: 65,
transition: `background ${theme.transitions.duration.shorter}ms`,
width: 32,
zIndex: 99
},
isMenuSmallDark: {
"&:hover": {
background: `linear-gradient(0deg, rgba(25, 195, 190, 0.1), rgba(25, 195, 190, 0.1)), ${theme.palette.background.paper}`
},
border: `solid 1px #252728`,
transition: `background ${theme.transitions.duration.shorter}ms`
},
isMenuSmallHide: {
"& svg": {
marginLeft: "3px",
transform: "rotate(0deg)"
}
},
logo: {
"& svg": {
left: "50%",
position: "absolute",
top: "50%",
transform: "translate(-50%,-50%)"
},
background: theme.palette.secondary.main,
display: "block",
height: 80,
position: "relative"
},
logoDark: {
"& path": {
fill: theme.palette.common.white
},
background: theme.palette.primary.main
},
logoSmall: {
"& svg": {
margin: 0,
padding: 0,
width: "80px"
}
},
menu: {
background: theme.palette.background.paper,
height: "100vh",
padding: "25px 20px"
},
menuIcon: {
"& span": {
"&:nth-child(1)": {
top: 15
},
"&:nth-child(2), &:nth-child(3)": {
top: 20
},
"&:nth-child(4)": {
top: 25
},
background: theme.palette.secondary.light,
display: "block",
height: 1,
left: "20%",
opacity: 1,
position: "absolute",
transform: "rotate(0deg)",
transition: ".25s ease-in-out",
width: "60%"
},
[theme.breakpoints.up("md")]: {
display: "none"
},
[theme.breakpoints.down("sm")]: {
left: 0
},
background: theme.palette.background.paper,
borderRadius: "50%",
cursor: "pointer",
height: 42,
left: theme.spacing(),
marginRight: theme.spacing(2),
position: "relative",
transform: "rotate(0deg)",
transition: `${theme.transitions.duration.shorter}ms ease-in-out`,
width: 42
},
menuIconDark: {
"& span": {
background: theme.palette.common.white
}
},
menuIconOpen: {
"& span": {
"&:nth-child(1), &:nth-child(4)": {
left: "50%",
top: 20,
width: 0
},
"&:nth-child(2)": {
transform: "rotate(45deg)"
},
"&:nth-child(3)": {
transform: "rotate(-45deg)"
}
},
left: 280,
position: "absolute",
zIndex: 1999
},
menuSmall: {
background: theme.palette.background.paper,
height: "100vh",
overflow: "hidden",
padding: 25
},
popover: {
Multichannel (#833) * add multichannel to configuration view * create multichannels list view * create multichannels list view * add ChannelsCreate view * update channels in configuration * add stories * update default messages * fix ChannelForm props * update channels list styles * update snapshots * update channel form currency input * update Channels fragments * extract messages * remove tabs from channelsList * channel details, channel delete modal (#598) * create Channel details view * create ChannelDeleteDialog * add channels delete dialog to channels list * update messages and types * fixes after review * channels availability (#609) * create Channel details view * update messages and types * create ChannelsAvailability component * create more product channels components * create channels stories, update fixtures, types * update product views with channels data * update schema and snapshots * update defaultMessages * update ProductUpdate view * create ChannelsAvailabilityDropdown component * add product channels to local storage * update globalTypes * Update to new schema and resolve issues * Update messages * create deleteChannel mutation * add channels availability component to product create view * refactor ProductCreate and ProductUpdate views * CollectionProducts view cleanup * add disabled prop to ActionDialog * use updateChannels mutation in ProductCreate view * ProductCreate - update submit function * fixes after review * update snapshots and messages Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * channels shipping components (#655) * create PricingCard, OrderValue and OrderWeight components * create ShippingZoneRatesPage and DeleteShippingRateDialog * update ChannelsAvailability component * updates after review * channels shipping views (#662) * update ChannelsAvailability component * updates after review * create PriceRate views, update types * create weight rates views * update shipping views, stories, messages * update snapshots * update snapshots * update useChannels hook * orders channels components (#698) * create OrderChannelSectionCard component * update OrderDetailsPage * update DraftOrderChannelCard * update snapshots * update fixtures * small change after review, update snapshots * product pricing (#702) * update product types * update Pricing in simple product view * use productVariantCreate mutation in simple product view * update snapshots and messages * handle create variant (#715) * update product types * update Pricing in simple product view * handle product create and update errors * update snapshots and messages * fix update and create product handlers * update pricing types * channels modal - new styles, search input (#717) * update product types * update Pricing in simple product view * handle product create and update errors * update pricing types * add search input in ChannelsAvailabilityDialog * update ChannelsAvailabilityDialog in all views * update snapshots * fix search input label styles * update toggleAllChannels function * update variant creator (#724) * update product types * update Pricing in simple product view * handle product create and update errors * update pricing types * add search input in ChannelsAvailabilityDialog * update ChannelsAvailabilityDialog in all views * update snapshots * add channelLisitngs to variant creator * update variant creator price styles * update product variant creator reducer tests * update createVariants tests * update error handling in product variant creator * add Skip pricing for now option * use PriceField instead of TextField in ProductVariantCreatorSummary * create price validation function * fix errors handling in ProductVariantPrice component * fixes after review * Product List - remove publish/unpublish buttons (#727) * ProductList - remove publish and unpublish buttons * update messages * update snapshots, messages * revert changes in ChannelsAvailabilityDropdown * products/shipping/discount list settings (#739) * create ChannelSettingsDialog component * update snapshots * ProductList - open settings modal when there is no selected channel * add settings modal to vouchers list * add settings dialog to sales list * add setting modal to shipping list * update shipping * update snapshots, messages * useChannelsSettings - remove selectedChannelSlug * fix channels update handler in product and shipping view * messages update in ChannelSettingsDialog * handle product/discount list when there is no channels * update onSettingsOpen prop * collection availability dropdown (#743) * add availability dropdown to collection products list * update channelListingProduct fragment name * update voucher view/components with channels (#746) * update voucher view/components with channels * update VoucherSummary, remove defaultCurrency from voucher components * update snapshots * move getChannelsVariables func to discounts handlers * update voucher messages * sale view/components with channels (#750) * update sale views with channels * small fixes in discounts * order views with channels (#752) * update draft orders with channels * add channel activate/deactivate mutations * remove sort by total in orders list * add error notification on channel activate/deactivate * product variants channel select (#755) * add channels selector to ProductVariants component * remove selectedChannel from ProductUpdate, update messages and snapshots * update product fragments * update translations (#762) * update translations * fix translation types * update messages * update Availability component (#766) * update ChannelsAvailability component * update product fixtures * update collection and channel fixtures * ChannelsAvailability - handle errors * update product handlers * update ChannelsAvailability styles * update ProductVariant * update snapshots * fix missing things in multichannel (#785) * add availability dropdown to discount products list * fix error handling in shipping components * update product views and components * update messages * update category view/components * update CategoryProducts styles * remove defaultCurrency from shipping components * create ChannelsSelect component * update channels error handling after review * another fixes after review * Add channels to collection views/components (#791) ** update collection components and views * update create collection view * update error handling in collection * remove filter bar from collection list * update products fragments * small fix in collection create view * use collectionFragment in useCatalogSearch * update defaultMessages and snapshots * update homepage view/drop defaultCurrency (#801) * update homepage view * drop defaultCurrency prop * fix onChannelChange function in home view * remove visibility from product list filters * update export products with channels (#803) * update ProductExportDialog with channels * add new channel error code * remover VISIBLE from product export dialog Financial information * fix input size in ProductVariantCreatorSummary (#804) * channels currency code select (#806) * create select with currency codes * fix ChannelDeleteDialog * update defaultMessages, remove unneeded ChannelDetails handlers * fixes after rebase * replace channelListing with channelListings * [multichannel] Update schema] * Fix product create test Co-authored-by: AlicjaSzu <alicja.szukuc@gmail.com> Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> Co-authored-by: Jakub Majorek <majorek.jakub@gmail.com>
2020-11-17 16:10:42 +00:00
zIndex: 2
},
2019-06-19 14:40:52 +00:00
root: {
2019-08-09 11:14:35 +00:00
width: `100%`
2019-06-19 14:40:52 +00:00
},
rotate: {
transform: "rotate(180deg)"
},
sideBar: {
[theme.breakpoints.down("sm")]: {
padding: 0
},
background: theme.palette.background.paper,
padding: `0 ${theme.spacing(4)}px`
},
2019-06-19 14:40:52 +00:00
spacer: {
flex: 1
},
userBar: {
[theme.breakpoints.down("sm")]: {
alignItems: "flex-end",
flexDirection: "column-reverse",
overflow: "hidden"
},
2019-06-19 14:40:52 +00:00
alignItems: "center",
display: "flex"
},
userChip: {
backgroundColor: theme.palette.background.paper,
borderRadius: 24,
color: theme.palette.text.primary,
height: 40,
padding: theme.spacing(0.5)
},
userMenuContainer: {
position: "relative"
},
userMenuItem: {
textAlign: "right"
},
2019-06-19 14:40:52 +00:00
view: {
backgroundColor: theme.palette.background.default,
2019-06-19 14:40:52 +00:00
flex: 1,
flexGrow: 1,
marginLeft: 0,
2019-10-28 16:16:49 +00:00
paddingBottom: theme.spacing(),
2019-10-23 15:10:46 +00:00
[theme.breakpoints.up("sm")]: {
2019-10-28 16:16:49 +00:00
paddingBottom: theme.spacing(3)
2019-10-23 15:10:46 +00:00
}
},
viewContainer: {
minHeight: `calc(100vh - ${theme.spacing(2) + appLoaderHeight + 70}px)`
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
}),
{
name: "AppLayout"
}
);
2019-06-19 14:40:52 +00:00
interface AppLayoutProps {
children: React.ReactNode;
}
2019-11-14 14:10:52 +00:00
const AppLayout: React.FC<AppLayoutProps> = ({ children }) => {
const classes = useStyles({});
const { isDark, toggleTheme } = useTheme();
const [isMenuSmall, setMenuSmall] = useLocalStorage("isMenuSmall", false);
const [isDrawerOpened, setDrawerState] = React.useState(false);
const [isMenuOpened, setMenuState] = React.useState(false);
2019-11-14 14:10:52 +00:00
const appActionAnchor = React.useRef<HTMLDivElement>();
const appHeaderAnchor = React.useRef<HTMLDivElement>();
const anchor = React.useRef<HTMLDivElement>();
2019-11-14 14:10:52 +00:00
const { logout, user } = useUser();
const navigate = useNavigator();
const intl = useIntl();
const [appState, dispatchAppState] = useAppState();
const { location } = useRouter();
2020-08-08 12:59:09 +00:00
const [isNavigatorVisible, setNavigatorVisibility] = React.useState(false);
2020-10-14 08:17:23 +00:00
const [docked, setDocked] = React.useState(true);
2019-08-29 10:55:56 +00:00
2019-11-14 14:10:52 +00:00
const menuStructure = createMenuStructure(intl);
const configurationMenu = createConfigurationMenu(intl);
const userPermissions = user?.userPermissions || [];
2019-10-09 15:14:23 +00:00
2019-11-14 14:10:52 +00:00
const renderConfigure = configurationMenu.some(section =>
section.menuItems.some(
menuItem =>
!!userPermissions.find(
userPermission => userPermission.code === menuItem.permission
)
)
);
2019-06-19 14:40:52 +00:00
const handleLogout = () => {
setMenuState(false);
logout();
};
const handleViewerProfile = () => {
setMenuState(false);
navigate(staffMemberDetailsUrl(user.id));
};
const handleMenuItemClick = (url: string, event: React.MouseEvent<any>) => {
event.stopPropagation();
event.preventDefault();
setDrawerState(false);
navigate(url);
};
const handleIsMenuSmall = () => {
setMenuSmall(!isMenuSmall);
};
2019-11-14 14:10:52 +00:00
const handleErrorBack = () => {
navigate("/");
dispatchAppState({
payload: {
error: null
},
type: "displayError"
});
};
return (
2020-08-08 12:59:09 +00:00
<>
<Navigator
visible={isNavigatorVisible}
setVisibility={setNavigatorVisibility}
/>
<AppHeaderContext.Provider value={appHeaderAnchor}>
2020-10-14 08:17:23 +00:00
<AppActionContext.Provider
value={{
anchor: appActionAnchor,
docked,
setDocked
}}
>
2020-08-08 12:59:09 +00:00
<div className={classes.root}>
<div className={classes.sideBar}>
<ResponsiveDrawer
onClose={() => setDrawerState(false)}
open={isDrawerOpened}
small={!isMenuSmall}
>
<div
className={classNames(classes.logo, {
[classes.logoSmall]: isMenuSmall,
[classes.logoDark]: isDark
})}
>
<SVG
src={isMenuSmall ? saleorDarkLogoSmall : saleorDarkLogo}
/>
</div>
<Hidden smDown>
<div
className={classNames(classes.isMenuSmall, {
[classes.isMenuSmallHide]: isMenuSmall,
[classes.isMenuSmallDark]: isDark
})}
onClick={handleIsMenuSmall}
>
<SVG src={menuArrowIcon} />
</div>
</Hidden>
<MenuList
className={isMenuSmall ? classes.menuSmall : classes.menu}
menuItems={menuStructure}
isMenuSmall={!isMenuSmall}
location={location.pathname}
user={user}
renderConfigure={renderConfigure}
onMenuItemClick={handleMenuItemClick}
/>
</ResponsiveDrawer>
</div>
<div
className={classNames(classes.content, {
[classes.contentToggle]: isMenuSmall
})}
>
2020-08-08 12:59:09 +00:00
{appState.loading ? (
<LinearProgress className={classes.appLoader} color="primary" />
) : (
<div className={classes.appLoaderPlaceholder} />
)}
<div className={classes.viewContainer}>
<div>
<Container>
<div className={classes.header}>
<div
className={classNames(classes.menuIcon, {
[classes.menuIconOpen]: isDrawerOpened,
[classes.menuIconDark]: isDark
})}
onClick={() => setDrawerState(!isDrawerOpened)}
>
<span />
<span />
<span />
<span />
</div>
<div ref={appHeaderAnchor} />
<div className={classes.spacer} />
<div className={classes.userBar}>
<ThemeSwitch
className={classes.darkThemeSwitch}
checked={isDark}
onClick={toggleTheme}
/>
<Hidden smDown>
<NavigatorButton
isMac={navigator.platform
.toLowerCase()
.includes("mac")}
onClick={() => setNavigatorVisibility(true)}
/>
</Hidden>
<div className={classes.userMenuContainer} ref={anchor}>
<Chip
avatar={
user.avatar && (
<Avatar alt="user" src={user.avatar.url} />
)
}
classes={{
avatar: classes.avatar
}}
className={classes.userChip}
label={
<>
{user.email}
<ArrowDropdown
className={classNames(classes.arrow, {
[classes.rotate]: isMenuOpened
})}
/>
</>
}
onClick={() => setMenuState(!isMenuOpened)}
data-test="userMenu"
/>
<Popper
className={classes.popover}
open={isMenuOpened}
anchorEl={anchor.current}
transition
placement="bottom-end"
>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{
transformOrigin:
placement === "bottom"
? "right top"
: "right bottom"
}}
>
<Paper>
<ClickAwayListener
onClickAway={() => setMenuState(false)}
mouseEvent="onClick"
>
<Menu>
<MenuItem
className={classes.userMenuItem}
onClick={handleViewerProfile}
data-test="accountSettingsButton"
>
<FormattedMessage
defaultMessage="Account Settings"
description="button"
/>
</MenuItem>
<MenuItem
className={classes.userMenuItem}
onClick={handleLogout}
data-test="logOutButton"
>
<FormattedMessage
defaultMessage="Log out"
description="button"
/>
</MenuItem>
</Menu>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</div>
2019-11-14 14:10:52 +00:00
</div>
2019-06-19 14:40:52 +00:00
</div>
2020-08-08 12:59:09 +00:00
</Container>
</div>
<main className={classes.view}>
{appState.error
? appState.error === "unhandled" && (
<ErrorPage onBack={handleErrorBack} />
)
: children}
</main>
2019-10-30 14:34:24 +00:00
</div>
2020-10-14 08:17:23 +00:00
<div
className={classNames(classes.appAction, {
[classes.appActionDocked]: docked
})}
ref={appActionAnchor}
/>
2019-11-14 14:10:52 +00:00
</div>
</div>
2020-08-08 12:59:09 +00:00
</AppActionContext.Provider>
</AppHeaderContext.Provider>
</>
2019-11-14 14:10:52 +00:00
);
};
2019-06-19 14:40:52 +00:00
export default AppLayout;