Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Add meta context wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgrundas committed Jan 3, 2019
1 parent d7da5bb commit b219695
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 41 deletions.
35 changes: 31 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"react": "^16.4.1",
"react-addons-css-transition-group": "^15.6.2",
"react-dom": "^16.4.1",
"react-helmet": "^5.2.0",
"react-media": "^1.8.0",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
Expand All @@ -48,6 +49,7 @@
"@types/react": "^16.4.11",
"@types/react-addons-css-transition-group": "^15.0.5",
"@types/react-dom": "^16.0.7",
"@types/react-helmet": "^5.0.8",
"@types/react-router": "^4.0.30",
"@types/react-router-dom": "^4.3.0",
"@types/react-select": "^2.0.2",
Expand Down
40 changes: 25 additions & 15 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import * as React from "react";

import { ApolloConsumer } from "react-apollo";
import { Footer, MainMenu, MainMenuNavOverlay, MobileNav, SearchOverlay } from "..";
import {
Footer,
MainMenu,
MainMenuNavOverlay,
MetaConsumer,
MobileNav,
SearchOverlay
} from "..";
import { CartOverlay } from "../CartOverlay";
import CartProvider from "../CartProvider";
import { LoginOverlay } from "../LoginOverlay";
Expand All @@ -14,20 +21,23 @@ import "./scss/index.scss";
const App: React.SFC<{}> = () => (
<ApolloConsumer>
{client => (
<CartProvider apolloClient={client}>
<header>
<MainMenu />
</header>
<Routes />
<Footer />
<CartOverlay />
<LoginOverlay />
<PasswordOverlay />
<MobileNav />
<MainMenuNavOverlay />
<NotificationOverlay />
<SearchOverlay />
</CartProvider>
<>
<MetaConsumer />
<CartProvider apolloClient={client}>
<header>
<MainMenu />
</header>
<Routes />
<Footer />
<CartOverlay />
<LoginOverlay />
<PasswordOverlay />
<MobileNav />
<MainMenuNavOverlay />
<NotificationOverlay />
<SearchOverlay />
</CartProvider>
</>
)}
</ApolloConsumer>
);
Expand Down
17 changes: 17 additions & 0 deletions src/components/Meta/MetaWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from "react";

import { default as MetaConsumer } from "./consumer";
import { MetaContextInterface, Provider as MetaProvider } from "./context";

interface MetaWrapperProps {
meta: MetaContextInterface;
children: React.ReactNode;
}

const MetaWrapper: React.SFC<MetaWrapperProps> = ({ children, meta }) => (
<MetaProvider value={meta}>
<MetaConsumer>{children}</MetaConsumer>
</MetaProvider>
);

export default MetaWrapper;
30 changes: 30 additions & 0 deletions src/components/Meta/consumer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as React from "react";
import { Helmet } from "react-helmet";

import { Consumer as MetaConsumer } from "./context";

class Consumer extends React.Component {
render() {
return (
<MetaConsumer>
{({ title, description, image, type, url, custom }) => (
<>
<Helmet
title={title}
meta={[
{ name: "description", content: description },
{ property: "og:url", content: url },
{ property: "og:type", content: type },
{ property: "og:image", content: image },
...custom
]}
/>
{this.props.children}
</>
)}
</MetaConsumer>
);
}
}

export default Consumer;
17 changes: 17 additions & 0 deletions src/components/Meta/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from "react";
import { META_DEFAULTS } from "../../core/config";

type MetaProps = JSX.IntrinsicElements["meta"];

export interface MetaContextInterface {
title?: string;
description?: string;
url?: string;
image?: string;
type?: string;
custom?: MetaProps[];
}

export const { Provider, Consumer } = React.createContext<MetaContextInterface>(
META_DEFAULTS
);
3 changes: 3 additions & 0 deletions src/components/Meta/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { Provider as MetaProvider, MetaContextInterface } from "./context";
export { default as MetaConsumer } from "./consumer";
export { default as MetaWrapper } from "./MetaWrapper";
10 changes: 10 additions & 0 deletions src/components/Meta/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// import * as React from "react";
// import { Helment } from "react-helmet";

// import { Provider as MetaProvider } from "./context";

// class Provider extends React.Component {
// render() {
// return <MetaProvider>{this.props.children}</MetaProvider>;
// }
// }
11 changes: 3 additions & 8 deletions src/components/MobileNav/NavList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class NavList extends React.PureComponent<NavListProps, NavListState> {
parent: null
};


handleShowSubItems = (item: NavItem) => {
this.setState({ parent: item, displayedItems: item.children });
};
Expand Down Expand Up @@ -63,13 +62,9 @@ class NavList extends React.PureComponent<NavListProps, NavListState> {
return (
<ul>
{parent ? (
<li className="side-nav__menu-item side-nav__menu-item--parent">
<span
className="side-nav__menu-item-back"
onClick={this.handleGoBack}
>
<ReactSVG path={backIcon} />{" "}
{parent.name}
<li className="side-nav__menu-item side-nav__menu-item-back">
<span onClick={this.handleGoBack}>
<ReactSVG path={backIcon} /> {parent.name}
</span>
</li>
) : (
Expand Down
13 changes: 7 additions & 6 deletions src/components/MobileNav/scss/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

.side-nav {
$item-height: 3.7rem;
width: 30rem;
max-width: calc(100vw - 5rem);
width: 30rem;

&__menu-item {
border-bottom: 1px solid $gray-light;
display: grid;
height: $item-height;

&-link,
&-back {
&-back span {
align-items: center;
color: $base-font-color;
display: flex;
display: flex;
font-weight: 600;
padding: $spacer;
text-decoration: none;
Expand All @@ -27,7 +27,8 @@
}

&-logo {
padding: $spacer;
align-self: center;
padding: 0.75rem $spacer;

svg {
width: 5rem;
Expand Down Expand Up @@ -58,7 +59,8 @@
}

&-more {
padding: $spacer $spacer * 2;
align-self: center;
padding: 1.25rem 1.75rem;
}

&-back {
Expand Down Expand Up @@ -87,7 +89,6 @@

&--has-subnavigation,
&--parent {
display: grid;
grid-template-columns: 1fr $item-height;
}

Expand Down
6 changes: 6 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ export { MainMenu, MainMenuNavOverlay } from "./MainMenu";
export { MobileNav } from "./MobileNav";
export { NavLink } from "./NavLink";
export { SearchOverlay } from "./SearchOverlay";
export {
MetaConsumer,
MetaProvider,
MetaContextInterface,
MetaWrapper
} from "./Meta";
8 changes: 8 additions & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ export const SOCIAL_MEDIA = [
path: require("../images/youtube-icon.svg")
}
];

export const META_DEFAULTS = {
description: "PWA Storefront",
image: `${window.location.origin}${require("../images/logo.svg")}`,
title: "Saleor",
type: "website",
url: window.location.href
};
2 changes: 2 additions & 0 deletions src/views/Collection/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const collectionProductsQuery = gql`
id
slug
name
seoDescription
seoTitle
backgroundImage {
url
}
Expand Down
2 changes: 2 additions & 0 deletions src/views/Collection/types/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface Collection_collection {
id: string;
slug: string;
name: string;
seoDescription: string | null;
seoTitle: string | null;
backgroundImage: Collection_collection_backgroundImage | null;
}

Expand Down
Loading

0 comments on commit b219695

Please sign in to comment.