All files / app/assets/javascripts/boards/components/sidebar board_sidebar_title.vue

90.69% Statements 39/43
84.21% Branches 16/19
100% Functions 11/11
90.69% Lines 39/43

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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197              6x                                             13x               13x     4x 4x     26x           13x 13x     13x 13x             26x 13x     13x             13x 13x   13x 1x 1x 1x 1x   12x       1x 1x 1x 1x     5x 5x     5x                       6x   6x 1x     5x 5x 5x       5x     5x       1x 1x 1x                                                                                                                                    
<script>
import { GlAlert, GlButton, GlForm, GlFormGroup, GlFormInput, GlLink } from '@gitlab/ui';
import BoardEditableItem from '~/boards/components/sidebar/board_editable_item.vue';
import { joinPaths } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
import autofocusonshow from '~/vue_shared/directives/autofocusonshow';
import { titleQueries } from 'ee_else_ce/boards/constants';
import { setError } from '../../graphql/cache_updates';
 
export default {
  components: {
    GlForm,
    GlAlert,
    GlButton,
    GlFormGroup,
    GlFormInput,
    GlLink,
    BoardEditableItem,
  },
  directives: {
    autofocusonshow,
  },
  inject: ['fullPath', 'issuableType', 'isEpicBoard'],
  props: {
    activeItem: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      title: '',
      loading: false,
      showChangesAlert: false,
    };
  },
  computed: {
    pendingChangesStorageKey() {
      return this.getPendingChangesKey(this.activeItem);
    },
    projectPath() {
      const referencePath = this.activeItem.referencePath || '';
      return referencePath.slice(0, referencePath.indexOf('#'));
    },
    validationState() {
      return Boolean(this.title);
    },
  },
  watch: {
    activeItem: {
      handler(updatedItem, formerItem) {
        if (formerItem?.title !== this.title) {
          localStorage.setItem(this.getPendingChangesKey(formerItem), this.title);
        }
 
        this.title = updatedItem.title;
        this.setPendingState();
      },
      immediate: true,
    },
  },
  methods: {
    getPendingChangesKey(item) {
      if (!item) {
        return '';
      }
 
      return joinPaths(
        window.location.pathname.slice(1),
        String(item.id),
        'item-title-pending-changes',
      );
    },
    async setPendingState() {
      const pendingChanges = localStorage.getItem(this.pendingChangesStorageKey);
      const shouldOpen = pendingChanges !== this.title;
 
      if (pendingChanges && shouldOpen) {
        this.title = pendingChanges;
        this.showChangesAlert = true;
        await this.$nextTick();
        this.$refs.sidebarItem.expand();
      } else {
        this.showChangesAlert = false;
      }
    },
    cancel() {
      this.title = this.activeItem.title;
      this.$refs.sidebarItem.collapse();
      this.showChangesAlert = false;
      localStorage.removeItem(this.pendingChangesStorageKey);
    },
    async setActiveBoardItemTitle() {
      const { fullPath, issuableType, isEpicBoard, title } = this;
      const workspacePath = isEpicBoard
        ? { groupPath: fullPath }
        : { projectPath: this.projectPath };
      await this.$apollo.mutate({
        mutation: titleQueries[issuableType].mutation,
        variables: {
          input: {
            ...workspacePath,
            iid: String(this.activeItem.iid),
            title,
          },
        },
      });
    },
    async setTitle() {
      this.$refs.sidebarItem.collapse();
 
      if (!this.title || this.title === this.activeItem.title) {
        return;
      }
 
      try {
        this.loading = true;
        await this.setActiveBoardItemTitle();
        localStorage.removeItem(this.pendingChangesStorageKey);
        this.showChangesAlert = false;
      } catch (e) {
        this.title = this.activeItem.title;
        setError({ error: e, message: this.$options.i18n.updateTitleError });
      } finally {
        this.loading = false;
      }
    },
    handleOffClick() {
      if (this.title !== this.activeItem.title) {
        this.showChangesAlert = true;
        localStorage.setItem(this.pendingChangesStorageKey, this.title);
      } else E{
        this.$refs.sidebarItem.collapse();
      }
    },
  },
  i18n: {
    titlePlaceholder: __('Title'),
    submitButton: __('Save changes'),
    cancelButton: __('Cancel'),
    updateTitleError: __('An error occurred when updating the title'),
    invalidFeedback: __('A title is required'),
    reviewYourChanges: __('Changes to the title have not been saved'),
  },
};
</script>
 
<template>
  <board-editable-item
    ref="sidebarItem"
    toggle-header
    :loading="loading"
    :handle-off-click="false"
    @off-click="handleOffClick"
  >
    <template #title>
      <span data-testid="item-title">
        <gl-link class="gl-reset-color gl-hover-text-blue-800" :href="activeItem.webUrl">
          {{ activeItem.title }}
        </gl-link>
      </span>
    </template>
    <template #collapsed>
      <span class="gl-text-gray-800">{{ activeItem.referencePath }}</span>
    </template>
    <gl-alert v-if="showChangesAlert" variant="warning" class="gl-mb-5" :dismissible="false">
      {{ $options.i18n.reviewYourChanges }}
    </gl-alert>
    <gl-form @submit.prevent="setTitle">
      <gl-form-group :invalid-feedback="$options.i18n.invalidFeedback" :state="validationState">
        <gl-form-input
          v-model="title"
          v-autofocusonshow
          :placeholder="$options.i18n.titlePlaceholder"
          :state="validationState"
        />
      </gl-form-group>
 
      <div class="gl-display-flex gl-w-full gl-justify-content-space-between gl-mt-5">
        <gl-button
          variant="confirm"
          size="small"
          data-testid="submit-button"
          :disabled="!title"
          @click="setTitle"
        >
          {{ $options.i18n.submitButton }}
        </gl-button>
 
        <gl-button size="small" data-testid="cancel-button" @click="cancel">
          {{ $options.i18n.cancelButton }}
        </gl-button>
      </div>
    </gl-form>
  </board-editable-item>
</template>