Refactor context menu favorite function for fetch

This commit is contained in:
artisticMink 2023-10-25 19:08:22 +02:00
parent a3685504ba
commit f6178b4c6d
1 changed files with 23 additions and 27 deletions

View File

@ -45,11 +45,18 @@ class CharacterContextMenu {
}); });
} }
static favorite = (characterId) => { /**
* Favorite a character
* and toggle its ui element.
*
* @param characterId
* @returns {Promise<void>}
*/
static favorite = async (characterId) => {
const character = CharacterContextMenu.getCharacter(characterId); const character = CharacterContextMenu.getCharacter(characterId);
const data = { const data = {
name: character.name, name: character.name,
avatar: character.avatar, avatar: character.avatar,
data: { data: {
extensions: { extensions: {
fav: !character.data.extensions.fav fav: !character.data.extensions.fav
@ -57,26 +64,13 @@ class CharacterContextMenu {
} }
}; };
jQuery.ajax({ return fetch('/v2/editcharacterattribute', {
type: "POST", method: "POST",
url: '/v2/editcharacterattribute', headers: getRequestHeaders(),
data: JSON.stringify(data), body: JSON.stringify(data),
contentType: "application/json; charset=utf-8", }).then((response) => {
cache: false, if (response.ok) toggleFavoriteHighlight(characterId)
processData: false, else toastr.error('Character not saved. Error: ' + response.json()?.message)
success: () =>
getOneCharacter(character.avatar)
.then(() => {
toggleFavoriteHighlight(characterId);
favsToHotswap()
.then(() => eventSource.emit(event_types.CHARACTER_EDITED, {
detail: {
id: characterId,
character: character
}
}))
}),
error: response => toastr.error('Character not saved. Error: ' + response.responseJSON?.message),
}); });
} }
@ -280,12 +274,14 @@ class CharacterGroupOverlay {
} }
} }
handleContextMenuFavorite = () => this.selectedCharacters.forEach(characterId => CharacterContextMenu.favorite(characterId)); handleContextMenuFavorite = () => Promise.all(this.selectedCharacters.map(async characterId => CharacterContextMenu.favorite(characterId)))
.then(() => getCharacters())
.then(() => favsToHotswap())
.then(() => this.browseState())
/** handleContextMenuDuplicate = () => Promise.all(this.selectedCharacters.map(async characterId => CharacterContextMenu.duplicate(characterId)))
* Aggregate duplicate promises for selected characters .then(() => getCharacters())
*/ .then(() => this.browseState())
handleContextMenuDuplicate = () => Promise.all(this.selectedCharacters.map(async characterId => CharacterContextMenu.duplicate(characterId))).then(() => getCharacters())
addStateChangeCallback = callback => this.stateChangeCallbacks.push(callback); addStateChangeCallback = callback => this.stateChangeCallbacks.push(callback);