All files / app/assets/javascripts/sidebar/components/assignees sidebar_assignees.vue

61.53% Statements 16/26
100% Branches 0/0
92.3% Functions 12/13
61.53% Lines 16/26

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                  2x                                                                                     5x               5x     4x           2x       5x 5x 5x                                 1x   1x 1x     2x   2x             2x 2x           4x                                                                      
<script>
import { createAlert } from '~/alert';
import { TYPE_ISSUE } from '~/issues/constants';
import { __ } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import eventHub from '../../event_hub';
import Store from '../../stores/sidebar_store';
import AssigneeTitle from './assignee_title.vue';
import Assignees from './assignees.vue';
import AssigneesRealtime from './assignees_realtime.vue';
 
export default {
  name: 'SidebarAssignees',
  components: {
    AssigneeTitle,
    Assignees,
    AssigneesRealtime,
  },
  mixins: [glFeatureFlagsMixin()],
  props: {
    mediator: {
      type: Object,
      required: true,
    },
    field: {
      type: String,
      required: true,
    },
    issuableType: {
      type: String,
      required: false,
      default: TYPE_ISSUE,
    },
    issuableIid: {
      type: String,
      required: true,
    },
    projectPath: {
      type: String,
      required: true,
    },
    issuableId: {
      type: Number,
      required: true,
    },
    assigneeAvailabilityStatus: {
      type: Object,
      required: false,
      default: () => ({}),
    },
  },
  data() {
    return {
      store: new Store(),
      loading: false,
    };
  },
  computed: {
    shouldEnableRealtime() {
      // Note: Realtime is only available on issues right now, future support for MR wil be built later.
      return this.issuableType === TYPE_ISSUE;
    },
    queryVariables() {
      return {
        iid: this.issuableIid,
        fullPath: this.projectPath,
      };
    },
    relativeUrlRoot() {
      return gon.relative_url_root ?? '';
    },
  },
  created() {
    this.removeAssignee = this.store.removeAssignee.bind(this.store);
    this.addAssignee = this.store.addAssignee.bind(this.store);
    this.removeAllAssignees = this.store.removeAllAssignees.bind(this.store);
 
    // Get events from deprecatedJQueryDropdown
    eventHub.$on('sidebar.removeAssignee', this.removeAssignee);
    eventHub.$on('sidebar.addAssignee', this.addAssignee);
    eventHub.$on('sidebar.removeAllAssignees', this.removeAllAssignees);
    eventHub.$on('sidebar.saveAssignees', this.saveAssignees);
  },
  beforeDestroy() {
    eventHub.$off('sidebar.removeAssignee', this.removeAssignee);
    eventHub.$off('sidebar.addAssignee', this.addAssignee);
    eventHub.$off('sidebar.removeAllAssignees', this.removeAllAssignees);
    eventHub.$off('sidebar.saveAssignees', this.saveAssignees);
  },
  methods: {
    assignSelf() {
      // Notify gl dropdown that we are now assigning to current user
      this.$el.parentElement.dispatchEvent(new Event('assignYourself'));
 
      this.mediator.assignYourself();
      this.saveAssignees();
    },
    saveAssignees() {
      this.loading = true;
 
      this.mediator
        .saveAssignees(this.field)
        .then(() => {
          this.loading = false;
          this.store.resetChanging();
        })
        .catch(() => {
          this.loading = false;
          return createAlert({
            message: __('Error occurred when saving assignees'),
          });
        });
    },
    exposeAvailabilityStatus(users) {
      return users.map(({ username, ...rest }) => ({
        ...rest,
        username,
        availability: this.assigneeAvailabilityStatus[username] || '',
      }));
    },
  },
};
</script>
 
<template>
  <div>
    <assignees-realtime
      v-if="shouldEnableRealtime"
      :issuable-type="issuableType"
      :issuable-id="issuableId"
      :query-variables="queryVariables"
      :mediator="mediator"
    />
    <assignee-title
      :number-of-assignees="store.assignees.length"
      :loading="loading || store.isFetching.assignees"
      :editable="store.editable"
      :changing="store.changing"
    />
    <assignees
      v-if="!store.isFetching.assignees"
      :root-path="relativeUrlRoot"
      :users="exposeAvailabilityStatus(store.assignees)"
      :editable="store.editable"
      :issuable-type="issuableType"
      @assign-self="assignSelf"
    />
  </div>
</template>