Fix/optimize bulk favorite

This commit is contained in:
Cohee 2023-12-06 01:56:07 +02:00
parent 60909cef1f
commit d36422e558
1 changed files with 26 additions and 17 deletions

View File

@ -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<number>}
* @returns {Promise<void>}
*/
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.