66
77import noop from 'lodash.noop' ;
88import Base from './Base' ;
9- import FolderAPI from './Folder' ;
10- import { ACCESS_NONE , CACHE_PREFIX_SEARCH } from '../constants' ;
119import getBadItemError from '../util/error' ;
12- import type { BoxItem , FlattenedBoxItem , FlattenedBoxItemCollection } from '../flowTypes' ;
10+ import { ACCESS_NONE , CACHE_PREFIX_SEARCH , CACHE_PREFIX_FOLDER , TYPE_FOLDER } from '../constants' ;
11+ import type { BoxItem , FlattenedBoxItem , FlattenedBoxItemCollection , BoxItemPermission } from '../flowTypes' ;
1312
1413class Item extends Base {
1514 /**
@@ -32,6 +31,17 @@ class Item extends Base {
3231 */
3332 errorCallback : Function ;
3433
34+ /**
35+ * Creates a key for the item's parent
36+ * This is always a folder
37+ *
38+ * @param {string } id folder id
39+ * @return {string } key
40+ */
41+ getParentCacheKey ( id : string ) : string {
42+ return `${ CACHE_PREFIX_FOLDER } ${ id } ` ;
43+ }
44+
3545 /**
3646 * Handles error responses
3747 *
@@ -111,13 +121,11 @@ class Item extends Base {
111121 return ;
112122 }
113123
114- const parentAPI = new FolderAPI ( this . options ) ;
115- const parentKey : string = parentAPI . getCacheKey ( this . parentId ) ;
116-
117124 // When fetching the parent folder from the cache
118125 // we have no guarantees that it will be there since
119126 // search results happen across folders and we only
120127 // add those folders to cache that have been navigated to.
128+ const parentKey : string = this . getParentCacheKey ( this . parentId ) ;
121129 const folder : ?FlattenedBoxItem = this . getCache ( ) . get ( parentKey ) ;
122130 if ( ! folder ) {
123131 this . postDeleteCleanup ( ) ;
@@ -152,25 +160,32 @@ class Item extends Base {
152160 /**
153161 * API to delete an Item
154162 *
155- * @param {String } id - item id
163+ * @param {Object } item - item to delete
156164 * @param {Function } successCallback - success callback
157165 * @param {Function } errorCallback - error callback
158166 * @param {Boolean } recursive - true for folders
159167 * @return {void }
160168 */
161- delete (
162- id : string ,
163- parentId : string ,
164- successCallback : Function ,
165- errorCallback : Function = noop ,
166- recursive : boolean = false
167- ) : void {
169+ delete ( item : BoxItem , successCallback : Function , errorCallback : Function = noop ) : void {
170+ const { id, permissions, parent, type } : BoxItem = item ;
171+ if ( ! id || ! permissions || ! parent || ! type ) {
172+ errorCallback ( ) ;
173+ return ;
174+ }
175+
176+ const { id: parentId } = parent ;
177+ const { can_delete } : BoxItemPermission = permissions ;
178+ if ( ! can_delete || ! parentId ) {
179+ errorCallback ( ) ;
180+ return ;
181+ }
182+
168183 this . id = id ;
169184 this . parentId = parentId ;
170185 this . successCallback = successCallback ;
171186 this . errorCallback = errorCallback ;
172187
173- const url = `${ this . getUrl ( id ) } ${ recursive ? '?recursive=true' : '' } ` ;
188+ const url = `${ this . getUrl ( id ) } ${ type === TYPE_FOLDER ? '?recursive=true' : '' } ` ;
174189 this . xhr . delete ( url ) . then ( this . deleteSuccessHandler ) . catch ( this . errorHandler ) ;
175190 }
176191
0 commit comments