From d36422e558bf7cff9b9784b15afd03385b812fda Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 6 Dec 2023 01:56:07 +0200 Subject: [PATCH] Fix/optimize bulk favorite --- public/scripts/BulkEditOverlay.js | 43 +++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/public/scripts/BulkEditOverlay.js b/public/scripts/BulkEditOverlay.js index 25944a40e..cbf80ba7a 100644 --- a/public/scripts/BulkEditOverlay.js +++ b/public/scripts/BulkEditOverlay.js @@ -69,30 +69,31 @@ class CharacterContextMenu { */ static favorite = async (characterId) => { const character = CharacterContextMenu.#getCharacter(characterId); + const newFavState = !character.data.extensions.fav; - // Only set fav for V2 spec const data = { name: character.name, avatar: character.avatar, data: { extensions: { - fav: !character.data.extensions.fav, + fav: newFavState, }, }, + fav: newFavState, }; - return fetch('/api/characters/merge-attributes', { + const mergeResponse = await fetch('/api/characters/merge-attributes', { method: 'POST', headers: getRequestHeaders(), body: JSON.stringify(data), - }).then((response) => { - if (response.ok) { - const element = document.getElementById(`CharID${characterId}`); - element.classList.toggle('is_fav'); - } else { - response.json().then(json => toastr.error('Character not saved. Error: ' + json.message + '. Field: ' + json.error)); - } }); + + if (!mergeResponse.ok) { + mergeResponse.json().then(json => toastr.error(`Character not saved. Error: ${json.message}. Field: ${json.error}`)); + } + + const element = document.getElementById(`CharID${characterId}`); + element.classList.toggle('is_fav'); }; /** @@ -484,7 +485,7 @@ class BulkEditOverlay { this.container.removeEventListener('mouseup', cancelHold); this.container.removeEventListener('touchend', cancelHold); }, - BulkEditOverlay.longPressDelay); + BulkEditOverlay.longPressDelay); }; handleLongPressEnd = (event) => { @@ -529,7 +530,7 @@ class BulkEditOverlay { #getEnabledElements = () => [...this.container.getElementsByClassName(BulkEditOverlay.characterClass)]; - #getDisabledElements = () =>[...this.container.getElementsByClassName(BulkEditOverlay.groupClass), ...this.container.getElementsByClassName(BulkEditOverlay.bogusFolderClass)]; + #getDisabledElements = () => [...this.container.getElementsByClassName(BulkEditOverlay.groupClass), ...this.container.getElementsByClassName(BulkEditOverlay.bogusFolderClass)]; toggleCharacterSelected = event => { event.stopPropagation(); @@ -572,12 +573,20 @@ class BulkEditOverlay { /** * Concurrently handle character favorite requests. * - * @returns {Promise} + * @returns {Promise} */ - handleContextMenuFavorite = () => Promise.all(this.selectedCharacters.map(async characterId => CharacterContextMenu.favorite(characterId))) - .then(() => getCharacters()) - .then(() => favsToHotswap()) - .then(() => this.browseState()); + handleContextMenuFavorite = async () => { + const promises = []; + + for (const characterId of this.selectedCharacters) { + promises.push(CharacterContextMenu.favorite(characterId)); + } + + await Promise.allSettled(promises); + await getCharacters(); + await favsToHotswap(); + this.browseState(); + }; /** * Concurrently handle character duplicate requests.