2022-02-07 10:15:53 -05:00
|
|
|
import Store from '~/sidebar/stores/sidebar_store';
|
2021-06-21 23:07:55 -04:00
|
|
|
import createFlash from '~/flash';
|
2021-11-09 10:12:42 -05:00
|
|
|
import { __, sprintf } from '~/locale';
|
2021-02-14 13:09:20 -05:00
|
|
|
import toast from '~/vue_shared/plugins/global_toast';
|
2022-03-02 04:13:50 -05:00
|
|
|
import { refreshUserMergeRequestCounts } from '~/commons/nav/user_merge_requests';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { visitUrl } from '../lib/utils/url_utility';
|
2017-05-04 08:11:15 -04:00
|
|
|
import Service from './services/sidebar_service';
|
|
|
|
|
|
|
|
export default class SidebarMediator {
|
|
|
|
constructor(options) {
|
|
|
|
if (!SidebarMediator.singleton) {
|
2017-12-03 21:43:10 -05:00
|
|
|
this.initSingleton(options);
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|
|
|
|
return SidebarMediator.singleton;
|
|
|
|
}
|
|
|
|
|
2017-12-03 21:43:10 -05:00
|
|
|
initSingleton(options) {
|
|
|
|
this.store = new Store(options);
|
|
|
|
this.service = new Service({
|
|
|
|
endpoint: options.endpoint,
|
|
|
|
moveIssueEndpoint: options.moveIssueEndpoint,
|
|
|
|
projectsAutocompleteEndpoint: options.projectsAutocompleteEndpoint,
|
2020-03-05 13:08:19 -05:00
|
|
|
fullPath: options.fullPath,
|
2020-03-13 11:09:21 -04:00
|
|
|
iid: options.iid,
|
2021-03-01 13:11:21 -05:00
|
|
|
issuableType: options.issuableType,
|
2017-12-03 21:43:10 -05:00
|
|
|
});
|
|
|
|
SidebarMediator.singleton = this;
|
|
|
|
}
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
assignYourself() {
|
|
|
|
this.store.addAssignee(this.store.currentUser);
|
|
|
|
}
|
|
|
|
|
2022-01-12 10:13:54 -05:00
|
|
|
async saveAssignees(field) {
|
2020-12-23 19:10:25 -05:00
|
|
|
const selected = this.store.assignees.map((u) => u.id);
|
2017-05-04 08:11:15 -04:00
|
|
|
|
|
|
|
// If there are no ids, that means we have to unassign (which is id = 0)
|
|
|
|
// And it only accepts an array, hence [0]
|
2019-09-10 15:07:06 -04:00
|
|
|
const assignees = selected.length === 0 ? [0] : selected;
|
|
|
|
const data = { assignee_ids: assignees };
|
|
|
|
|
2022-01-12 10:13:54 -05:00
|
|
|
try {
|
|
|
|
const res = await this.service.update(field, data);
|
|
|
|
|
|
|
|
this.store.overwrite('assignees', res.data.assignees);
|
|
|
|
|
|
|
|
if (res.data.reviewers) {
|
|
|
|
this.store.overwrite('reviewers', res.data.reviewers);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(res);
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|
|
|
|
|
2022-01-12 10:13:54 -05:00
|
|
|
async saveReviewers(field) {
|
2020-12-23 19:10:25 -05:00
|
|
|
const selected = this.store.reviewers.map((u) => u.id);
|
2020-09-29 05:09:49 -04:00
|
|
|
|
|
|
|
// If there are no ids, that means we have to unassign (which is id = 0)
|
|
|
|
// And it only accepts an array, hence [0]
|
|
|
|
const reviewers = selected.length === 0 ? [0] : selected;
|
|
|
|
const data = { reviewer_ids: reviewers };
|
|
|
|
|
2022-01-12 10:13:54 -05:00
|
|
|
try {
|
|
|
|
const res = await this.service.update(field, data);
|
|
|
|
|
|
|
|
this.store.overwrite('reviewers', res.data.reviewers);
|
|
|
|
this.store.overwrite('assignees', res.data.assignees);
|
|
|
|
|
|
|
|
return Promise.resolve(res);
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2020-09-29 05:09:49 -04:00
|
|
|
}
|
|
|
|
|
2021-02-04 01:09:22 -05:00
|
|
|
requestReview({ userId, callback }) {
|
|
|
|
return this.service
|
|
|
|
.requestReview(userId)
|
|
|
|
.then(() => {
|
2021-11-09 10:12:42 -05:00
|
|
|
this.store.updateReviewer(userId, 'reviewed');
|
2021-02-04 01:09:22 -05:00
|
|
|
toast(__('Requested review'));
|
2021-02-11 10:09:11 -05:00
|
|
|
callback(userId, true);
|
2021-02-04 01:09:22 -05:00
|
|
|
})
|
2021-02-11 10:09:11 -05:00
|
|
|
.catch(() => callback(userId, false));
|
2021-02-04 01:09:22 -05:00
|
|
|
}
|
|
|
|
|
2022-01-12 10:13:54 -05:00
|
|
|
removeCurrentUserAttentionRequested() {
|
|
|
|
const currentUserId = gon.current_user_id;
|
|
|
|
|
|
|
|
const currentUserReviewer = this.store.findReviewer({ id: currentUserId });
|
|
|
|
const currentUserAssignee = this.store.findAssignee({ id: currentUserId });
|
|
|
|
|
|
|
|
if (currentUserReviewer?.attention_requested || currentUserAssignee?.attention_requested) {
|
|
|
|
// Update current users attention_requested state
|
|
|
|
this.store.updateReviewer(currentUserId, 'attention_requested');
|
|
|
|
this.store.updateAssignee(currentUserId, 'attention_requested');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:12:37 -05:00
|
|
|
async toggleAttentionRequested(type, { user, callback }) {
|
2021-11-09 10:12:42 -05:00
|
|
|
try {
|
|
|
|
const isReviewer = type === 'reviewer';
|
|
|
|
const reviewerOrAssignee = isReviewer
|
|
|
|
? this.store.findReviewer(user)
|
|
|
|
: this.store.findAssignee(user);
|
|
|
|
|
2021-11-16 19:12:37 -05:00
|
|
|
await this.service.toggleAttentionRequested(user.id);
|
|
|
|
|
|
|
|
if (reviewerOrAssignee.attention_requested) {
|
2021-11-09 10:12:42 -05:00
|
|
|
toast(
|
|
|
|
sprintf(__('Removed attention request from @%{username}'), {
|
|
|
|
username: user.username,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
2021-12-08 07:13:04 -05:00
|
|
|
const currentUserId = gon.current_user_id;
|
|
|
|
|
|
|
|
if (currentUserId !== user.id) {
|
2022-01-12 10:13:54 -05:00
|
|
|
this.removeCurrentUserAttentionRequested();
|
2021-12-08 07:13:04 -05:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:12:42 -05:00
|
|
|
toast(sprintf(__('Requested attention from @%{username}'), { username: user.username }));
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:12:37 -05:00
|
|
|
this.store.updateReviewer(user.id, 'attention_requested');
|
|
|
|
this.store.updateAssignee(user.id, 'attention_requested');
|
2021-11-09 10:12:42 -05:00
|
|
|
|
2022-03-02 04:13:50 -05:00
|
|
|
refreshUserMergeRequestCounts();
|
2021-11-09 10:12:42 -05:00
|
|
|
callback();
|
|
|
|
} catch (error) {
|
|
|
|
callback();
|
|
|
|
createFlash({
|
|
|
|
message: sprintf(__('Updating the attention request for %{username} failed.'), {
|
|
|
|
username: user.username,
|
|
|
|
}),
|
|
|
|
error,
|
|
|
|
captureError: true,
|
|
|
|
actionConfig: {
|
|
|
|
title: __('Try again'),
|
|
|
|
clickHandler: () => this.toggleAttentionRequired(type, { user, callback }),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
setMoveToProjectId(projectId) {
|
|
|
|
this.store.setMoveToProjectId(projectId);
|
|
|
|
}
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
fetch() {
|
2018-10-30 16:28:31 -04:00
|
|
|
return this.service
|
|
|
|
.get()
|
2020-03-05 13:08:19 -05:00
|
|
|
.then(([restResponse, graphQlResponse]) => {
|
|
|
|
this.processFetchedData(restResponse.data, graphQlResponse.data);
|
2017-05-04 08:11:15 -04:00
|
|
|
})
|
2021-06-20 23:10:27 -04:00
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('Error occurred when fetching sidebar data'),
|
|
|
|
}),
|
|
|
|
);
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|
2017-08-14 03:26:19 -04:00
|
|
|
|
2017-12-03 21:43:10 -05:00
|
|
|
processFetchedData(data) {
|
|
|
|
this.store.setAssigneeData(data);
|
2020-09-29 05:09:49 -04:00
|
|
|
this.store.setReviewerData(data);
|
2017-12-03 21:43:10 -05:00
|
|
|
this.store.setTimeTrackingData(data);
|
2017-10-31 12:15:03 -04:00
|
|
|
}
|
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
fetchAutocompleteProjects(searchTerm) {
|
2019-09-10 15:07:06 -04:00
|
|
|
return this.service.getProjectsAutocomplete(searchTerm).then(({ data }) => {
|
|
|
|
this.store.setAutocompleteProjects(data);
|
|
|
|
return this.store.autocompleteProjects;
|
|
|
|
});
|
2017-08-14 03:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
moveIssue() {
|
2019-09-10 15:07:06 -04:00
|
|
|
return this.service.moveIssue(this.store.moveToProjectId).then(({ data }) => {
|
|
|
|
if (window.location.pathname !== data.web_url) {
|
|
|
|
visitUrl(data.web_url);
|
|
|
|
}
|
|
|
|
});
|
2017-08-14 03:26:19 -04:00
|
|
|
}
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|