diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
commit | e4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 (patch) | |
tree | 2fcdfa7dcdb9db8f5208b2562f4b4e803d671243 /app/assets/javascripts/work_items/notes/award_utils.js | |
parent | ffda4e7bcac36987f936b4ba515995a6698698f0 (diff) |
Add latest changes from gitlab-org/gitlab@16-2-stable-eev16.2.0-rc42
Diffstat (limited to 'app/assets/javascripts/work_items/notes/award_utils.js')
-rw-r--r-- | app/assets/javascripts/work_items/notes/award_utils.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/app/assets/javascripts/work_items/notes/award_utils.js b/app/assets/javascripts/work_items/notes/award_utils.js new file mode 100644 index 00000000000..5351a22d593 --- /dev/null +++ b/app/assets/javascripts/work_items/notes/award_utils.js @@ -0,0 +1,67 @@ +import { __ } from '~/locale'; +import { TYPENAME_USER } from '~/graphql_shared/constants'; +import { convertToGraphQLId, getIdFromGraphQLId } from '~/graphql_shared/utils'; +import { + updateCacheAfterAddingAwardEmojiToNote, + updateCacheAfterRemovingAwardEmojiFromNote, +} from '~/work_items/graphql/cache_utils'; +import workItemNotesByIidQuery from '../graphql/notes/work_item_notes_by_iid.query.graphql'; +import addAwardEmojiMutation from '../graphql/notes/work_item_note_add_award_emoji.mutation.graphql'; +import removeAwardEmojiMutation from '../graphql/notes/work_item_note_remove_award_emoji.mutation.graphql'; + +function awardedByCurrentUser(note) { + return (note.awardEmoji?.nodes ?? []) + .filter((award) => { + return getIdFromGraphQLId(award.user.id) === window.gon.current_user_id; + }) + .map((award) => award.name); +} + +export function getMutation({ note, name }) { + if (awardedByCurrentUser(note).includes(name)) { + return { + mutation: removeAwardEmojiMutation, + mutationName: 'awardEmojiRemove', + errorMessage: __('Failed to remove emoji. Please try again'), + }; + } + return { + mutation: addAwardEmojiMutation, + mutationName: 'awardEmojiAdd', + errorMessage: __('Failed to add emoji. Please try again'), + }; +} + +export function optimisticAwardUpdate({ note, name, fullPath, workItemIid }) { + const { mutation } = getMutation({ note, name }); + + const currentUserId = window.gon.current_user_id; + + return (store) => { + store.updateQuery( + { + query: workItemNotesByIidQuery, + variables: { fullPath, iid: workItemIid }, + }, + (sourceData) => { + const updatedNote = { + id: note.id, + awardEmoji: { + __typename: 'AwardEmoji', + name, + user: { + __typename: 'UserCore', + id: convertToGraphQLId(TYPENAME_USER, currentUserId), + name: null, + }, + }, + }; + + if (mutation === removeAwardEmojiMutation) { + return updateCacheAfterRemovingAwardEmojiFromNote(sourceData, updatedNote); + } + return updateCacheAfterAddingAwardEmojiToNote(sourceData, updatedNote); + }, + ); + }; +} |