summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/sidebar/components/assignees/sidebar_assignees.js
blob: 74c17bc14a2e99a0df450f580f7e4c21b3ffa07f (plain)
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
import Flash from '../../../flash';
import AssigneeTitle from './assignee_title';
import Assignees from './assignees';

import Store from '../../stores/sidebar_store';
import Mediator from '../../sidebar_mediator';

import eventHub from '../../event_hub';

export default {
  name: 'SidebarAssignees',
  data() {
    return {
      mediator: new Mediator(),
      store: new Store(),
      loading: false,
      field: '',
    };
  },
  components: {
    'assignee-title': AssigneeTitle,
    assignees: Assignees,
  },
  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;

      function setLoadingFalse() {
        this.loading = false;
      }

      this.mediator.saveAssignees(this.field)
        .then(setLoadingFalse.bind(this))
        .catch(() => {
          setLoadingFalse();
          return new Flash('Error occurred when saving assignees');
        });
    },
  },
  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);
  },
  beforeMount() {
    this.field = this.$el.dataset.field;
    this.signedIn = typeof this.$el.dataset.signedIn !== 'undefined';
  },
  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"
        class="value"
        :root-path="store.rootPath"
        :users="store.assignees"
        :editable="store.editable"
        @assign-self="assignSelf"
      />
    </div>
  `,
};