sidebar_assignees.vue 2.71 KB
Newer Older
1
<script>
2 3 4
import Flash from '~/flash';
import eventHub from '~/sidebar/event_hub';
import Store from '~/sidebar/stores/sidebar_store';
5
import { refreshUserMergeRequestCounts } from '~/commons/nav/user_merge_requests';
6
import AssigneeTitle from './assignee_title.vue';
7
import Assignees from './assignees.vue';
8
import { __ } from '~/locale';
9 10 11

export default {
  name: 'SidebarAssignees',
12 13 14
  components: {
    AssigneeTitle,
    Assignees,
15
  },
Clement Ho's avatar
Clement Ho committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
  props: {
    mediator: {
      type: Object,
      required: true,
    },
    field: {
      type: String,
      required: true,
    },
    signedIn: {
      type: Boolean,
      required: false,
      default: false,
    },
30 31
    issuableType: {
      type: String,
32
      required: false,
33 34
      default: 'issue',
    },
Clement Ho's avatar
Clement Ho committed
35
  },
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  data() {
    return {
      store: new Store(),
      loading: false,
    };
  },
  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 glDropdown
    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);
58 59 60 61 62 63 64 65 66 67 68 69
  },
  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;

Mike Greiling's avatar
Mike Greiling committed
70 71
      this.mediator
        .saveAssignees(this.field)
72
        .then(() => {
73
          this.loading = false;
74 75
          refreshUserMergeRequestCounts();
        })
76
        .catch(() => {
77
          this.loading = false;
78
          return new Flash(__('Error occurred when saving assignees'));
79 80 81 82
        });
    },
  },
};
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
</script>

<template>
  <div>
    <assignee-title
      :number-of-assignees="store.assignees.length"
      :loading="loading || store.isFetching.assignees"
      :editable="store.editable"
      :show-toggle="!signedIn"
    />
    <assignees
      v-if="!store.isFetching.assignees"
      :root-path="store.rootPath"
      :users="store.assignees"
      :editable="store.editable"
98
      :issuable-type="issuableType"
99 100
      class="value"
      @assign-self="assignSelf"
101 102 103
    />
  </div>
</template>