Commit d9928d1a authored by Fatih Acet's avatar Fatih Acet

IssueNotesRefactor: Decouple poll from main component to increase reusability.

parent 820c0d6d
......@@ -61,17 +61,14 @@ export default {
});
},
initPolling() {
const { notesPath, lastFetchedAt } = this.$el.parentNode.dataset;
const options = {
endpoint: `${notesPath}?full_data=1`,
lastFetchedAt,
};
const { lastFetchedAt } = $('.js-notes-wrapper')[0].dataset;
this.$store.commit('setLastFetchedAt', lastFetchedAt);
// FIXME: @fatihacet Implement real polling mechanism
setInterval(() => {
this.$store.dispatch('poll', options)
this.$store.dispatch('poll')
.then((res) => {
options.lastFetchedAt = res.last_fetched_at;
this.$store.commit('setLastFetchedAt', res.lastFetchedAt);
})
.catch(() => {
new Flash('Something went wrong while fetching latest comments.'); // eslint-disable-line
......
......@@ -7,6 +7,7 @@ const findNoteObjectById = (notes, id) => notes.filter(n => n.id === id)[0];
const state = {
notes: [],
targetNoteHash: null,
lastFetchedAt: null,
};
const getters = {
......@@ -104,6 +105,9 @@ const mutations = {
});
}
},
setLastFetchedAt(storeState, fetchedAt) {
storeState.lastFetchedAt = fetchedAt;
},
};
const actions = {
......@@ -156,10 +160,10 @@ const actions = {
});
},
poll(context, data) {
const { endpoint, lastFetchedAt } = data;
const { notesPath } = $('.js-notes-wrapper')[0].dataset;
return service
.poll(endpoint, lastFetchedAt)
.poll(`${notesPath}?full_data=1`, context.state.lastFetchedAt)
.then(res => res.json())
.then((res) => {
if (res.notes.length) {
......
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