summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/alert_management/components/sidebar/sidebar_todo.vue
blob: 7d3135ad50d2ff582c6f022ac53e3c8f74c93cbd (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
<script>
import { s__ } from '~/locale';
import Todo from '~/sidebar/components/todo_toggle/todo.vue';
import axios from '~/lib/utils/axios_utils';
import createAlertTodo from '../../graphql/mutations/alert_todo_create.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,
      isTodo: false,
      todo: '',
    };
  },
  computed: {
    alertID() {
      return parseInt(this.alert.iid, 10);
    },
  },
  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);
    },
    toggleTodo() {
      if (this.todo) {
        return this.markAsDone();
      }

      this.isUpdating = true;
      return this.$apollo
        .mutate({
          mutation: createAlertTodo,
          variables: {
            iid: this.alert.iid,
            projectPath: this.projectPath,
          },
        })
        .then(({ data: { alertTodoCreate: { todo = {}, errors = [] } } = {} } = {}) => {
          if (errors[0]) {
            return this.$emit(
              'alert-error',
              `${this.$options.i18n.UPDATE_ALERT_TODO_ERROR} ${errors[0]}.`,
            );
          }

          this.todo = todo.id;
          return this.updateToDoCount(true);
        })
        .catch(() => {
          this.$emit(
            'alert-error',
            `${this.$options.i18n.UPDATE_ALERT_TODO_ERROR} ${s__(
              'AlertManagement|Please try again.',
            )}`,
          );
        })
        .finally(() => {
          this.isUpdating = false;
        });
    },
    markAsDone() {
      this.isUpdating = true;

      return axios
        .delete(`/dashboard/todos/${this.todo.split('/').pop()}`)
        .then(() => {
          this.todo = '';
          return this.updateToDoCount(false);
        })
        .catch(() => {
          this.$emit('alert-error', this.$options.i18n.UPDATE_ALERT_TODO_ERROR);
        })
        .finally(() => {
          this.isUpdating = false;
        });
    },
  },
};
</script>

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