2017-12-11 07:28:11 -05:00
|
|
|
import { visitUrl } from '../lib/utils/url_utility';
|
2017-10-02 08:32:53 -04:00
|
|
|
import Flash from '../flash';
|
2017-05-04 08:11:15 -04:00
|
|
|
import Service from './services/sidebar_service';
|
|
|
|
import Store from './stores/sidebar_store';
|
|
|
|
|
|
|
|
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,
|
|
|
|
toggleSubscriptionEndpoint: options.toggleSubscriptionEndpoint,
|
|
|
|
moveIssueEndpoint: options.moveIssueEndpoint,
|
|
|
|
projectsAutocompleteEndpoint: options.projectsAutocompleteEndpoint,
|
|
|
|
});
|
|
|
|
SidebarMediator.singleton = this;
|
|
|
|
}
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
setMoveToProjectId(projectId) {
|
|
|
|
this.store.setMoveToProjectId(projectId);
|
|
|
|
}
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
fetch() {
|
2017-12-03 21:43:10 -05:00
|
|
|
return this.service.get()
|
2017-07-12 10:47:09 -04:00
|
|
|
.then(response => response.json())
|
|
|
|
.then((data) => {
|
2017-12-03 21:43:10 -05:00
|
|
|
this.processFetchedData(data);
|
2017-05-04 08:11:15 -04:00
|
|
|
})
|
2017-09-25 16:02:08 -04:00
|
|
|
.catch(() => new Flash('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);
|
|
|
|
this.store.setTimeTrackingData(data);
|
|
|
|
this.store.setParticipantsData(data);
|
|
|
|
this.store.setSubscriptionsData(data);
|
|
|
|
}
|
|
|
|
|
2017-10-31 12:15:03 -04:00
|
|
|
toggleSubscription() {
|
|
|
|
this.store.setFetchingState('subscriptions', true);
|
|
|
|
return this.service.toggleSubscription()
|
|
|
|
.then(() => {
|
|
|
|
this.store.setSubscribedState(!this.store.subscribed);
|
|
|
|
this.store.setFetchingState('subscriptions', false);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.store.setFetchingState('subscriptions', false);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
fetchAutocompleteProjects(searchTerm) {
|
|
|
|
return this.service.getProjectsAutocomplete(searchTerm)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
this.store.setAutocompleteProjects(data);
|
|
|
|
return this.store.autocompleteProjects;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
moveIssue() {
|
|
|
|
return this.service.moveIssue(this.store.moveToProjectId)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
if (location.pathname !== data.web_url) {
|
2017-12-11 07:28:11 -05:00
|
|
|
visitUrl(data.web_url);
|
2017-08-14 03:26:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|