-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from outgrow/outgrow-shop-selector
Add global shop selector for multi-shop usage
- Loading branch information
Showing
40 changed files
with
459 additions
and
1,282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
imports/client/ui/components/ShopSelectorWithData/ShopSelectorWithData.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { compose } from "recompose"; | ||
import classNames from "classnames"; | ||
import { Link, useLocation } from "react-router-dom"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { faPlus } from "@fortawesome/free-solid-svg-icons"; | ||
import InputBase from "@material-ui/core/InputBase"; | ||
import MenuItem from "@material-ui/core/MenuItem"; | ||
import Select from "@material-ui/core/Select"; | ||
import Typography from "@material-ui/core/Typography"; | ||
import withStyles from "@material-ui/core/styles/withStyles"; | ||
import { withComponents } from "@reactioncommerce/components-context"; | ||
|
||
const defaultLogo = "/resources/reaction-logo-circular.svg"; | ||
|
||
const styles = (theme) => ({ | ||
root: { | ||
display: "flex", | ||
alignItems: "center" | ||
}, | ||
logo: { | ||
marginRight: theme.spacing(2) | ||
}, | ||
menuItem: { | ||
"& a span": { | ||
color: theme.palette.colors.black90 | ||
} | ||
}, | ||
selectMenu: { | ||
"& a span": { | ||
color: theme.palette.colors.black15 | ||
} | ||
}, | ||
dummyShopName: { | ||
color: theme.palette.colors.black15 | ||
}, | ||
newShopLink: { | ||
"color": theme.palette.colors.darkBlue, | ||
"&:hover": { | ||
color: theme.palette.colors.darkBlue | ||
} | ||
}, | ||
plusIcon: { | ||
display: "flex", | ||
justifyContent: "center" | ||
}, | ||
selectArrow: { | ||
color: theme.palette.colors.black15 | ||
} | ||
}); | ||
|
||
const ShopSelectorInput = withStyles(() => ({ | ||
input: { | ||
"border": "none", | ||
"&:focus": { | ||
border: "none", | ||
background: "transparent" | ||
} | ||
} | ||
}))(InputBase); | ||
|
||
/** | ||
* ShopSelectorWithData | ||
* @param {Object} props Component props | ||
* @returns {Node} React component | ||
*/ | ||
function ShopSelectorWithData({ className, classes, shouldShowShopName, shopId, linkTo, size, viewer }) { | ||
const location = useLocation(); | ||
|
||
let adminUIShops = []; | ||
|
||
if (viewer?.adminUIShops) { | ||
({ adminUIShops } = viewer); | ||
} else { | ||
return ( | ||
<Link | ||
className={classNames(classes.root, className)} | ||
to={linkTo} | ||
> | ||
<img | ||
alt="Reaction Commerce" | ||
className={classes.logo} | ||
src={defaultLogo} | ||
width={size} | ||
/> | ||
{shouldShowShopName && | ||
<Typography | ||
className={classes.dummyShopName} | ||
component="span" | ||
variant="h3" | ||
> | ||
Reaction Commerce | ||
</Typography> | ||
} | ||
</Link> | ||
); | ||
} | ||
|
||
return ( | ||
<Select classes={{ selectMenu: classes.selectMenu, icon: classes.selectArrow }} value={shopId} input={<ShopSelectorInput />}> | ||
{adminUIShops.map((shop) => { | ||
const customLogoFromUpload = shop.brandAssets && shop.brandAssets.navbarBrandImage && shop.brandAssets.navbarBrandImage.large; | ||
const customLogoFromUrlInput = shop.shopLogoUrls && shop.shopLogoUrls.primaryShopLogoUrl; | ||
// Replace only the first segment of the URL with the shop ID and keep the rest (if any) | ||
const linkUrl = location.pathname.replace(/(\/.[^/]*(\/.*)?)/g, (match, firstUrlSegment, restOfUrl) => `/${shop._id}${restOfUrl || ""}`); | ||
|
||
return ( | ||
<MenuItem className={classes.menuItem} value={shop._id} key={shop._id}> | ||
<Link | ||
className={classNames(classes.root, className)} | ||
to={linkUrl} | ||
> | ||
<img | ||
alt={shop.name} | ||
className={classes.logo} | ||
src={customLogoFromUrlInput || customLogoFromUpload || defaultLogo} | ||
width={size} | ||
/> | ||
{shouldShowShopName && | ||
<Typography | ||
variant="h3" | ||
component="span" | ||
> | ||
{shop.name} | ||
</Typography> | ||
} | ||
</Link> | ||
</MenuItem> | ||
); | ||
})} | ||
<MenuItem className={classes.menuItem} value="new-shop" key="new-shop"> | ||
<Link | ||
className={classNames([classes.root, className, classes.newShopLink])} | ||
to={"/new-shop"} | ||
> | ||
<div className={classNames([classes.logo, classes.plusIcon])} style={{ width: size }}> | ||
<FontAwesomeIcon icon={faPlus} /> | ||
</div> | ||
<Typography | ||
variant="h3" | ||
component="span" | ||
> | ||
New Shop | ||
</Typography> | ||
</Link> | ||
</MenuItem> | ||
</Select> | ||
); | ||
} | ||
|
||
ShopSelectorWithData.propTypes = { | ||
className: PropTypes.string, | ||
classes: PropTypes.object, | ||
linkTo: PropTypes.string, | ||
shopId: PropTypes.string, | ||
shouldShowShopName: PropTypes.bool, | ||
size: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
viewer: PropTypes.object | ||
}; | ||
|
||
ShopSelectorWithData.defaultProps = { | ||
linkTo: "/", | ||
shouldShowShopName: false, | ||
size: 60 | ||
}; | ||
|
||
export default compose( | ||
withComponents, | ||
withStyles(styles, { name: "RuiShopSelectorWithData" }) | ||
)(ShopSelectorWithData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./ShopSelectorWithData"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.