summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/alert_management/components/sidebar/sidebar_todo.vue
blob: 5bd69a1f0ec9947e6b6d15ea0e3ac848bc4c52f0 (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
145
<script>
import { s__ } from '~/locale';
import Todo from '~/sidebar/components/todo_toggle/todo.vue';
import createAlertTodo from '../../graphql/mutations/alert_todo_create.mutation.graphql';
import todoMarkDone from '../../graphql/mutations/alert_todo_mark_done.mutation.graphql';
import alertQuery from '../../graphql/queries/details.query.graphql';

export default {
  i18n: {
    UPDATE_ALERT_TODO_ERROR: s__(
      'AlertManagement|There was an error while updating the To-Do of the alert.',
    ),
  },
  components: {
    Todo,
  },
  props: {
    alert: {
      type: Object,
      required: true,
    },
    projectPath: {
      type: String,
      required: true,
    },
    sidebarCollapsed: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      isUpdating: false,
    };
  },
  computed: {
    alertID() {
      return parseInt(this.alert.iid, 10);
    },
    firstToDoId() {
      return this.alert?.todos?.nodes[0]?.id;
    },
    hasPendingTodos() {
      return this.alert?.todos?.nodes.length > 0;
    },
    getAlertQueryVariables() {
      return {
        fullPath: this.projectPath,
        alertId: this.alert.iid,
      };
    },
  },
  methods: {
    updateToDoCount(add) {
      const oldCount = parseInt(document.querySelector('.todos-count').innerText, 10);
      const count = add ? oldCount + 1 : oldCount - 1;
      const headerTodoEvent = new CustomEvent('todo:toggle', {
        detail: {
          count,
        },
      });

      return document.dispatchEvent(headerTodoEvent);
    },
    addToDo() {
      this.isUpdating = true;
      return this.$apollo
        .mutate({
          mutation: createAlertTodo,
          variables: {
            iid: this.alert.iid,
            projectPath: this.projectPath,
          },
        })
        .then(({ data: { errors = [] } }) => {
          if (errors[0]) {
            return this.throwError(errors[0]);
          }
          return this.updateToDoCount(true);
        })
        .catch(() => {
          this.throwError();
        })
        .finally(() => {
          this.isUpdating = false;
        });
    },
    markAsDone() {
      this.isUpdating = true;
      return this.$apollo
        .mutate({
          mutation: todoMarkDone,
          variables: {
            id: this.firstToDoId,
          },
          update: this.updateCache,
        })
        .then(({ data: { errors = [] } }) => {
          if (errors[0]) {
            return this.throwError(errors[0]);
          }
          return this.updateToDoCount(false);
        })
        .catch(() => {
          this.throwError();
        })
        .finally(() => {
          this.isUpdating = false;
        });
    },
    updateCache(store) {
      const data = store.readQuery({
        query: alertQuery,
        variables: this.getAlertQueryVariables,
      });

      data.project.alertManagementAlerts.nodes[0].todos.nodes.shift();

      store.writeQuery({
        query: alertQuery,
        variables: this.getAlertQueryVariables,
        data,
      });
    },
    throwError(err = '') {
      const error = err || s__('AlertManagement|Please try again.');
      this.$emit('alert-error', `${this.$options.i18n.UPDATE_ALERT_TODO_ERROR} ${error}`);
    },
  },
};
</script>

<template>
  <div :class="{ 'block todo': sidebarCollapsed, 'gl-ml-auto': !sidebarCollapsed }">
    <todo
      data-testid="alert-todo-button"
      :collapsed="sidebarCollapsed"
      :issuable-id="alertID"
      :is-todo="hasPendingTodos"
      :is-action-active="isUpdating"
      issuable-type="alert"
      @toggleTodo="hasPendingTodos ? markAsDone() : addToDo()"
    />
  </div>
</template>