Skip to content

Commit 03d3117

Browse files
committed
fix(preview-sidebar): fix preview versions behavior
1 parent a79e193 commit 03d3117

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

src/constants.js

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ export const SIDEBAR_VIEW_METADATA: 'metadata' = 'metadata';
394394
export const SIDEBAR_VIEW_ACTIVITY: 'activity' = 'activity';
395395
export const SIDEBAR_VIEW_VERSIONS: 'versions' = 'versions';
396396

397+
/* ------------------ Sidebar Path ---------------------- */
398+
export const SIDEBAR_PATH_VERSIONS = '/:sidebar(activity|details)/versions/:versionId?';
399+
397400
/* ------------------ HTTP Requests ---------------------- */
398401
export const HTTP_GET: 'GET' = 'GET';
399402
export const HTTP_POST: 'POST' = 'POST';

src/elements/content-sidebar/Sidebar.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import flow from 'lodash/flow';
1010
import getProp from 'lodash/get';
1111
import noop from 'lodash/noop';
1212
import uniqueid from 'lodash/uniqueId';
13-
import { withRouter } from 'react-router-dom';
13+
import { matchPath, withRouter } from 'react-router-dom';
1414
import type { Location, RouterHistory } from 'react-router-dom';
1515
import LoadingIndicator from '../../components/loading-indicator/LoadingIndicator';
1616
import LocalStore from '../../utils/LocalStore';
1717
import SidebarNav from './SidebarNav';
1818
import SidebarPanels from './SidebarPanels';
1919
import SidebarUtils from './SidebarUtils';
20+
import { SIDEBAR_PATH_VERSIONS } from '../../constants';
2021
import { withCurrentUser } from '../common/current-user';
2122
import { withFeatureConsumer } from '../common/feature-checking';
2223
import type { FeatureConfig } from '../common/feature-checking';
@@ -97,7 +98,7 @@ class Sidebar extends React.Component<Props, State> {
9798
}
9899

99100
componentDidUpdate(prevProps: Props): void {
100-
const { fileId, history, location }: Props = this.props;
101+
const { fileId, history, location, onVersionChange }: Props = this.props;
101102
const { fileId: prevFileId, location: prevLocation }: Props = prevProps;
102103
const { isDirty }: State = this.state;
103104

@@ -111,8 +112,18 @@ class Sidebar extends React.Component<Props, State> {
111112
this.setForcedByLocation();
112113
this.setState({ isDirty: true });
113114
}
115+
116+
// Reset the current version id if the wrapping versions route is no longer active
117+
if (onVersionChange && this.getVersionsMatchPath(prevLocation) && !this.getVersionsMatchPath(location)) {
118+
onVersionChange(null);
119+
}
114120
}
115121

122+
getVersionsMatchPath = (location: Location) => {
123+
const pathname = getProp(location, 'pathname', '');
124+
return matchPath(pathname, SIDEBAR_PATH_VERSIONS);
125+
};
126+
116127
getUrlPrefix = (pathname: string) => {
117128
const basePath = pathname.substring(1).split('/')[0];
118129
return basePath;

src/elements/content-sidebar/SidebarPanels.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ORIGIN_METADATA_SIDEBAR,
1919
ORIGIN_SKILLS_SIDEBAR,
2020
ORIGIN_VERSIONS_SIDEBAR,
21+
SIDEBAR_PATH_VERSIONS,
2122
SIDEBAR_VIEW_ACTIVITY,
2223
SIDEBAR_VIEW_DETAILS,
2324
SIDEBAR_VIEW_METADATA,
@@ -250,7 +251,7 @@ class SidebarPanels extends React.Component<Props, State> {
250251
)}
251252
{hasVersions && (
252253
<Route
253-
path="/:sidebar(activity|details)/versions/:versionId?"
254+
path={SIDEBAR_PATH_VERSIONS}
254255
render={({ match }) => (
255256
<LoadableVersionsSidebar
256257
fileId={fileId}

src/elements/content-sidebar/__tests__/Sidebar.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ describe('elements/content-sidebar/Sidebar', () => {
7474
wrapper.setProps({ location: { pathname: '/', state: { open: false } } });
7575
expect(instance.isForced).toHaveBeenCalledWith(false);
7676
});
77+
78+
test('should reset the current version if the versions route is no longer active', () => {
79+
const onVersionChange = jest.fn();
80+
const wrapper = getWrapper({ location: { pathname: '/activity' }, onVersionChange });
81+
82+
wrapper.setProps({ location: { pathname: '/activity/versions/123', state: { open: true } } });
83+
expect(onVersionChange).not.toBeCalled();
84+
85+
wrapper.setProps({ location: { pathname: '/activity/versions/123', state: { open: false } } });
86+
expect(onVersionChange).not.toBeCalled();
87+
88+
wrapper.setProps({ location: { pathname: '/activity/versions/456', state: { open: true } } });
89+
expect(onVersionChange).not.toBeCalled();
90+
91+
wrapper.setProps({ location: { pathname: '/details' } });
92+
expect(onVersionChange).lastCalledWith(null);
93+
});
7794
});
7895

7996
describe('handleVersionHistoryClick', () => {

src/elements/content-sidebar/versions/VersionsSidebarContainer.js

-5
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
9595
}
9696
}
9797

98-
componentWillUnmount() {
99-
// Reset the current version id since the wrapping route is no longer active
100-
this.props.onVersionChange(null);
101-
}
102-
10398
handleActionDelete = (versionId: string): Promise<void> => {
10499
this.setState({ isLoading: true });
105100

src/elements/content-sidebar/versions/__tests__/VersionsSidebarContainer.test.js

-11
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ describe('elements/content-sidebar/versions/VersionsSidebarContainer', () => {
5858
});
5959
});
6060

61-
describe('componentWillUnmount', () => {
62-
test('should forward verison id reset to the parent component', () => {
63-
const onVersionChange = jest.fn();
64-
const wrapper = getWrapper({ onVersionChange });
65-
66-
wrapper.instance().componentWillUnmount();
67-
68-
expect(onVersionChange).toBeCalledWith(null);
69-
});
70-
});
71-
7261
describe('componentDidMount', () => {
7362
test('should call onLoad after a successful fetchData() call', async () => {
7463
const onLoad = jest.fn();

0 commit comments

Comments
 (0)