2020-01-01 13:55:28 +05:30
|
|
|
import Store from 'ee_else_ce/sidebar/stores/sidebar_store';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { __ } from '~/locale';
|
|
|
|
import toast from '~/vue_shared/plugins/global_toast';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { deprecatedCreateFlash as Flash } from '../flash';
|
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,
|
|
|
|
toggleSubscriptionEndpoint: options.toggleSubscriptionEndpoint,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
return this.service.update(field, data);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
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 };
|
|
|
|
|
|
|
|
return this.service.update(field, data);
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
requestReview({ userId, callback }) {
|
|
|
|
return this.service
|
|
|
|
.requestReview(userId)
|
|
|
|
.then(() => {
|
|
|
|
this.store.updateReviewer(userId);
|
|
|
|
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()
|
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
|
|
|
})
|
2019-07-31 22:56:46 +05:30
|
|
|
.catch(() => new Flash(__('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);
|
|
|
|
this.store.setParticipantsData(data);
|
|
|
|
this.store.setSubscriptionsData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleSubscription() {
|
|
|
|
this.store.setFetchingState('subscriptions', true);
|
2018-12-13 13:39:08 +05:30
|
|
|
return this.service
|
|
|
|
.toggleSubscription()
|
2018-03-17 18:26:18 +05:30
|
|
|
.then(() => {
|
|
|
|
this.store.setSubscribedState(!this.store.subscribed);
|
|
|
|
this.store.setFetchingState('subscriptions', false);
|
|
|
|
})
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((err) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
this.store.setFetchingState('subscriptions', false);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|