Commit 0e53b116 authored by Clement Ho's avatar Clement Ho

[skip ci] rename user to assignee

parent 7c022ae7
...@@ -79,6 +79,9 @@ export default { ...@@ -79,6 +79,9 @@ export default {
renderAssignee(index) { renderAssignee(index) {
return !this.showLess || (index < this.defaultRenderCount && this.showLess); return !this.showLess || (index < this.defaultRenderCount && this.showLess);
}, },
avatarUrl(user) {
return user.avatarUrl || user.avatar_url;
},
assigneeUrl(user) { assigneeUrl(user) {
return `${this.rootPath}${user.username}`; return `${this.rootPath}${user.username}`;
}, },
...@@ -118,7 +121,7 @@ export default { ...@@ -118,7 +121,7 @@ export default {
width="24" width="24"
class="avatar avatar-inline s24" class="avatar avatar-inline s24"
:alt="assigneeAlt(user)" :alt="assigneeAlt(user)"
:src="user.avatarUrl" :src="avatarUrl(user)"
/> />
<span class="author"> <span class="author">
{{ user.name }} {{ user.name }}
...@@ -158,7 +161,7 @@ export default { ...@@ -158,7 +161,7 @@ export default {
width="32" width="32"
class="avatar avatar-inline s32" class="avatar avatar-inline s32"
:alt="assigneeAlt(firstUser)" :alt="assigneeAlt(firstUser)"
:src="firstUser.avatarUrl" :src="avatarUrl(firstUser)"
/> />
<span class="author"> <span class="author">
{{ firstUser.name }} {{ firstUser.name }}
...@@ -185,7 +188,7 @@ export default { ...@@ -185,7 +188,7 @@ export default {
width="32" width="32"
class="avatar avatar-inline s32" class="avatar avatar-inline s32"
:alt="assigneeAlt(user)" :alt="assigneeAlt(user)"
:src="user.avatarUrl" :src="avatarUrl(user)"
/> />
</a> </a>
</div> </div>
......
/* global Flash */
import AssigneeTitle from './assignee_title'; import AssigneeTitle from './assignee_title';
import Assignees from './assignees'; import Assignees from './assignees';
...@@ -20,39 +22,45 @@ export default { ...@@ -20,39 +22,45 @@ export default {
'assignee-title': AssigneeTitle, 'assignee-title': AssigneeTitle,
assignees: Assignees, assignees: Assignees,
}, },
computed: {
numberOfAssignees() {
return this.store.selectedUserIds.length;
},
},
methods: { methods: {
assignSelf() { assignSelf() {
// Notify gl dropdown that we are now assigning to current user // Notify gl dropdown that we are now assigning to current user
this.$el.parentElement.dispatchEvent(new Event('assignYourself')); this.$el.parentElement.dispatchEvent(new Event('assignYourself'));
this.mediator.assignYourself(); this.mediator.assignYourself();
this.saveUsers(); this.saveAssignees();
}, },
saveUsers() { saveAssignees() {
this.loading = true; this.loading = true;
function setLoadingFalse() { function setLoadingFalse() {
this.loading = false; this.loading = false;
} }
const setLoadingFalseWrapper = setLoadingFalse.bind(this); this.mediator.saveAssignees(this.field)
.then(setLoadingFalse.bind(this))
this.mediator.saveSelectedUsers(this.field) .catch(() => {
.then(setLoadingFalseWrapper) setLoadingFalse();
.catch(setLoadingFalseWrapper); return new Flash('Error occurred when saving assignees');
});
}, },
}, },
created() { 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 // Get events from glDropdown
eventHub.$on('sidebar.removeUser', this.store.removeUserId.bind(this.store)); eventHub.$on('sidebar.removeAssignee', this.removeAssignee);
eventHub.$on('sidebar.addUser', this.store.addUserId.bind(this.store)); eventHub.$on('sidebar.addAssignee', this.addAssignee);
eventHub.$on('sidebar.removeAllUsers', this.store.removeAllUserIds.bind(this.store)); eventHub.$on('sidebar.removeAllAssignees', this.removeAllAssignees);
eventHub.$on('sidebar.saveUsers', this.saveUsers); 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() { beforeMount() {
this.field = this.$el.dataset.field; this.field = this.$el.dataset.field;
...@@ -60,15 +68,14 @@ export default { ...@@ -60,15 +68,14 @@ export default {
template: ` template: `
<div> <div>
<assignee-title <assignee-title
:number-of-assignees="store.selectedUserIds.length" :number-of-assignees="store.assignees.length"
:loading="loading" :loading="loading"
:editable="store.editable" :editable="store.editable"
/> />
<assignees <assignees
class="value" class="value"
v-if="!loading"
:root-path="store.rootPath" :root-path="store.rootPath"
:users="store.renderedUsers" :users="store.assignees"
@assign-self="assignSelf" @assign-self="assignSelf"
/> />
</div> </div>
......
...@@ -15,33 +15,22 @@ export default class SidebarMediator { ...@@ -15,33 +15,22 @@ export default class SidebarMediator {
} }
assignYourself() { assignYourself() {
this.store.addUserId(this.store.currentUserId); this.store.addAssignee(this.store.currentUser);
} }
saveSelectedUsers(field) { saveAssignees(field) {
return new Promise((resolve, reject) => { const selected = this.store.assignees.map((u) => u.id);
const selected = this.store.selectedUserIds;
// If there are no ids, that means we have to unassign (which is id = 0) // If there are no ids, that means we have to unassign (which is id = 0)
// And it only accepts an array, hence [0] // And it only accepts an array, hence [0]
this.service.update(field, selected.length === 0 ? [0] : selected) return this.service.update(field, selected.length === 0 ? [0] : selected);
.then((response) => {
const data = response.json();
this.store.processUserData(data);
resolve();
})
.catch(() => {
reject();
return new Flash('Error occurred when saving users');
});
});
} }
fetch() { fetch() {
this.service.get() this.service.get()
.then((response) => { .then((response) => {
const data = response.json(); const data = response.json();
this.store.processUserData(data); this.store.processAssigneeData(data);
this.store.processTimeTrackingData(data); this.store.processTimeTrackingData(data);
}) })
.catch(() => new Flash('Error occured when fetching sidebar data')); .catch(() => new Flash('Error occured when fetching sidebar data'));
......
export default class SidebarStore { export default class SidebarStore {
constructor(store) { constructor(store) {
if (!SidebarStore.singleton) { if (!SidebarStore.singleton) {
const { currentUserId, rootPath, editable } = store; const { currentUser, rootPath, editable } = store;
this.currentUserId = currentUserId; this.currentUser = currentUser;
this.rootPath = rootPath; this.rootPath = rootPath;
this.editable = editable; this.editable = editable;
this.timeEstimate = 0; this.timeEstimate = 0;
this.totalTimeSpent = 0; this.totalTimeSpent = 0;
this.humanTimeEstimate = ''; this.humanTimeEstimate = '';
this.humanTimeSpent = ''; this.humanTimeSpent = '';
this.selectedUserIds = []; this.assignees = [];
this.renderedUsers = [];
SidebarStore.singleton = this; SidebarStore.singleton = this;
} }
...@@ -18,17 +17,14 @@ export default class SidebarStore { ...@@ -18,17 +17,14 @@ export default class SidebarStore {
return SidebarStore.singleton; return SidebarStore.singleton;
} }
processUserData(data) { processAssigneeData(data) {
if (data.assignees) { if (data.assignees) {
this.renderedUsers = data.assignees.map(a => { this.assignees = data.assignees.map(a => {
a.avatarUrl = a.avatar_url; a.avatarUrl = a.avatar_url;
delete a.avatar_url; delete a.avatar_url;
return a; return a;
}); });
this.removeAllUserIds();
this.renderedUsers.map(u => this.addUserId(u.id));
} }
} }
...@@ -39,18 +35,23 @@ export default class SidebarStore { ...@@ -39,18 +35,23 @@ export default class SidebarStore {
this.humanTimeSpent = data.human_time_spent; this.humanTimeSpent = data.human_time_spent;
} }
addUserId(id) { addAssignee(assignee) {
// Prevent duplicate user id's from being added if (!this.findAssignee(assignee)) {
if (this.selectedUserIds.indexOf(id) === -1) { this.assignees.push(assignee);
this.selectedUserIds.push(id); }
} }
findAssignee(findAssignee) {
return this.assignees.filter(assignee => assignee.id === findAssignee.id)[0];
} }
removeUserId(id) { removeAssignee(removeAssignee) {
this.selectedUserIds = this.selectedUserIds.filter(uid => uid !== id); if (removeAssignee) {
this.assignees = this.assignees.filter(assignee => assignee.id !== removeAssignee.id);
}
} }
removeAllUserIds() { removeAllAssignees() {
this.selectedUserIds = []; this.assignees = [];
} }
} }
...@@ -277,7 +277,7 @@ import eventHub from './sidebar/event_hub'; ...@@ -277,7 +277,7 @@ import eventHub from './sidebar/event_hub';
defaultLabel: defaultLabel, defaultLabel: defaultLabel,
hidden: function(e) { hidden: function(e) {
if ($dropdown.hasClass('js-multiselect')) { if ($dropdown.hasClass('js-multiselect')) {
eventHub.$emit('sidebar.saveUsers'); eventHub.$emit('sidebar.saveAssignees');
} }
$selectbox.hide(); $selectbox.hide();
...@@ -311,7 +311,9 @@ import eventHub from './sidebar/event_hub'; ...@@ -311,7 +311,9 @@ import eventHub from './sidebar/event_hub';
.find(`input[name='${$dropdown.data('field-name')}'][value=${firstSelectedId}]`); .find(`input[name='${$dropdown.data('field-name')}'][value=${firstSelectedId}]`);
firstSelected.remove(); firstSelected.remove();
eventHub.$emit('sidebar.removeUser', firstSelectedId); eventHub.$emit('sidebar.removeAssignee', {
id: firstSelectedId,
});
} }
} }
...@@ -321,10 +323,10 @@ import eventHub from './sidebar/event_hub'; ...@@ -321,10 +323,10 @@ import eventHub from './sidebar/event_hub';
const id = parseInt(element.value, 10); const id = parseInt(element.value, 10);
element.remove(); element.remove();
}); });
eventHub.$emit('sidebar.removeAllUsers'); eventHub.$emit('sidebar.removeAllAssignees');
} else if (isActive) { } else if (isActive) {
// user selected // user selected
eventHub.$emit('sidebar.addUser', user.id); eventHub.$emit('sidebar.addAssignee', user);
// Remove unassigned selection (if it was previously selected) // Remove unassigned selection (if it was previously selected)
const unassignedSelected = $dropdown.closest('.selectbox') const unassignedSelected = $dropdown.closest('.selectbox')
...@@ -340,7 +342,7 @@ import eventHub from './sidebar/event_hub'; ...@@ -340,7 +342,7 @@ import eventHub from './sidebar/event_hub';
} }
// User unselected // User unselected
eventHub.$emit('sidebar.removeUser', user.id); eventHub.$emit('sidebar.removeAssignee', user);
} }
} }
......
...@@ -219,7 +219,7 @@ ...@@ -219,7 +219,7 @@
gl.sidebarOptions = { gl.sidebarOptions = {
endpoint: "#{issuable_json_path(issuable)}", endpoint: "#{issuable_json_path(issuable)}",
editable: #{can_edit_issuable ? true : false}, editable: #{can_edit_issuable ? true : false},
currentUserId: #{current_user.id}, currentUser: #{current_user.to_json(only: [:username, :id, :name], methods: :avatar_url)},
rootPath: "#{root_path}" rootPath: "#{root_path}"
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment