Skip to content

Commit c7a19e7

Browse files
Conrad Chanmergify[bot]
authored andcommitted
feat(sidebar): programmatic refresh (#1472)
* feat(sidebar): programmatic refresh * chore: update AsyncLoad to accept ref, address pr comments * chore: rename refresh to refreshIdentity * chore: destructure more efficiently * chore: adding unit tests * chore: fixing flow errors * chore: use forwardRef in AsyncLoad * chore: contentSidebarRef as prop * chore: fix flow for ref
1 parent 21271e3 commit c7a19e7

15 files changed

+197
-12
lines changed

src/elements/content-preview/ContentPreview.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import 'regenerator-runtime/runtime';
8-
import React, { PureComponent } from 'react';
8+
import * as React from 'react';
99
import classNames from 'classnames';
1010
import uniqueid from 'lodash/uniqueId';
1111
import throttle from 'lodash/throttle';
@@ -62,6 +62,7 @@ type Props = {
6262
collection: Array<string | BoxItem>,
6363
contentOpenWithProps: ContentOpenWithProps,
6464
contentSidebarProps: ContentSidebarProps,
65+
contentSidebarRef: React.Ref<any>,
6566
enableThumbnailsSidebar: boolean,
6667
features?: FeatureConfig,
6768
fileId?: string,
@@ -145,7 +146,7 @@ const LoadableSidebar = AsyncLoad({
145146
loader: () => import(/* webpackMode: "lazy", webpackChunkName: "content-sidebar" */ '../content-sidebar'),
146147
});
147148

148-
class ContentPreview extends PureComponent<Props, State> {
149+
class ContentPreview extends React.PureComponent<Props, State> {
149150
id: string;
150151

151152
props: Props;
@@ -1096,6 +1097,7 @@ class ContentPreview extends PureComponent<Props, State> {
10961097
messages,
10971098
className,
10981099
contentSidebarProps,
1100+
contentSidebarRef,
10991101
contentOpenWithProps,
11001102
hasHeader,
11011103
history,
@@ -1187,6 +1189,7 @@ class ContentPreview extends PureComponent<Props, State> {
11871189
getViewer={this.getViewer}
11881190
history={history}
11891191
language={language}
1192+
ref={contentSidebarRef}
11901193
sharedLink={sharedLink}
11911194
sharedLinkPassword={sharedLinkPassword}
11921195
requestInterceptor={requestInterceptor}

src/elements/content-sidebar/ActivitySidebar.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type PropsWithoutContext = {
4545
file: BoxItem,
4646
isDisabled: boolean,
4747
onVersionHistoryClick?: Function,
48+
refreshIdentity?: boolean,
4849
translations?: Translations,
4950
} & ExternalProps &
5051
WithLoggerProps;
@@ -96,6 +97,13 @@ class ActivitySidebar extends React.PureComponent<Props, State> {
9697
this.fetchCurrentUser(currentUser);
9798
}
9899

100+
componentDidUpdate({ refreshIdentity: prevRefreshIdentity }: Props) {
101+
const { refreshIdentity } = this.props;
102+
if (refreshIdentity !== prevRefreshIdentity) {
103+
this.fetchFeedItems(true);
104+
}
105+
}
106+
99107
/**
100108
* Fetches a Users info
101109
*

src/elements/content-sidebar/ContentSidebar.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type State = {
7070
file?: BoxItem,
7171
isLoading: boolean,
7272
metadataEditors?: Array<MetadataEditor>,
73+
refreshIdentity?: boolean,
7374
};
7475

7576
const MARK_NAME_JS_READY = `${ORIGIN_CONTENT_SIDEBAR}_${EVENT_JS_READY}`;
@@ -283,6 +284,10 @@ class ContentSidebar extends React.Component<Props, State> {
283284
}
284285
}
285286

287+
refresh(): void {
288+
this.setState(({ refreshIdentity }: State) => ({ refreshIdentity: !refreshIdentity }));
289+
}
290+
286291
/**
287292
* Renders the sidebar
288293
*
@@ -314,7 +319,7 @@ class ContentSidebar extends React.Component<Props, State> {
314319
onVersionHistoryClick,
315320
versionsSidebarProps,
316321
}: Props = this.props;
317-
const { file, isLoading, metadataEditors }: State = this.state;
322+
const { file, isLoading, metadataEditors, refreshIdentity }: State = this.state;
318323
const initialPath = defaultView.charAt(0) === '/' ? defaultView : `/${defaultView}`;
319324

320325
if (!file || !fileId || !SidebarUtils.shouldRenderSidebar(this.props, file, metadataEditors)) {
@@ -345,6 +350,7 @@ class ContentSidebar extends React.Component<Props, State> {
345350
metadataSidebarProps={metadataSidebarProps}
346351
onVersionChange={onVersionChange}
347352
onVersionHistoryClick={onVersionHistoryClick}
353+
refreshIdentity={refreshIdentity}
348354
versionsSidebarProps={versionsSidebarProps}
349355
/>
350356
</SidebarRouter>

src/elements/content-sidebar/DetailsSidebar.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type ExternalProps = {
4848
onClassificationClick?: (e: SyntheticEvent<HTMLButtonElement>) => void,
4949
onRetentionPolicyExtendClick?: Function,
5050
onVersionHistoryClick?: Function,
51+
refreshIdentity?: boolean,
5152
retentionPolicy?: Object,
5253
} & ErrorContextProps &
5354
WithLoggerProps;
@@ -98,10 +99,10 @@ class DetailsSidebar extends React.PureComponent<Props, State> {
9899
}
99100
}
100101

101-
componentDidUpdate(prevProps: Props) {
102-
const { hasAccessStats } = this.props;
102+
componentDidUpdate({ hasAccessStats: prevHasAccessStats, refreshIdentity: prevRefreshIdentity }: Props) {
103+
const { hasAccessStats, refreshIdentity } = this.props;
103104
// Component visibility props such as hasAccessStats can sometimes be flipped after an async call
104-
const hasAccessStatsChanged = prevProps.hasAccessStats !== hasAccessStats;
105+
const hasAccessStatsChanged = prevHasAccessStats !== hasAccessStats;
105106
if (hasAccessStatsChanged) {
106107
if (hasAccessStats) {
107108
this.fetchAccessStats();
@@ -113,6 +114,10 @@ class DetailsSidebar extends React.PureComponent<Props, State> {
113114
});
114115
}
115116
}
117+
118+
if (refreshIdentity !== prevRefreshIdentity) {
119+
this.fetchAccessStats();
120+
}
116121
}
117122

118123
/**

src/elements/content-sidebar/MetadataSidebar.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type ExternalProps = {
4242

4343
type PropsWithoutContext = {
4444
fileId: string,
45+
refreshIdentity?: boolean,
4546
} & ExternalProps;
4647

4748
type Props = {
@@ -81,6 +82,13 @@ class MetadataSidebar extends React.PureComponent<Props, State> {
8182
this.fetchFile();
8283
}
8384

85+
componentDidUpdate({ refreshIdentity: prevRefreshIdentity }: Props) {
86+
const { refreshIdentity } = this.props;
87+
if (refreshIdentity !== prevRefreshIdentity) {
88+
this.fetchMetadata();
89+
}
90+
}
91+
8492
/**
8593
* Common error callback
8694
*

src/elements/content-sidebar/Sidebar.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Props = {
4646
metadataSidebarProps: MetadataSidebarProps,
4747
onVersionChange?: Function,
4848
onVersionHistoryClick?: Function,
49+
refreshIdentity?: boolean,
4950
versionsSidebarProps: VersionsSidebarProps,
5051
};
5152

@@ -191,6 +192,7 @@ class Sidebar extends React.Component<Props, State> {
191192
metadataEditors,
192193
metadataSidebarProps,
193194
onVersionChange,
195+
refreshIdentity,
194196
versionsSidebarProps,
195197
}: Props = this.props;
196198

@@ -241,6 +243,7 @@ class Sidebar extends React.Component<Props, State> {
241243
metadataSidebarProps={metadataSidebarProps}
242244
onVersionChange={onVersionChange}
243245
onVersionHistoryClick={onVersionHistoryClick}
246+
refreshIdentity={refreshIdentity}
244247
versionsSidebarProps={versionsSidebarProps}
245248
/>
246249
</React.Fragment>

src/elements/content-sidebar/SidebarPanels.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Props = {
4141
metadataSidebarProps: MetadataSidebarProps,
4242
onVersionChange?: Function,
4343
onVersionHistoryClick?: Function,
44+
refreshIdentity?: boolean,
4445
versionsSidebarProps: VersionsSidebarProps,
4546
};
4647

@@ -84,6 +85,7 @@ const SidebarPanels = ({
8485
metadataSidebarProps,
8586
onVersionChange,
8687
onVersionHistoryClick,
88+
refreshIdentity,
8789
versionsSidebarProps,
8890
}: Props) =>
8991
isOpen && (
@@ -114,6 +116,7 @@ const SidebarPanels = ({
114116
onVersionHistoryClick={onVersionHistoryClick}
115117
startMarkName={MARK_NAME_JS_LOADING_ACTIVITY}
116118
{...activitySidebarProps}
119+
refreshIdentity={refreshIdentity}
117120
/>
118121
)}
119122
/>
@@ -129,6 +132,7 @@ const SidebarPanels = ({
129132
onVersionHistoryClick={onVersionHistoryClick}
130133
startMarkName={MARK_NAME_JS_LOADING_DETAILS}
131134
{...detailsSidebarProps}
135+
refreshIdentity={refreshIdentity}
132136
/>
133137
)}
134138
/>
@@ -142,6 +146,7 @@ const SidebarPanels = ({
142146
fileId={fileId}
143147
startMarkName={MARK_NAME_JS_LOADING_METADATA}
144148
{...metadataSidebarProps}
149+
refreshIdentity={refreshIdentity}
145150
/>
146151
)}
147152
/>
@@ -157,6 +162,7 @@ const SidebarPanels = ({
157162
parentName={match.params.sidebar}
158163
versionId={match.params.versionId}
159164
{...versionsSidebarProps}
165+
refreshIdentity={refreshIdentity}
160166
/>
161167
)}
162168
/>

src/elements/content-sidebar/SkillsSidebar.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PropsWithoutContext = {
3030
file: BoxItem,
3131
getPreview: Function,
3232
getViewer: Function,
33+
refreshIdentity?: boolean,
3334
};
3435

3536
type Props = {
@@ -64,6 +65,14 @@ class SkillsSidebar extends React.PureComponent<Props, State> {
6465
api.getMetadataAPI(false).getSkills(file, this.fetchSkillsSuccessCallback, noop);
6566
}
6667

68+
componentDidUpdate({ refreshIdentity: prevRefreshIdentity }: Props) {
69+
const { api, file, refreshIdentity }: Props = this.props;
70+
71+
if (refreshIdentity !== prevRefreshIdentity) {
72+
api.getMetadataAPI(false).getSkills(file, this.fetchSkillsSuccessCallback, noop);
73+
}
74+
}
75+
6776
/**
6877
* Handles skills fetch success
6978
*

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ describe('elements/content-sidebar/ActivitySidebar', () => {
9494
});
9595
});
9696

97+
describe('componentDidUpdate()', () => {
98+
let wrapper;
99+
let instance;
100+
currentUser = {
101+
id: '123',
102+
};
103+
beforeEach(() => {
104+
jest.spyOn(ActivitySidebarComponent.prototype, 'fetchFeedItems');
105+
wrapper = getWrapper({
106+
currentUser,
107+
refreshIdentity: false,
108+
});
109+
instance = wrapper.instance();
110+
});
111+
112+
afterEach(() => {
113+
jest.restoreAllMocks();
114+
});
115+
116+
test('should fetch the feed items if refreshIdentity changed', () => {
117+
wrapper.setProps({ refreshIdentity: true });
118+
119+
expect(instance.fetchFeedItems.mock.calls.length).toEqual(2);
120+
});
121+
122+
test('should not fetch the feed items if refreshIdentity did not change', () => {
123+
wrapper.setProps({ refreshIdentity: false });
124+
125+
expect(instance.fetchFeedItems.mock.calls.length).toEqual(1);
126+
});
127+
});
128+
97129
describe('render()', () => {
98130
test('should render the activity feed sidebar', () => {
99131
const wrapper = getWrapper();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,25 @@ describe('elements/content-sidebar/ContentSidebar', () => {
243243
expect(getMetadata).not.toBeCalled();
244244
});
245245
});
246+
247+
describe('refresh()', () => {
248+
let wrapper;
249+
let instance;
250+
251+
beforeEach(() => {
252+
wrapper = getWrapper();
253+
instance = wrapper.instance();
254+
});
255+
256+
test.each`
257+
testcase | initialValue | expectedResult
258+
${'null'} | ${null} | ${true}
259+
${'false'} | ${false} | ${true}
260+
${'true'} | ${true} | ${false}
261+
`('should change the refreshIdentity state: $testcase', ({ initialValue, expectedResult }) => {
262+
instance.setState({ refreshIdentity: initialValue });
263+
instance.refresh();
264+
expect(instance.state.refreshIdentity).toEqual(expectedResult);
265+
});
266+
});
246267
});

0 commit comments

Comments
 (0)