All files / app/assets/javascripts/design_management/components design_todo_button.vue

62.5% Statements 20/32
27.27% Branches 3/11
75% Functions 12/16
64.51% Lines 20/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159                    5x                                                   17x           2x               2x                     17x     17x     17x     17x         2x 2x                                     2x       2x   2x 2x   2x 2x                                                 2x       4x 2x     2x                                        
<script>
import { GlIcon, GlTooltipDirective } from '@gitlab/ui';
import todoMarkDoneMutation from '~/graphql_shared/mutations/todo_mark_done.mutation.graphql';
import TodoButton from '~/sidebar/components/todo_toggle/todo_button.vue';
import { todoLabel } from '~/sidebar/utils';
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,
    GlIcon,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  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);
    },
    buttonIcon() {
      return this.pendingTodo ? 'todo-done' : 'todo-add';
    },
    tooltipLabel() {
      return todoLabel(this.hasPendingTodo);
    },
  },
  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];
            Iif (createDesignTodoError) {
              this.$emit('error', Error(createDesignTodoError.message));
            }
          },
        })
        .catch((err) => {
          this.$emit('error', Error(CREATE_DESIGN_TODO_ERROR));
          throw err;
        })
        .finally(() => {
          this.todoLoading = false;
        });
    },
    deleteTodo() {
      Iif (!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
    v-gl-tooltip
    :title="tooltipLabel"
    issuable-type="design"
    :issuable-id="design.iid"
    :is-todo="hasPendingTodo"
    :loading="todoLoading"
    :is-icon-button="true"
    @click.stop.prevent="toggleTodo"
  >
    <gl-icon :class="{ 'gl-fill-blue-500': pendingTodo }" :name="buttonIcon" />
  </todo-button>
</template>