Welcome to mirror list, hosted at ThFree Co, Russian Federation.

work_item_award_emoji.vue « components « work_items « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91f87be12336c468c3fd2d3c447698d476c43e27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<script>
import * as Sentry from '@sentry/browser';
import { getIdFromGraphQLId, convertToGraphQLId } from '~/graphql_shared/utils';
import AwardsList from '~/vue_shared/components/awards_list.vue';
import { isLoggedIn } from '~/lib/utils/common_utils';
import { TYPENAME_USER } from '~/graphql_shared/constants';
import updateWorkItemMutation from '../graphql/update_work_item.mutation.graphql';
import {
  EMOJI_ACTION_REMOVE,
  EMOJI_ACTION_ADD,
  WIDGET_TYPE_AWARD_EMOJI,
  EMOJI_THUMBSDOWN,
  EMOJI_THUMBSUP,
} from '../constants';

export default {
  defaultAwards: [EMOJI_THUMBSUP, EMOJI_THUMBSDOWN],
  isLoggedIn: isLoggedIn(),
  components: {
    AwardsList,
  },
  props: {
    workItem: {
      type: Object,
      required: true,
    },
    awardEmoji: {
      type: Object,
      required: true,
    },
  },
  computed: {
    currentUserId() {
      return window.gon.current_user_id;
    },
    /**
     * Parse and convert award emoji list to a format that AwardsList can understand
     */
    awards() {
      return this.awardEmoji.nodes.map((emoji, index) => ({
        id: index + 1,
        name: emoji.name,
        user: {
          id: getIdFromGraphQLId(emoji.user.id),
        },
      }));
    },
  },
  methods: {
    handleAward(name) {
      // Decide action based on emoji is already present
      const action =
        this.awards.findIndex((emoji) => emoji.name === name) > -1
          ? EMOJI_ACTION_REMOVE
          : EMOJI_ACTION_ADD;
      const inputVariables = {
        id: this.workItem.id,
        awardEmojiWidget: {
          action,
          name,
        },
      };

      this.$apollo
        .mutate({
          mutation: updateWorkItemMutation,
          variables: {
            input: inputVariables,
          },
          optimisticResponse: this.getOptimisticResponse({ name, action }),
        })
        .then(
          ({
            data: {
              workItemUpdate: { errors },
            },
          }) => {
            if (errors?.length) {
              throw new Error(errors[0]);
            }
          },
        )
        .catch((error) => {
          this.$emit('error', error.message);
          Sentry.captureException(error);
        });
    },
    /**
     * Prepare workItemUpdate for optimistic response
     */
    getOptimisticResponse({ name, action }) {
      let awardEmojiNodes = [
        ...this.awardEmoji.nodes,
        {
          name,
          __typename: 'AwardEmoji',
          user: {
            id: convertToGraphQLId(TYPENAME_USER, this.currentUserId),
            __typename: 'UserCore',
          },
        },
      ];
      // Exclude the award emoji node in case of remove action
      if (action === EMOJI_ACTION_REMOVE) {
        awardEmojiNodes = [...this.awardEmoji.nodes.filter((emoji) => emoji.name !== name)];
      }
      return {
        workItemUpdate: {
          errors: [],
          workItem: {
            ...this.workItem,
            widgets: [
              {
                type: WIDGET_TYPE_AWARD_EMOJI,
                awardEmoji: {
                  nodes: awardEmojiNodes,
                  __typename: 'AwardEmojiConnection',
                },
                __typename: 'WorkItemWidgetAwardEmoji',
              },
            ],
            __typename: 'WorkItem',
          },
          __typename: 'WorkItemUpdatePayload',
        },
      };
    },
  },
};
</script>

<template>
  <div class="gl-mt-3">
    <awards-list
      data-testid="work-item-award-list"
      :awards="awards"
      :can-award-emoji="$options.isLoggedIn"
      :current-user-id="currentUserId"
      :default-awards="$options.defaultAwards"
      selected-class="selected"
      @award="handleAward"
    />
  </div>
</template>