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

Remove mock code from approvalsApi, init SubbableResource.

parent 7dc3d257
//= require ../stores/approvals_store //= 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 { class ApprovalsApi {
constructor() { constructor() {
this.resource = gl.ApprovalsResource = new gl.SubbableResource('my/endpoint');
this.store = gl.MergeRequestWidget.ApprovalsStore; this.store = gl.MergeRequestWidget.ApprovalsStore;
this.isFetching = false;
this.hasBeenFetched = true;
} }
fetchApprovals() { fetchApprovals() {
const payload = { return this.resource.get();
type: 'GET',
};
return this.genericApprovalRequest(payload);
} }
approveMergeRequest() { approveMergeRequest() {
const payload = { return this.resource.post().then(() => {
type: 'POST',
};
return this.genericApprovalRequest(payload).then(() => {
gl.MergeRequestWidget.ApprovalsStore.approve(); gl.MergeRequestWidget.ApprovalsStore.approve();
}); });
} }
unapproveMergeRequest() { unapproveMergeRequest() {
const payload = { return this.resource.delete().then(() => {
type: 'DELETE',
};
return this.genericApprovalRequest(payload).then(() => {
gl.MergeRequestWidget.ApprovalsStore.unapprove(); gl.MergeRequestWidget.ApprovalsStore.unapprove();
}); });
} }
genericApprovalRequest(payload = {}) {
return mockApprovalRequest(payload)
.then(resp => this.updateStore(resp))
.catch((err) => {
throw err;
});
}
updateStore(newState = {}) { 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); return Object.assign(this.store, newState);
} }
} }
gl.MergeRequestWidget.ApprovalsApi = new ApprovalsApi(); 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