Skip to content

Commit d0be987

Browse files
committed
redirect comment page to app
1 parent a790b4b commit d0be987

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

app/containers/App/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { hot } from 'react-hot-loader/root';
1313
import { Switch, Route, Redirect, useLocation } from 'react-router-dom';
1414
import CritterpediaPage from 'pages/Critterpedia/loadable';
1515
import NotFoundPage from 'pages/404';
16+
import DisqusRedirect from 'pages/DisqusRedirect';
1617
import { useInjectReducer } from 'redux-injectors';
1718
import { CacheProvider } from '@emotion/core';
1819
import createCache from '@emotion/cache';
@@ -57,6 +58,7 @@ const App = () => {
5758
path={`/:mode(${MODE_COLLECTION}|${MODE_DISCOVERY}|${MODE_ALL})/:category(${CATEGORY_FISH}|${CATEGORY_INSECTS}|${CATEGORY_SEA})`}
5859
component={CritterpediaPage}
5960
/>
61+
<Route path={`/comment-:commentId(\\d+)`} component={DisqusRedirect} />
6062
<Route component={NotFoundPage} />
6163
</Switch>
6264
{/* the global modals */}

app/pages/DisqusRedirect/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useEffect } from 'react';
2+
import { useHistory } from 'react-router-dom';
3+
import { waitAndScrollByElement } from './utils';
4+
5+
/**
6+
* Redirect to disqus comment when it's ready /comment-5621046783
7+
*/
8+
const DisqusRedirect = () => {
9+
// TODO: redirect to comment id
10+
// const { commentId } = useParams();
11+
const history = useHistory();
12+
useEffect(() => {
13+
history.push('/');
14+
return () => {
15+
waitAndScrollByElement('#disqus_thread').catch(() => {});
16+
};
17+
}, []);
18+
19+
return <></>;
20+
};
21+
22+
DisqusRedirect.propTypes = {};
23+
24+
export default DisqusRedirect;

app/pages/DisqusRedirect/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const waitAndScrollByElement = (selector, interval = 1000, retry = 10) =>
2+
new Promise((resolve, reject) => {
3+
let remainingRetry = retry;
4+
const waitingForElement = () => {
5+
remainingRetry -= 1;
6+
const element = document.querySelector(selector);
7+
if (element) {
8+
element.scrollIntoView();
9+
resolve(element);
10+
} else if (remainingRetry > 0) {
11+
setTimeout(waitingForElement, interval);
12+
} else {
13+
reject(new Error('element not found'));
14+
}
15+
};
16+
waitingForElement();
17+
});

0 commit comments

Comments
 (0)