Skip to content

Commit 1035afb

Browse files
rickstefanikmergify[bot]
authored andcommitted
feat(content-explorer): Fetch thumbnails for search and recents (#1475)
* feat: fetching thumbnails for search and recents * fix: better setState calls with updateCollection * feat: added tests * fix: solved merge conflict for BCE
1 parent 6c1a363 commit 1035afb

File tree

11 files changed

+274
-173
lines changed

11 files changed

+274
-173
lines changed

flow-typed/box-ui-elements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ type BoxItem = {
398398
selected?: boolean,
399399
shared_link?: SharedLink,
400400
size?: number,
401-
thumbnailUrl?: string,
401+
thumbnailUrl?: ?string,
402402
type?: ItemType,
403403
url?: string,
404404
version_limit?: ?number,

src/api/APIFactory.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,10 @@ class APIFactory {
325325
*
326326
* @return {FileAPI} FileAPI instance
327327
*/
328-
getFileAPI(): FileAPI {
329-
this.destroy();
328+
getFileAPI(shouldDestroy: boolean = true): FileAPI {
329+
if (shouldDestroy) {
330+
this.destroy();
331+
}
330332
this.fileAPI = new FileAPI(this.options);
331333
return this.fileAPI;
332334
}

src/api/Folder.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ import {
1616
ERROR_CODE_FETCH_FOLDER,
1717
ERROR_CODE_CREATE_FOLDER,
1818
FIELD_REPRESENTATIONS,
19+
X_REP_HINT_HEADER_DIMENSIONS_DEFAULT,
1920
} from '../constants';
2021

21-
// available dimensions for JPG: "32x32", "94x94", "160x160", "320x320", "1024x1024", "2048x2048"
22-
const DEFAULT_JPG_DIMENSIONS = '1024x1024';
23-
24-
// available dimensions for PNG: "1024x1024", "2048x2048"
25-
const DEFAULT_PNG_DIMENSIONS = '1024x1024';
26-
2722
class Folder extends Item {
2823
/**
2924
* @property {string}
@@ -237,9 +232,7 @@ class Folder extends Item {
237232
params,
238233
headers: requestFields.includes(FIELD_REPRESENTATIONS)
239234
? {
240-
// if unable to fetch jpg thumbnail, grab png rep of first page. Certain file types do
241-
// not have a thumbnail rep but do have a first page rep.
242-
'X-Rep-Hints': `[jpg?dimensions=${DEFAULT_JPG_DIMENSIONS},png?dimensions=${DEFAULT_PNG_DIMENSIONS}]`,
235+
'X-Rep-Hints': X_REP_HINT_HEADER_DIMENSIONS_DEFAULT,
243236
}
244237
: {},
245238
})

src/api/Recents.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import Base from './Base';
1111
import FileAPI from './File';
1212
import FolderAPI from './Folder';
1313
import WebLinkAPI from './WebLink';
14-
import { DEFAULT_ROOT, CACHE_PREFIX_RECENTS, FIELD_DATE, SORT_DESC, ERROR_CODE_FETCH_RECENTS } from '../constants';
14+
import {
15+
DEFAULT_ROOT,
16+
CACHE_PREFIX_RECENTS,
17+
ERROR_CODE_FETCH_RECENTS,
18+
FIELD_DATE,
19+
FIELD_REPRESENTATIONS,
20+
X_REP_HINT_HEADER_DIMENSIONS_DEFAULT,
21+
SORT_DESC,
22+
} from '../constants';
1523

1624
class Recents extends Base {
1725
/**
@@ -150,20 +158,29 @@ class Recents extends Base {
150158
/**
151159
* Does the network request
152160
*
161+
* @param {FetchOptions} options - options for request
153162
* @return {Promise}
154163
*/
155-
recentsRequest(): Promise<void> {
164+
recentsRequest(options: FetchOptions = {}): Promise<void> {
156165
if (this.isDestroyed()) {
157166
return Promise.reject();
158167
}
159168

169+
const { fields } = options;
170+
const requestFields = fields || FOLDER_FIELDS_TO_FETCH;
171+
160172
this.errorCode = ERROR_CODE_FETCH_RECENTS;
161173
return this.xhr
162174
.get({
163175
url: this.getUrl(),
164176
params: {
165-
fields: FOLDER_FIELDS_TO_FETCH.toString(),
177+
fields: requestFields.toString(),
166178
},
179+
headers: requestFields.includes(FIELD_REPRESENTATIONS)
180+
? {
181+
'X-Rep-Hints': X_REP_HINT_HEADER_DIMENSIONS_DEFAULT,
182+
}
183+
: {},
167184
})
168185
.then(this.recentsSuccessHandler)
169186
.catch(this.recentsErrorHandler);
@@ -203,7 +220,7 @@ class Recents extends Base {
203220
}
204221

205222
// Make the XHR request
206-
this.recentsRequest();
223+
this.recentsRequest(options);
207224
}
208225
}
209226

src/api/Search.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import Base from './Base';
1111
import FileAPI from './File';
1212
import FolderAPI from './Folder';
1313
import WebLinkAPI from './WebLink';
14-
import { CACHE_PREFIX_SEARCH, FIELD_RELEVANCE, SORT_DESC, ERROR_CODE_SEARCH } from '../constants';
14+
import {
15+
CACHE_PREFIX_SEARCH,
16+
FIELD_RELEVANCE,
17+
FIELD_REPRESENTATIONS,
18+
X_REP_HINT_HEADER_DIMENSIONS_DEFAULT,
19+
SORT_DESC,
20+
ERROR_CODE_SEARCH,
21+
} from '../constants';
1522

1623
class Search extends Base {
1724
/**
@@ -185,13 +192,17 @@ class Search extends Base {
185192
/**
186193
* Does the network request
187194
*
195+
* @param {FetchOptions} options - options for request
188196
* @return {void}
189197
*/
190-
searchRequest(): Promise<void> {
198+
searchRequest(options: FetchOptions = {}): Promise<void> {
191199
if (this.isDestroyed()) {
192200
return Promise.reject();
193201
}
194202

203+
const { fields } = options;
204+
const requestFields = fields || FOLDER_FIELDS_TO_FETCH;
205+
195206
this.errorCode = ERROR_CODE_SEARCH;
196207
return this.xhr
197208
.get({
@@ -201,8 +212,13 @@ class Search extends Base {
201212
query: this.query,
202213
ancestor_folder_ids: this.id,
203214
limit: this.limit,
204-
fields: FOLDER_FIELDS_TO_FETCH.toString(),
215+
fields: requestFields.toString(),
205216
},
217+
headers: requestFields.includes(FIELD_REPRESENTATIONS)
218+
? {
219+
'X-Rep-Hints': X_REP_HINT_HEADER_DIMENSIONS_DEFAULT,
220+
}
221+
: {},
206222
})
207223
.then(this.searchSuccessHandler)
208224
.catch(this.searchErrorHandler);
@@ -254,7 +270,7 @@ class Search extends Base {
254270
}
255271

256272
// Make the XHR request
257-
this.searchRequest();
273+
this.searchRequest(options);
258274
}
259275
}
260276

src/api/__tests__/Folder-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Cache from '../../utils/Cache';
22
import { FOLDER_FIELDS_TO_FETCH } from '../../utils/fields';
33
import Folder from '../Folder';
4-
import { FIELD_REPRESENTATIONS } from '../../constants';
4+
import { FIELD_REPRESENTATIONS, X_REP_HINT_HEADER_DIMENSIONS_DEFAULT } from '../../constants';
55

66
let folder;
77
let cache;
@@ -178,7 +178,7 @@ describe('api/Folder', () => {
178178
fields: fields.toString(),
179179
sort: 'by',
180180
},
181-
headers: { 'X-Rep-Hints': '[jpg?dimensions=1024x1024,png?dimensions=1024x1024]' },
181+
headers: { 'X-Rep-Hints': X_REP_HINT_HEADER_DIMENSIONS_DEFAULT },
182182
});
183183
});
184184
});

src/api/__tests__/Recents-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('api/Recents', () => {
9595
expect(recents.xhr.get).toHaveBeenCalledWith({
9696
url: 'https://api.box.com/2.0/recent_items',
9797
params: { fields: FOLDER_FIELDS_TO_FETCH.toString() },
98+
headers: {},
9899
});
99100
});
100101
});
@@ -114,6 +115,7 @@ describe('api/Recents', () => {
114115
expect(recents.xhr.get).toHaveBeenCalledWith({
115116
url: 'https://api.box.com/2.0/recent_items',
116117
params: { fields: FOLDER_FIELDS_TO_FETCH.toString() },
118+
headers: {},
117119
});
118120
});
119121
});

