All files / ee/app/assets/javascripts/boards/components board_settings_wip_limit.vue

83.33% Statements 20/24
70% Branches 7/10
90.9% Functions 10/11
83.33% Lines 20/24

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          2x                                                         23x               12x     19x     19x         3x 3x                   6x     3x 3x 3x     12x 4x   4x   4x   8x       1x     5x 5x                 3x                                                                                                              
<script>
import { GlButton, GlFormInput } from '@gitlab/ui';
import { __, n__ } from '~/locale';
import autofocusonshow from '~/vue_shared/directives/autofocusonshow';
import { setError } from '~/boards/graphql/cache_updates';
import listUpdateLimitMetricsMutation from '../graphql/list_update_limit_metrics.mutation.graphql';
 
export default {
  i18n: {
    wipLimitText: __('Work in progress limit'),
    editButtonText: __('Edit'),
    noneText: __('None'),
    inputPlaceholderText: __('Enter number of issues'),
    removeLimitText: __('Remove limit'),
    updateListError: __('Something went wrong while updating your list settings'),
  },
  components: {
    GlButton,
    GlFormInput,
  },
  directives: {
    autofocusonshow,
  },
  props: {
    activeListId: {
      type: String,
      required: true,
    },
    maxIssueCount: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      currentWipLimit: null,
      edit: false,
      updating: false,
    };
  },
  computed: {
    wipLimitTypeText() {
      return n__('%d issue', '%d issues', this.maxIssueCount);
    },
    wipLimitIsSet() {
      return this.maxIssueCount !== 0;
    },
    activeListWipLimit() {
      return this.wipLimitIsSet ? this.wipLimitTypeText : this.$options.i18n.noneText;
    },
  },
  methods: {
    showInput() {
      this.edit = true;
      this.currentWipLimit = this.maxIssueCount > 0 ? this.maxIssueCount : null;
    },
    handleWipLimitChange(wipLimit) {
      if (wipLimit === '') {
        this.currentWipLimit = null;
      } else {
        this.currentWipLimit = Number(wipLimit);
      }
    },
    onEnter() {
      this.offFocus();
    },
    resetStateAfterUpdate() {
      this.edit = false;
      this.updating = false;
      this.currentWipLimit = null;
    },
    offFocus() {
      if (this.currentWipLimit !== this.maxIssueCount && this.currentWipLimit !== null) {
        this.updating = true;
        // need to reassign bc were clearing the ref in resetStateAfterUpdate.
        const wipLimit = this.currentWipLimit;
 
        this.updateWipLimit(this.activeListId, wipLimit);
      } else {
        this.edit = false;
      }
    },
    clearWipLimit() {
      this.updateWipLimit(this.activeListId, 0);
    },
    async updateWipLimit(listId, maxIssueCount) {
      try {
        await this.$apollo.mutate({
          mutation: listUpdateLimitMetricsMutation,
          variables: {
            input: {
              listId,
              maxIssueCount,
            },
          },
        });
        this.resetStateAfterUpdate();
      } catch (error) {
        setError({
          error,
          message: this.$options.i18n.updateListError,
        });
      }
    },
  },
};
</script>
 
<template>
  <div class="gl-display-flex gl-justify-content-space-between gl-flex-direction-column">
    <div class="gl-display-flex gl-justify-content-space-between gl-align-items-center gl-mb-2">
      <label class="m-0">{{ $options.i18n.wipLimitText }}</label>
      <gl-button
        class="gl-h-full gl-border-0 text-dark"
        category="tertiary"
        size="small"
        data-testid="edit-button"
        @click="showInput"
        >{{ $options.i18n.editButtonText }}</gl-button
      >
    </div>
    <gl-form-input
      v-if="edit"
      v-autofocusonshow
      :value="currentWipLimit"
      :disabled="updating"
      :placeholder="$options.i18n.inputPlaceholderText"
      trim
      number
      type="number"
      min="0"
      @input="handleWipLimitChange"
      @keydown.enter.native="onEnter"
      @blur="offFocus"
    />
    <div v-else class="gl-display-flex gl-align-items-center">
      <p class="bold gl-m-0 text-secondary" data-testid="wip-limit">{{ activeListWipLimit }}</p>
      <template v-if="wipLimitIsSet">
        <span class="m-1">-</span>
        <gl-button
          class="gl-h-full gl-border-0 text-secondary"
          category="tertiary"
          size="small"
          data-testid="remove-limit"
          @click="clearWipLimit"
          >{{ $options.i18n.removeLimitText }}</gl-button
        >
      </template>
    </div>
  </div>
</template>