Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 46ca268

Browse files
Merge pull request #12 from thefringeninja/fix-hoc-names
Higher Order Components Refactor
2 parents 8db83ba + b69af87 commit 46ca268

File tree

16 files changed

+107
-112
lines changed

16 files changed

+107
-112
lines changed

src/SqlStreamStoreBrowser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
mount,
77
withAuthorization,
88
AuthorizationProvider,
9+
NavigationProvider,
910
Notifications,
1011
Loading,
1112
} from './components';
@@ -70,8 +71,10 @@ const SqlStreamStoreBrowser = withAuthorization()(
7071
<CssBaseline />
7172
<Hero />
7273
<Loading open={loading} />
73-
<Viewer {...props} onNavigate={onNavigate} />
74-
<Notifications />
74+
<NavigationProvider onNavigate={onNavigate}>
75+
<Viewer {...props} />
76+
<Notifications />
77+
</NavigationProvider>
7578
</div>
7679
</MuiThemeProvider>
7780
)),
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import getDisplayName from './getDisplayName';
23

34
const { Consumer, Provider } = React.createContext();
45

@@ -8,10 +9,19 @@ const AuthorizationProvider = ({ authorization, children }) => (
89

910
export default AuthorizationProvider;
1011

11-
export const withAuthorization = () => WrappedComponent => props => (
12-
<Consumer>
13-
{authorization => (
14-
<WrappedComponent {...props} authorization={authorization} />
15-
)}
16-
</Consumer>
17-
);
12+
const withAuthorization = () => WrappedComponent => {
13+
const Component = props => (
14+
<Consumer>
15+
{authorization => (
16+
<WrappedComponent {...props} authorization={authorization} />
17+
)}
18+
</Consumer>
19+
);
20+
Component.displayName = getDisplayName(
21+
'WithAuthorization',
22+
WrappedComponent,
23+
);
24+
return Component;
25+
};
26+
27+
export { withAuthorization };

src/components/HyperMediaControls/LinkButton.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { PureComponent } from 'react';
22
import { TextField } from '@material-ui/core';
33
import uriTemplate from 'uri-template';
4-
import { withAuthorization } from '../AuthorizationProvider';
4+
import { withNavigation } from '../NavigationProvider';
55
import Dialog from './Dialog';
66
import RelButton from './RelButton';
77
import { preventDefault } from '../../utils';
88

9-
const TemplatedLinkButton = withAuthorization()(
9+
const TemplatedLinkButton = withNavigation()(
1010
class TemplatedLinkButton extends PureComponent {
1111
state = {};
1212

@@ -50,7 +50,7 @@ const TemplatedLinkButton = withAuthorization()(
5050
},
5151
);
5252

53-
const NonTemplatedLinkButton = withAuthorization()(
53+
const NonTemplatedLinkButton = withNavigation()(
5454
({ link, rel, authorization, onNavigate }) => (
5555
<RelButton
5656
rel={rel}

src/components/HyperMediaControls/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const isNotSelf = (rel, links) =>
1111

1212
const state$ = createState(store.url$.map(href => ['href', () => href]));
1313

14-
const HyperMediaControls = ({ forms, href, actions, links, onNavigate }) => (
14+
const HyperMediaControls = ({ forms, href, actions, links }) => (
1515
<Card>
1616
<CardActions>
1717
<div>
@@ -23,7 +23,6 @@ const HyperMediaControls = ({ forms, href, actions, links, onNavigate }) => (
2323
key={rel}
2424
rel={rel}
2525
link={links[rel][0]}
26-
onNavigate={onNavigate}
2726
color={'active'}
2827
curies={links[rels.curies]}
2928
/>

src/components/Hyperlink.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { withStyles } from '@material-ui/core';
33
import { preventDefault } from '../utils';
4-
import { withAuthorization } from './AuthorizationProvider';
4+
import { withNavigation } from './NavigationProvider';
55
import theme from '../theme';
66

77
const color = theme.palette.action.active;
@@ -20,7 +20,7 @@ const styles = {
2020
},
2121
},
2222
};
23-
const Hyperlink = withAuthorization()(
23+
const Hyperlink = withNavigation()(
2424
withStyles(styles)(
2525
({ classes, link, children, authorization, onNavigate }) => (
2626
<a

src/components/NavigationLinks.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React, { PureComponent } from 'react';
22
import { IconButton } from '@material-ui/core';
33
import RelIcon from './RelIcon';
4-
import { withAuthorization } from './AuthorizationProvider';
4+
import { withNavigation } from './NavigationProvider';
55
import { navigation } from '../stream-store';
6-
import { preventDefault } from '../utils';
76

8-
const FeedNavigationLink = withAuthorization()(
9-
class FeedNavigationLink extends PureComponent {
7+
const FeedNavigationLink = withNavigation()(
8+
class What extends PureComponent {
109
_handleOnClick = e => {
1110
const { onNavigate, authorization, link } = this.props;
1211

@@ -34,13 +33,12 @@ const FeedNavigationLink = withAuthorization()(
3433
},
3534
);
3635

37-
const NavigationLinks = ({ onNavigate, links }) => (
36+
const NavigationLinks = ({ links }) => (
3837
<nav>
3938
{[...navigation].map(rel => (
4039
<FeedNavigationLink
4140
key={rel}
4241
link={(links[rel] || [])[0]}
43-
onNavigate={onNavigate}
4442
rel={rel}
4543
/>
4644
))}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import getDisplayName from './getDisplayName';
3+
const { Consumer, Provider } = React.createContext();
4+
5+
const NavigationProvider = ({ onNavigate, children }) => (
6+
<Provider value={onNavigate}>{children}</Provider>
7+
);
8+
9+
export default NavigationProvider;
10+
11+
const withNavigation = () => WrappedComponent => {
12+
const Component = props => (
13+
<Consumer>
14+
{onNavigate => (
15+
<WrappedComponent {...props} onNavigate={onNavigate} />
16+
)}
17+
</Consumer>
18+
);
19+
Component.displayName = getDisplayName('WithNavigation', WrappedComponent);
20+
return Component;
21+
};
22+
23+
export { withNavigation };

src/components/StreamBrowser.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const StreamBrowser = withStyles(theme => ({
1313
browser: {
1414
padding: theme.spacing.unit * 2.5,
1515
},
16-
}))(({ streams, onNavigate, classes, loading }) => (
16+
}))(({ streams, classes, loading }) => (
1717
<div className={classes.browser}>
1818
<Typography variant={'title'}>Stream Browser</Typography>
1919
{loading ? (
@@ -23,9 +23,7 @@ const StreamBrowser = withStyles(theme => ({
2323
{streams.map(link => (
2424
<ListItem key={link.href}>
2525
<ListItemText>
26-
<Hyperlink link={link} onNavigate={onNavigate}>
27-
{link.title}
28-
</Hyperlink>
26+
<Hyperlink link={link}>{link.title}</Hyperlink>
2927
</ListItemText>
3028
</ListItem>
3129
))}

src/components/getDisplayName.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default (hocName, WrappedComponent) =>
2+
`${hocName}(${WrappedComponent.displayName ||
3+
WrappedComponent.name ||
4+
'Component'})`;

src/components/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export { default as Hyperlink } from './Hyperlink';
66
export { default as HyperMediaControls } from './HyperMediaControls';
77
export { default as Loading } from './Loading';
88
export { default as NavigationLinks } from './NavigationLinks';
9+
export {
10+
default as NavigationProvider,
11+
withNavigation,
12+
} from './NavigationProvider';
913
export { default as Notifications } from './Notifications';
1014
export { default as StreamBrowser } from './StreamBrowser';
1115
export { default as mount } from './mount';

0 commit comments

Comments
 (0)