Commit 40097f97 authored by Bryce Johnson's avatar Bryce Johnson

Remove mock code from approvalsApi, init SubbableResource.

parent 7dc3d257
//= require ../stores/approvals_store
//= require subbable_resource
// TODO: Determine whether component-specific store should be accessed here as a member
((MergeRequestWidget) => {
function mockApprovalRequest(payload) {
const store = gl.MergeRequestWidget.Store;
return new Promise((resolve) => {
const approvalsStore = store.data.approvals;
setTimeout(() => {
resolve(approvalsStore);
}, 100);
});
}
(() => {
class ApprovalsApi {
constructor() {
this.resource = gl.ApprovalsResource = new gl.SubbableResource('my/endpoint');
this.store = gl.MergeRequestWidget.ApprovalsStore;
this.isFetching = false;
this.hasBeenFetched = true;
}
fetchApprovals() {
const payload = {
type: 'GET',
};
return this.genericApprovalRequest(payload);
return this.resource.get();
}
approveMergeRequest() {
const payload = {
type: 'POST',
};
return this.genericApprovalRequest(payload).then(() => {
return this.resource.post().then(() => {
gl.MergeRequestWidget.ApprovalsStore.approve();
});
}
unapproveMergeRequest() {
const payload = {
type: 'DELETE',
};
return this.genericApprovalRequest(payload).then(() => {
return this.resource.delete().then(() => {
gl.MergeRequestWidget.ApprovalsStore.unapprove();
});
}
genericApprovalRequest(payload = {}) {
return mockApprovalRequest(payload)
.then(resp => this.updateStore(resp))
.catch((err) => {
throw err;
});
}
updateStore(newState = {}) {
this.store = gl.MergeRequestWidget.Store.data.approvals; // Always update shared store
this.store = gl.MergeRequestWidget.Store.data.approvals;
return Object.assign(this.store, newState);
}
}
gl.MergeRequestWidget.ApprovalsApi = new ApprovalsApi();
})(gl.MergeRequestWidget || (gl.MergeRequestWidget = {}));
})();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment