Skip to content

Commit 9255f23

Browse files
author
Łukasz Serwatka
authored
Merge pull request #78 from tischsoic/EZP-29314-Lots-of-error-messages-after-clicking-fast-bookmark-icon-UDW
EZP-29314: Lots of error messages after clicking fast bookmark button
2 parents bee95b1 + 85a0463 commit 9255f23

File tree

10 files changed

+108
-55
lines changed

10 files changed

+108
-55
lines changed

Resources/public/js/MultiFileUpload.module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/public/js/MultiFileUpload.module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/public/js/SubItems.module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/public/js/SubItems.module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/public/js/UniversalDiscovery.module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/public/js/UniversalDiscovery.module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/universal-discovery/components/bookmarks/bookmarks.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ BookmarksComponent.propTypes = {
7070
maxHeight: PropTypes.number.isRequired,
7171
contentTypesMap: PropTypes.object.isRequired,
7272
bookmarksPerPage: PropTypes.number.isRequired,
73-
userBookmarksCount: PropTypes.number.isRequired,
74-
userBookmarksItems: PropTypes.array.isRequired,
73+
userBookmarksCount: PropTypes.number,
74+
userBookmarksItems: PropTypes.array,
7575
requireBookmarksCount: PropTypes.func.isRequired,
7676
labels: PropTypes.shape({
7777
bookmarks: PropTypes.shape({

src/modules/universal-discovery/components/content-meta-preview/content.meta.preview.component.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import React, { Component, Fragment } from 'react';
22
import PropTypes from 'prop-types';
33

44
import './css/content.meta.preview.component.css';
5-
import { addBookmark, removeBookmark } from '../../services/bookmark.service';
6-
import { showErrorNotification } from '../../../common/services/notification.service';
75
import { TAB_CREATE } from '../../universal.discovery.module';
86

97
export default class ContentMetaPreviewComponent extends Component {
@@ -126,7 +124,7 @@ export default class ContentMetaPreviewComponent extends Component {
126124
const version = data.CurrentVersion.Version;
127125
const versionLanguages = version.VersionInfo.VersionTranslationInfo.Language;
128126

129-
return versionLanguages.map((langauge) => this.props.languages.mappings[langauge.languageCode].name);
127+
return versionLanguages.map((language) => this.props.languages.mappings[language.languageCode].name);
130128
}
131129

132130
/**
@@ -172,19 +170,15 @@ export default class ContentMetaPreviewComponent extends Component {
172170
}
173171

174172
/**
175-
* Removes or adds bookmark depending on if it exists or not
173+
* Toggles bookmark
176174
*
177175
* @method toggleBookmark
178176
* @memberof ContentMetaPreviewComponent
179177
*/
180178
toggleBookmark() {
181-
const { onBookmarkRemoved, onBookmarkAdded, data, restInfo, bookmarked } = this.props;
182-
const toggleBookmark = bookmarked ? removeBookmark : addBookmark;
183-
const onBookmarkToggled = bookmarked ? onBookmarkRemoved : onBookmarkAdded;
184-
const locationId = data.id;
185-
const bookmarkToggled = new Promise((resolve) => toggleBookmark(restInfo, locationId, resolve));
179+
const { toggleBookmark, data } = this.props;
186180

187-
bookmarkToggled.then(() => onBookmarkToggled(data)).catch(showErrorNotification);
181+
toggleBookmark(data);
188182
}
189183

190184
/**
@@ -194,14 +188,10 @@ export default class ContentMetaPreviewComponent extends Component {
194188
* @memberof ContentMetaPreviewComponent
195189
*/
196190
renderBookmarkIcon() {
197-
const { bookmarked } = this.props;
191+
const { isBookmarked } = this.props;
198192

199-
if (bookmarked === null) {
200-
return null;
201-
}
202-
203-
const bookmarkIconId = bookmarked ? 'bookmark-active' : 'bookmark';
204-
const action = bookmarked ? 'remove' : 'add';
193+
const bookmarkIconId = isBookmarked ? 'bookmark-active' : 'bookmark';
194+
const action = isBookmarked ? 'remove' : 'add';
205195
const iconHref = `/bundles/ezplatformadminui/img/ez-icons.svg#${bookmarkIconId}`;
206196
const wrapperClassName = `ez-add-to-bookmarks__icon-wrapper ez-add-to-bookmarks__icon-wrapper--${action}`;
207197

@@ -256,10 +246,9 @@ export default class ContentMetaPreviewComponent extends Component {
256246

257247
ContentMetaPreviewComponent.propTypes = {
258248
data: PropTypes.object.isRequired,
259-
bookmarked: PropTypes.bool,
249+
isBookmarked: PropTypes.bool,
260250
onSelectContent: PropTypes.func.isRequired,
261-
onBookmarkRemoved: PropTypes.func.isRequired,
262-
onBookmarkAdded: PropTypes.func.isRequired,
251+
toggleBookmark: PropTypes.func.isRequired,
263252
canSelectContent: PropTypes.func.isRequired,
264253
contentTypesMap: PropTypes.object.isRequired,
265254
restInfo: PropTypes.shape({
@@ -282,5 +271,5 @@ ContentMetaPreviewComponent.propTypes = {
282271
};
283272

284273
ContentMetaPreviewComponent.defaultProps = {
285-
bookmarked: null,
274+
isBookmarked: null,
286275
};

src/modules/universal-discovery/components/tab-content/bookmarks.panel.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ BookmarksPanelComponent.propTypes = {
6868
contentTypesMap: PropTypes.object.isRequired,
6969
bookmarksPerPage: PropTypes.number.isRequired,
7070
labels: PropTypes.object.isRequired,
71-
userBookmarksItems: PropTypes.array.isRequired,
72-
userBookmarksCount: PropTypes.number.isRequired,
71+
userBookmarksItems: PropTypes.array,
72+
userBookmarksCount: PropTypes.number,
7373
requireBookmarksCount: PropTypes.func.isRequired,
7474
restInfo: PropTypes.shape({
7575
token: PropTypes.string.isRequired,

src/modules/universal-discovery/universal.discovery.module.js

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2020
import { showErrorNotification } from '../common/services/notification.service';
2121
import { areSameLocations } from '../common/helpers/compare.helper';
2222
import 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

Comments
 (0)