src/api/__tests__/Search-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ describe('api/Search', () => {
155155
limit: 20,
156156
fields: FOLDER_FIELDS_TO_FETCH.toString(),
157157
},
158+
headers: {},
158159
});
159160
});
160161
});
@@ -180,6 +181,7 @@ describe('api/Search', () => {
180181
limit: 20,
181182
fields: FOLDER_FIELDS_TO_FETCH.toString(),
182183
},
184+
headers: {},
183185
});
184186
});
185187
});

src/constants.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ export const SKILLS_STATUS_INVOKED = 'skills_invoked_status';
312312
/* ------------------ File Extensions ---------------------- */
313313
export const FILE_EXTENSION_BOX_NOTE = 'boxnote';
314314

315+
/* ------------------ X-Rep-Hints ---------------------- */
316+
// available dimensions for JPG: "32x32", "94x94", "160x160", "320x320", "1024x1024", "2048x2048"
317+
export const X_REP_HINT_JPG_DIMENSIONS_DEFAULT: '1024x1024' = '1024x1024';
318+
319+
// available dimensions for PNG: "1024x1024", "2048x2048"
320+
export const X_REP_HINT_PNG_DIMENSIONS_DEFAULT: '1024x1024' = '1024x1024';
321+
322+
// if unable to fetch jpg thumbnail, grab png rep of first page. Certain file types do
323+
// not have a thumbnail rep but do have a first page rep.
324+
export const X_REP_HINT_HEADER_DIMENSIONS_DEFAULT = `[jpg?dimensions=${X_REP_HINT_JPG_DIMENSIONS_DEFAULT}&paged=false,png?dimensions=${X_REP_HINT_PNG_DIMENSIONS_DEFAULT}]`;
325+
315326
/* ------------------ Sidebar View ---------------------- */
316327
export const SIDEBAR_VIEW_SKILLS: 'skills' = 'skills';
317328
export const SIDEBAR_VIEW_DETAILS: 'details' = 'details';

0 commit comments

Comments
 (0)