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.

132 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2022-08-27 11:52:29 +05:30
import { __ } from '~/locale';
2021-03-11 19:13:27 +05:30
import toast from '~/vue_shared/plugins/global_toast';
2023-03-04 22:38:38 +05:30
import { visitUrl } from '~/lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
import Service from './services/sidebar_service';
2023-03-04 22:38:38 +05:30
import Store from './stores/sidebar_store';
2017-08-17 22:00:37 +05:30
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
}
2022-08-13 15:12:31 +05:30
// eslint-disable-next-line no-constructor-return
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);
}
2023-03-04 22:38:38 +05:30
addSelfReview() {
this.store.addReviewer(this.store.currentUser);
}
2017-08-17 22:00:37 +05:30
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) {
2023-03-04 22:38:38 +05:30
const selectedReviewers = this.store.reviewers;
const selectedIds = selectedReviewers.map((u) => u.id);
const suggestedSelectedIds = selectedReviewers.filter((u) => u.suggested).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]
2023-03-04 22:38:38 +05:30
const reviewers = selectedIds.length === 0 ? [0] : selectedIds;
const data = { reviewer_ids: reviewers, suggested_reviewer_ids: suggestedSelectedIds };
2021-01-03 14:25:43 +05:30
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));
}
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()
2022-11-25 23:54:43 +05:30
.then(({ data }) => {
this.processFetchedData(data);
2017-08-17 22:00:37 +05:30
})
2021-09-30 23:02:18 +05:30
.catch(() =>
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-30 23:02:18 +05:30
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
}
}