debian-mirror-gitlab/app/assets/javascripts/sidebar/sidebar_mediator.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import Store from '~/sidebar/stores/sidebar_store';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2021-12-11 22:18:48 +05:30
import { __, sprintf } from '~/locale';
2021-03-11 19:13:27 +05:30
import toast from '~/vue_shared/plugins/global_toast';
2022-05-07 20:08:51 +05:30
import { refreshUserMergeRequestCounts } from '~/commons/nav/user_merge_requests';
2021-03-11 19:13:27 +05:30
import { visitUrl } from '../lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
import Service from './services/sidebar_service';
export default class SidebarMediator {
constructor(options) {
if (!SidebarMediator.singleton) {
2018-03-17 18:26:18 +05:30
this.initSingleton(options);
2017-08-17 22:00:37 +05:30
}
return SidebarMediator.singleton;
}
2018-03-17 18:26:18 +05:30
initSingleton(options) {
this.store = new Store(options);
this.service = new Service({
endpoint: options.endpoint,
moveIssueEndpoint: options.moveIssueEndpoint,
projectsAutocompleteEndpoint: options.projectsAutocompleteEndpoint,
2020-04-08 14:13:33 +05:30
fullPath: options.fullPath,
iid: options.iid,
2021-04-17 20:07:23 +05:30
issuableType: options.issuableType,
2018-03-17 18:26:18 +05:30
});
SidebarMediator.singleton = this;
}
2017-08-17 22:00:37 +05:30
assignYourself() {
this.store.addAssignee(this.store.currentUser);
}
2022-03-02 08:16:31 +05:30
async saveAssignees(field) {
2021-03-08 18:12:59 +05:30
const selected = this.store.assignees.map((u) => u.id);
2017-08-17 22:00:37 +05:30
// If there are no ids, that means we have to unassign (which is id = 0)
// And it only accepts an array, hence [0]
2019-12-04 20:38:33 +05:30
const assignees = selected.length === 0 ? [0] : selected;
const data = { assignee_ids: assignees };
2022-03-02 08:16:31 +05:30
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-08-17 22:00:37 +05:30
}
2022-03-02 08:16:31 +05:30
async saveReviewers(field) {
2021-03-08 18:12:59 +05:30
const selected = this.store.reviewers.map((u) => u.id);
2021-01-03 14:25:43 +05:30
// 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-03-02 08:16:31 +05:30
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();
}
2021-01-03 14:25:43 +05:30
}
2021-03-11 19:13:27 +05:30
requestReview({ userId, callback }) {
return this.service
.requestReview(userId)
.then(() => {
2021-12-11 22:18:48 +05:30
this.store.updateReviewer(userId, 'reviewed');
2021-03-11 19:13:27 +05:30
toast(__('Requested review'));
callback(userId, true);
})
.catch(() => callback(userId, false));
}
2022-03-02 08:16:31 +05:30
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-12-11 22:18:48 +05:30
async toggleAttentionRequested(type, { user, callback }) {
try {
const isReviewer = type === 'reviewer';
const reviewerOrAssignee = isReviewer
? this.store.findReviewer(user)
: this.store.findAssignee(user);
await this.service.toggleAttentionRequested(user.id);
if (reviewerOrAssignee.attention_requested) {
toast(
sprintf(__('Removed attention request from @%{username}'), {
username: user.username,
}),
);
} else {
2022-01-26 12:08:38 +05:30
const currentUserId = gon.current_user_id;
if (currentUserId !== user.id) {
2022-03-02 08:16:31 +05:30
this.removeCurrentUserAttentionRequested();
2022-01-26 12:08:38 +05:30
}
2021-12-11 22:18:48 +05:30
toast(sprintf(__('Requested attention from @%{username}'), { username: user.username }));
}
this.store.updateReviewer(user.id, 'attention_requested');
this.store.updateAssignee(user.id, 'attention_requested');
2022-05-07 20:08:51 +05:30
refreshUserMergeRequestCounts();
2021-12-11 22:18:48 +05:30
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 }),
},
});
}
}
2018-03-17 18:26:18 +05:30
setMoveToProjectId(projectId) {
this.store.setMoveToProjectId(projectId);
}
2017-08-17 22:00:37 +05:30
fetch() {
2018-12-13 13:39:08 +05:30
return this.service
.get()
2020-04-08 14:13:33 +05:30
.then(([restResponse, graphQlResponse]) => {
this.processFetchedData(restResponse.data, graphQlResponse.data);
2017-08-17 22:00:37 +05:30
})
2021-09-30 23:02:18 +05:30
.catch(() =>
createFlash({
message: __('Error occurred when fetching sidebar data'),
}),
);
2018-03-17 18:26:18 +05:30
}
processFetchedData(data) {
this.store.setAssigneeData(data);
2021-01-03 14:25:43 +05:30
this.store.setReviewerData(data);
2018-03-17 18:26:18 +05:30
this.store.setTimeTrackingData(data);
}
fetchAutocompleteProjects(searchTerm) {
2019-12-04 20:38:33 +05:30
return this.service.getProjectsAutocomplete(searchTerm).then(({ data }) => {
this.store.setAutocompleteProjects(data);
return this.store.autocompleteProjects;
});
2018-03-17 18:26:18 +05:30
}
moveIssue() {
2019-12-04 20:38:33 +05:30
return this.service.moveIssue(this.store.moveToProjectId).then(({ data }) => {
if (window.location.pathname !== data.web_url) {
visitUrl(data.web_url);
}
});
2017-08-17 22:00:37 +05:30
}
}