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

design_todo_button.vue « components « design_management « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1a23d61093811e5e204d0e4362b270993d3486b (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
<script>
import todoMarkDoneMutation from '~/graphql_shared/mutations/todo_mark_done.mutation.graphql';
import TodoButton from '~/sidebar/components/todo_toggle/todo_button.vue';
import createDesignTodoMutation from '../graphql/mutations/create_design_todo.mutation.graphql';
import getDesignQuery from '../graphql/queries/get_design.query.graphql';
import allVersionsMixin from '../mixins/all_versions';
import { updateStoreAfterDeleteDesignTodo } from '../utils/cache_update';
import { findIssueId, findDesignId } from '../utils/design_management_utils';
import { CREATE_DESIGN_TODO_ERROR, DELETE_DESIGN_TODO_ERROR } from '../utils/error_messages';

export default {
  components: {
    TodoButton,
  },
  mixins: [allVersionsMixin],
  inject: {
    projectPath: {
      default: '',
    },
    issueIid: {
      default: '',
    },
  },
  props: {
    design: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      todoLoading: false,
    };
  },
  computed: {
    designVariables() {
      return {
        fullPath: this.projectPath,
        iid: this.issueIid,
        filenames: [this.$route.params.id],
        atVersion: this.designsVersion,
      };
    },
    designTodoVariables() {
      return {
        projectPath: this.projectPath,
        issueId: findIssueId(this.design.issue.id),
        designId: findDesignId(this.design.id),
        issueIid: this.issueIid,
        filenames: [this.$route.params.id],
        atVersion: this.designsVersion,
      };
    },
    pendingTodo() {
      // TODO data structure pending BE MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40555#note_405732940
      return this.design.currentUserTodos?.nodes[0];
    },
    hasPendingTodo() {
      return Boolean(this.pendingTodo);
    },
  },
  methods: {
    createTodo() {
      this.todoLoading = true;
      return this.$apollo
        .mutate({
          mutation: createDesignTodoMutation,
          variables: this.designTodoVariables,
          update: (store, { data: { createDesignTodo } }) => {
            // because this is a @client mutation,
            // we control what is in errors, and therefore
            // we are certain that there is at most 1 item in the array
            const createDesignTodoError = (createDesignTodo.errors || [])[0];
            if (createDesignTodoError) {
              this.$emit('error', Error(createDesignTodoError.message));
            }
          },
        })
        .catch((err) => {
          this.$emit('error', Error(CREATE_DESIGN_TODO_ERROR));
          throw err;
        })
        .finally(() => {
          this.todoLoading = false;
        });
    },
    deleteTodo() {
      if (!this.hasPendingTodo) return Promise.reject();

      const { id } = this.pendingTodo;
      const { designVariables } = this;

      this.todoLoading = true;
      return this.$apollo
        .mutate({
          mutation: todoMarkDoneMutation,
          variables: {
            id,
          },
          update(store, { data: { todoMarkDone } }) {
            const todoMarkDoneFirstError = (todoMarkDone.errors || [])[0];
            if (todoMarkDoneFirstError) {
              this.$emit('error', Error(todoMarkDoneFirstError));
            } else {
              updateStoreAfterDeleteDesignTodo(
                store,
                todoMarkDone,
                getDesignQuery,
                designVariables,
              );
            }
          },
        })
        .catch((err) => {
          this.$emit('error', Error(DELETE_DESIGN_TODO_ERROR));
          throw err;
        })
        .finally(() => {
          this.todoLoading = false;
        });
    },
    toggleTodo() {
      if (this.hasPendingTodo) {
        return this.deleteTodo();
      }

      return this.createTodo();
    },
  },
};
</script>

<template>
  <todo-button
    issuable-type="design"
    :issuable-id="design.iid"
    :is-todo="hasPendingTodo"
    :loading="todoLoading"
    @click.stop.prevent="toggleTodo"
  />
</template>