Skip to content

Commit

Permalink
#317: Using hooks instead of components
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa committed Aug 1, 2019
1 parent 7ac6a76 commit 8cc2ad4
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 120 deletions.

This file was deleted.

30 changes: 0 additions & 30 deletions client/src/google-analytics/__tests__/google-analytics.test.jsx

This file was deleted.

4 changes: 4 additions & 0 deletions client/src/google-analytics/__tests__/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

describe('Google Analytics hooks test suite', () => {

});
21 changes: 0 additions & 21 deletions client/src/google-analytics/google-analytics.jsx

This file was deleted.

40 changes: 40 additions & 0 deletions client/src/google-analytics/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {useEffect} from 'react';
import get from 'lodash/get';
import isFunction from 'lodash/isFunction';
import {googleAnalyticsTrackingId} from './selectors';

const updateLocation = trackingId => {
if (isFunction(window.gtag)) {
window.gtag('config', trackingId, {
page_path: get(window, 'location.pathname')
});
}
};

export const useAnalytics = () => {
const trackingId = googleAnalyticsTrackingId();
if (trackingId) {
useEffect(() => {
const scriptLoader = document.createElement('script');
scriptLoader.async = true;
scriptLoader.src = `https://www.googletagmanager.com/gtag/js?id=${trackingId}`;
document.body.appendChild(scriptLoader);
window.gtag = function gtag() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(arguments);
};
window.gtag('js', new Date());
updateLocation(trackingId);
return () => document.body.removeChild(scriptLoader);
}, []);
}
};

export const updateAnalytics = () => {
const trackingId = googleAnalyticsTrackingId();
if (trackingId) {
useEffect(() => {
updateLocation(trackingId);
});
}
};
6 changes: 4 additions & 2 deletions client/src/google-analytics/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

export {default} from './google-analytics';
export {
useAnalytics,
updateAnalytics
} from './hooks';
2 changes: 0 additions & 2 deletions client/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import debounce from './services/debounce';
import i18n from './services/i18n';
import {loadState, saveState} from './services/state';
import Routes from './routes/routes';
import GoogleAnalytics from './google-analytics';
import rootReducer from './reducers';

const SAVE_STATE_DEBOUNCE_PERIOD_IN_MILLIS = 500;
Expand Down Expand Up @@ -36,7 +35,6 @@ async function init () {
<Routes />
</I18nextProvider>
</Provider>
<GoogleAnalytics />
</>,
document.getElementById('root')
);
Expand Down
58 changes: 17 additions & 41 deletions client/src/routes/__tests__/__snapshots__/routes.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Routes component test suite Snapshot render 1`] = `
<Router
basename="/"
history={
Object {
"action": "POP",
"block": [Function],
"createHref": [Function],
"go": [Function],
"goBack": [Function],
"goForward": [Function],
"length": 1,
"listen": [Function],
"location": Object {
"hash": "",
"pathname": "/",
"search": "",
"state": undefined,
},
"push": [Function],
"replace": [Function],
}
}
>
<Switch>
<Route
exact={true}
path="/configuration-not-found"
render={[Function]}
/>
<Connect(ApplicationReadyRoute)
exact={true}
path="/login"
render={[Function]}
/>
<Connect(ApplicationReadyRoute)
component={[Function]}
exact={true}
path="/"
/>
</Switch>
</Router>
<Switch>
<Route
exact={true}
path="/configuration-not-found"
render={[Function]}
/>
<Connect(ApplicationReadyRoute)
exact={true}
path="/login"
render={[Function]}
/>
<Connect(ApplicationReadyRoute)
component={[Function]}
exact={true}
path="/"
/>
</Switch>
`;
6 changes: 4 additions & 2 deletions client/src/routes/__tests__/routes.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import Routes from '../routes';

describe('Routes component test suite', () => {
test('Snapshot render', () => {
// When
// Given
const routes = shallow(<Routes />);
// When
const wrappedSwitch = routes.find('SwitchWrapper').dive();
// Then
expect(routes).toMatchSnapshot();
expect(wrappedSwitch).toMatchSnapshot();
});
});
12 changes: 10 additions & 2 deletions client/src/routes/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import ApplicationReadyRoute from './application-ready-route';
import App from '../components/app';
import ConfigurationNotFound from '../components/error-pages/configuration-not-found';
import Login from '../components/login/login';
import {useAnalytics, updateAnalytics} from '../google-analytics';
import '../styles/main.scss';

const Routes = () => (
<Router basename='/' history={history}>
const SwitchWrapper = () => {
useAnalytics();
updateAnalytics();
return (
<Switch>
<Route exact path="/configuration-not-found" render={() => <ConfigurationNotFound />} />
<ApplicationReadyRoute exact path="/login" render={() => <Login />} />
<ApplicationReadyRoute exact path="/" component={App} />
</Switch>
);
};
const Routes = () => (
<Router basename='/' history={history}>
<SwitchWrapper />
</Router>
);

Expand Down

0 comments on commit 8cc2ad4

Please sign in to comment.