summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/work_items/components/work_item_state.vue
blob: 87f4a8822b151767d9c2ff4a56096f06ebb97d0c (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
<script>
import * as Sentry from '@sentry/browser';
import Tracking from '~/tracking';
import {
  i18n,
  STATE_OPEN,
  STATE_CLOSED,
  STATE_EVENT_CLOSE,
  STATE_EVENT_REOPEN,
  TRACKING_CATEGORY_SHOW,
} from '../constants';
import { getUpdateWorkItemMutation } from './update_work_item';
import ItemState from './item_state.vue';

export default {
  components: {
    ItemState,
  },
  mixins: [Tracking.mixin()],
  props: {
    workItem: {
      type: Object,
      required: true,
    },
    workItemParentId: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      updateInProgress: false,
    };
  },
  computed: {
    workItemType() {
      return this.workItem.workItemType?.name;
    },
    tracking() {
      return {
        category: TRACKING_CATEGORY_SHOW,
        label: 'item_state',
        property: `type_${this.workItemType}`,
      };
    },
  },
  methods: {
    updateWorkItemState(newState) {
      const stateEventMap = {
        [STATE_OPEN]: STATE_EVENT_REOPEN,
        [STATE_CLOSED]: STATE_EVENT_CLOSE,
      };

      const stateEvent = stateEventMap[newState];

      this.updateWorkItem(stateEvent);
    },

    async updateWorkItem(updatedState) {
      if (!updatedState) {
        return;
      }

      const input = {
        id: this.workItem.id,
        stateEvent: updatedState,
      };

      this.updateInProgress = true;

      try {
        this.track('updated_state');

        const { mutation, variables } = getUpdateWorkItemMutation({
          workItemParentId: this.workItemParentId,
          input,
        });

        const { data } = await this.$apollo.mutate({
          mutation,
          variables,
        });

        const errors = data.workItemUpdate?.errors;

        if (errors?.length) {
          throw new Error(errors[0]);
        }
      } catch (error) {
        this.$emit('error', i18n.updateError);
        Sentry.captureException(error);
      }

      this.updateInProgress = false;
    },
  },
};
</script>

<template>
  <item-state
    v-if="workItem.state"
    :state="workItem.state"
    :loading="updateInProgress"
    @changed="updateWorkItemState"
  />
</template>