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