@@ -16,7 +16,7 @@ import {
1616 findContentBySearchQuery ,
1717 checkCreatePermission ,
1818} from './services/universal.discovery.service' ;
19- import { checkIsBookmarked , loadBookmarks } from './services/bookmark.service' ;
19+ import { checkIsBookmarked , loadBookmarks , addBookmark , removeBookmark } from './services/bookmark.service' ;
2020import { showErrorNotification } from '../common/services/notification.service' ;
2121import { areSameLocations } from '../common/helpers/compare.helper' ;
2222import deepClone from '../common/helpers/deep.clone.helper' ;
@@ -57,14 +57,15 @@ export default class UniversalDiscoveryModule extends Component {
5757 this . handleConfirm = this . handleConfirm . bind ( this ) ;
5858 this . setCreateModeState = this . setCreateModeState . bind ( this ) ;
5959 this . updateContentMetaWithCurrentVersion = this . updateContentMetaWithCurrentVersion . bind ( this ) ;
60- this . onBookmarkRemoved = this . onBookmarkRemoved . bind ( this ) ;
60+ this . toggleBookmark = this . toggleBookmark . bind ( this ) ;
6161 this . onBookmarkAdded = this . onBookmarkAdded . bind ( this ) ;
62- this . setBookmarked = this . setBookmarked . bind ( this ) ;
62+ this . onBookmarkRemoved = this . onBookmarkRemoved . bind ( this ) ;
63+ this . setBookmarkLoadingStatus = this . setBookmarkLoadingStatus . bind ( this ) ;
6364 this . requireBookmarksCount = this . requireBookmarksCount . bind ( this ) ;
6465 this . onBookmarksLoaded = this . onBookmarksLoaded . bind ( this ) ;
6566 this . updatePermissionsState = this . updatePermissionsState . bind ( this ) ;
6667
67- this . _refBookmarksPanelComponent = null ;
68+ this . loadingBookmarksLocationsIds = { } ;
6869
6970 if ( isForcedContentType ) {
7071 selectedContentType = this . findContentType ( props . cotfAllowedContentTypes [ 0 ] ) ;
@@ -254,29 +255,63 @@ export default class UniversalDiscoveryModule extends Component {
254255 document . body . dispatchEvent ( event ) ;
255256 }
256257
258+ /**
259+ * Sets loading status of bookmark
260+ *
261+ * @method setBookmarkLoadingStatus
262+ * @param {String } locationId
263+ * @param {Boolean } isLoading
264+ * @memberof UniversalDiscoveryModule
265+ */
266+ setBookmarkLoadingStatus ( locationId , isLoading ) {
267+ this . loadingBookmarksLocationsIds [ locationId ] = isLoading ;
268+ }
269+
270+ /**
271+ * Returns true if bookmark is loading, otherwise returns false
272+ *
273+ * @method isBookmarkLoading
274+ * @param {String } locationId
275+ * @memberof UniversalDiscoveryModule
276+ */
277+ isBookmarkLoading ( locationId ) {
278+ return ! ! this . loadingBookmarksLocationsIds [ locationId ] ;
279+ }
280+
257281 /**
258282 * Checks whether location is already bookmarked
259283 *
260- * @method checkIsBookmarked
284+ * @method fetchBookmarkStatus
261285 * @param {String } locationId
262286 * @memberof UniversalDiscoveryModule
263287 */
264- checkIsBookmarked ( locationId ) {
288+ fetchBookmarkStatus ( locationId ) {
289+ if ( this . isBookmarkLoading ( locationId ) ) {
290+ return ;
291+ }
292+
293+ this . setBookmarkLoadingStatus ( locationId , true ) ;
294+
265295 const { restInfo } = this . props ;
266296 const checked = new Promise ( ( resolve ) => checkIsBookmarked ( restInfo , locationId , resolve ) ) ;
267297
268- checked . then ( this . setBookmarked . bind ( this , locationId ) ) . catch ( showErrorNotification ) ;
298+ checked
299+ . then ( ( isBookmarked ) => {
300+ this . setIsBookmarked ( locationId , isBookmarked ) ;
301+ this . setBookmarkLoadingStatus ( locationId , false ) ;
302+ } )
303+ . catch ( showErrorNotification ) ;
269304 }
270305
271306 /**
272- * Sets bookmarked flag
307+ * Sets bookmark value in the state
273308 *
274- * @method setBookmarked
309+ * @method setIsBookmarked
275310 * @param {String } locationId
276311 * @param {Boolean } isBookmarked
277312 * @memberof ContentMetaPreviewComponent
278313 */
279- setBookmarked ( locationId , isBookmarked ) {
314+ setIsBookmarked ( locationId , isBookmarked ) {
280315 this . setState ( ( state ) => {
281316 const bookmarked = { ...state . bookmarked } ;
282317
@@ -287,23 +322,53 @@ export default class UniversalDiscoveryModule extends Component {
287322 }
288323
289324 /**
290- * Returns true or false if locations bookmark data was cached
291- * if not, checks it and then caches it
325+ * Removes or adds bookmark depending on if it exists or not
326+ *
327+ * @method toggleBookmark
328+ * @param {Object } location
329+ * @memberof UniversalDiscoveryModule
330+ */
331+ toggleBookmark ( location ) {
332+ const locationId = location . id ;
333+ const isBookmarked = this . isBookmarked ( locationId ) ;
334+
335+ if ( this . isBookmarkLoading ( locationId ) ) {
336+ return ;
337+ }
338+
339+ this . setBookmarkLoadingStatus ( locationId , true ) ;
340+
341+ const { restInfo } = this . props ;
342+ const toggleBookmark = isBookmarked ? removeBookmark : addBookmark ;
343+ const onBookmarkToggled = isBookmarked ? this . onBookmarkRemoved : this . onBookmarkAdded ;
344+ const bookmarkToggled = new Promise ( ( resolve ) => toggleBookmark ( restInfo , locationId , resolve ) ) ;
345+
346+ bookmarkToggled
347+ . then ( ( ) => {
348+ onBookmarkToggled ( location ) ;
349+ this . setBookmarkLoadingStatus ( locationId , false ) ;
350+ } )
351+ . catch ( showErrorNotification ) ;
352+ }
353+
354+ /**
355+ * Returns
292356 *
293357 * @method isBookmarked
294358 * @param {String } locationId
359+ * @returns {Boolean }
295360 * @memberof UniversalDiscoveryModule
296361 */
297362 isBookmarked ( locationId ) {
298363 const { bookmarked } = this . state ;
364+ const locationBookmarkChecked = locationId in bookmarked ;
299365
300- if ( locationId in bookmarked ) {
301- return bookmarked [ locationId ] ;
366+ if ( ! locationBookmarkChecked ) {
367+ this . fetchBookmarkStatus ( locationId ) ;
368+ return null ;
302369 }
303370
304- this . checkIsBookmarked ( locationId ) ;
305-
306- return null ;
371+ return bookmarked [ locationId ] ;
307372 }
308373
309374 /**
@@ -321,7 +386,7 @@ export default class UniversalDiscoveryModule extends Component {
321386 userBookmarksItems : state . userBookmarksItems . filter ( ( item ) => ! areSameLocations ( item . Location , itemToRemoveLocation ) ) ,
322387 } ) ,
323388 ( ) => {
324- this . setBookmarked ( itemToRemoveLocation . id , false ) ;
389+ this . setIsBookmarked ( itemToRemoveLocation . id , false ) ;
325390
326391 const { activeTab } = this . state ;
327392
@@ -349,7 +414,7 @@ export default class UniversalDiscoveryModule extends Component {
349414 userBookmarksItems : [ { Location : addedBookmarkLocation } , ...state . userBookmarksItems ] ,
350415 } ) ,
351416 ( ) => {
352- this . setBookmarked ( addedBookmarkLocation . id , true ) ;
417+ this . setIsBookmarked ( addedBookmarkLocation . id , true ) ;
353418 this . dispatchBookmarkChangeEvent ( addedBookmarkLocation . id , true ) ;
354419 }
355420 ) ;
@@ -371,7 +436,7 @@ export default class UniversalDiscoveryModule extends Component {
371436 bookmarksDuringLoadingCount : state . bookmarksDuringLoadingCount - items . length ,
372437 userBookmarksItems : [ ...items , ...state . userBookmarksItems ] ,
373438 } ) ,
374- ( ) => items . forEach ( ( bookmarkLocation ) => this . setBookmarked ( bookmarkLocation . Location . id , true ) )
439+ ( ) => items . forEach ( ( bookmarkLocation ) => this . setIsBookmarked ( bookmarkLocation . Location . id , true ) )
375440 ) ;
376441 }
377442
@@ -524,17 +589,16 @@ export default class UniversalDiscoveryModule extends Component {
524589
525590 const { contentTypesMap, maxHeight, activeTab, contentMeta, isPreviewMetaReady } = this . state ;
526591 const { loadContentInfo, restInfo, languages, labels } = this . props ;
527- const isSelectedContentBookmarked = this . isBookmarked ( contentMeta . id ) ;
592+ const isContentBookmarked = this . isBookmarked ( contentMeta . id ) ;
528593
529594 return (
530595 < div className = "m-ud__preview" >
531596 < ContentMetaPreviewComponent
532597 data = { contentMeta }
533- bookmarked = { isSelectedContentBookmarked }
598+ isBookmarked = { isContentBookmarked }
534599 canSelectContent = { this . canSelectContent }
535600 onSelectContent = { this . updateSelectedContent }
536- onBookmarkRemoved = { this . onBookmarkRemoved }
537- onBookmarkAdded = { this . onBookmarkAdded }
601+ toggleBookmark = { this . toggleBookmark }
538602 loadContentInfo = { loadContentInfo }
539603 restInfo = { restInfo }
540604 contentTypesMap = { contentTypesMap }
0 commit comments