Skip to content

Commit fff4a49

Browse files
committed
migration
1 parent d1c28b6 commit fff4a49

File tree

5 files changed

+55
-81
lines changed

5 files changed

+55
-81
lines changed

front/src/components/scrollToTop/ScrollToTop.js

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { Fragment, useEffect, useRef } from 'react';
2+
import { withRouter, RouteComponentProps } from 'react-router';
3+
import { Location } from 'history';
4+
5+
// #region types
6+
type Props = {
7+
children: any;
8+
} & RouteComponentProps;
9+
// #endregion
10+
11+
function useScrollToTopOnLocationChange(location: any) {
12+
const prevLocation = useRef<Location | null>(null);
13+
14+
useEffect(() => {
15+
prevLocation.current = location;
16+
}, []);
17+
18+
useEffect(
19+
() => {
20+
if (prevLocation.current !== location) {
21+
window && window.scrollTo(0, 0);
22+
prevLocation.current = location;
23+
}
24+
},
25+
[location],
26+
);
27+
}
28+
29+
function ScrollToTop({ children, location }: Props) {
30+
useScrollToTopOnLocationChange(location);
31+
32+
return <Fragment>{children}</Fragment>;
33+
}
34+
35+
ScrollToTop.displayName = 'ScrollToTop';
36+
37+
export default withRouter(ScrollToTop);

front/src/components/scrollToTop/___tests___/ScrolToTop.test.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
import { MemoryRouter } from 'react-router';
4+
import ScrollToTop from '../ScrollToTop';
5+
6+
describe('ScrollToTop component', () => {
7+
it('renders as expected', () => {
8+
const component = shallow(
9+
<MemoryRouter>
10+
<ScrollToTop>
11+
<p>a child</p>
12+
</ScrollToTop>
13+
</MemoryRouter>,
14+
);
15+
16+
expect(component).toMatchSnapshot();
17+
});
18+
});

front/src/components/scrollToTop/___tests___/__snapshots__/ScrolToTop.test.js.snap

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)