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

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import { visitUrl } from '../lib/utils/url_utility';
import Flash from '../flash';
2017-08-17 22:00:37 +05:30
import Service from './services/sidebar_service';
import Store from './stores/sidebar_store';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
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
}
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,
});
SidebarMediator.singleton = this;
}
2017-08-17 22:00:37 +05:30
assignYourself() {
this.store.addAssignee(this.store.currentUser);
}
saveAssignees(field) {
const selected = this.store.assignees.map(u => u.id);
// If there are no ids, that means we have to unassign (which is id = 0)
// And it only accepts an array, hence [0]
return this.service.update(field, selected.length === 0 ? [0] : selected);
}
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()
2017-09-10 17:25:29 +05:30
.then(response => response.json())
2018-12-13 13:39:08 +05:30
.then(data => {
2018-03-17 18:26:18 +05:30
this.processFetchedData(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);
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);
})
2018-12-13 13:39:08 +05:30
.catch(err => {
2018-03-17 18:26:18 +05:30
this.store.setFetchingState('subscriptions', false);
throw err;
});
}
fetchAutocompleteProjects(searchTerm) {
2018-12-13 13:39:08 +05:30
return this.service
.getProjectsAutocomplete(searchTerm)
2018-03-17 18:26:18 +05:30
.then(response => response.json())
2018-12-13 13:39:08 +05:30
.then(data => {
2018-03-17 18:26:18 +05:30
this.store.setAutocompleteProjects(data);
return this.store.autocompleteProjects;
});
}
moveIssue() {
2018-12-13 13:39:08 +05:30
return this.service
.moveIssue(this.store.moveToProjectId)
2018-03-17 18:26:18 +05:30
.then(response => response.json())
2018-12-13 13:39:08 +05:30
.then(data => {
2018-11-08 19:23:39 +05:30
if (window.location.pathname !== data.web_url) {
2018-03-17 18:26:18 +05:30
visitUrl(data.web_url);
}
});
2017-08-17 22:00:37 +05:30
}
}