From bb2e4c8d02c5dd44cc7975441fdf29550bd28f6e Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Tue, 2 Apr 2019 00:17:35 +0300 Subject: [PATCH] Add infinite scroll --- src/api/reports.js | 6 + src/api/reportsData.js | 218 +++++++++++++++++++++++++++++++++++ src/store/modules/reports.js | 61 ++++------ src/store/modules/users.js | 4 +- src/views/reports/index.vue | 20 +++- 5 files changed, 268 insertions(+), 41 deletions(-) create mode 100644 src/api/reports.js create mode 100644 src/api/reportsData.js diff --git a/src/api/reports.js b/src/api/reports.js new file mode 100644 index 00000000..500ecfef --- /dev/null +++ b/src/api/reports.js @@ -0,0 +1,6 @@ +import { reports } from './reportsData' + +export function fetchReports(index, size) { + const response = reports.slice(index, index + size) + return response +} diff --git a/src/api/reportsData.js b/src/api/reportsData.js new file mode 100644 index 00000000..bc86469b --- /dev/null +++ b/src/api/reportsData.js @@ -0,0 +1,218 @@ +export const reports = [ + { + id: '1', + timestamp: '2019/4/12', + local: true, + from: 'John', // actor nickname + object: 'Bob', // user nickname + header: 'Report #1', // content + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, + { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } + ] + }, + { + id: '2', + timestamp: '2019/4/1', + local: true, + from: 'Max', + object: 'Vic', + header: 'Report #2', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, + { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } + ] + }, + { + id: '3', + timestamp: '2019/2/28', + local: true, + from: 'Tim', + object: 'Jen', + header: 'Report #3', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] + }, + { + id: '4', + timestamp: '2019/4/12', + local: true, + from: 'John', // actor nickname + object: 'Bob', // user nickname + header: 'Report #4', // content + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, + { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } + ] + }, + { + id: '5', + timestamp: '2019/4/1', + local: true, + from: 'Max', + object: 'Vic', + header: 'Report #5', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, + { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } + ] + }, + { + id: '6', + timestamp: '2019/2/28', + local: true, + from: 'Tim', + object: 'Jen', + header: 'Report #6', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] + }, + { + id: '7', + timestamp: '2019/4/12', + local: true, + from: 'John', // actor nickname + object: 'Bob', // user nickname + header: 'Report #7', // content + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, + { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } + ] + }, + { + id: '8', + timestamp: '2019/4/1', + local: true, + from: 'Max', + object: 'Vic', + header: 'Report #8', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, + { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } + ] + }, + { + id: '9', + timestamp: '2019/2/28', + local: true, + from: 'Tim', + object: 'Jen', + header: 'Report #9', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] + }, + { + id: '10', + timestamp: '2019/4/12', + local: true, + from: 'John', // actor nickname + object: 'Bob', // user nickname + header: 'Report #10', // content + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, + { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } + ] + }, + { + id: '11', + timestamp: '2019/4/1', + local: true, + from: 'Max', + object: 'Vic', + header: 'Report #11', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, + { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } + ] + }, + { + id: '12', + timestamp: '2019/2/28', + local: true, + from: 'Tim', + object: 'Jen', + header: 'Report #12', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] + }, + { + id: '13', + timestamp: '2019/4/12', + local: true, + from: 'John', // actor nickname + object: 'Bob', // user nickname + header: 'Report #13', // content + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, + { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } + ] + }, + { + id: '14', + timestamp: '2019/4/1', + local: true, + from: 'Max', + object: 'Vic', + header: 'Report #14', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, + { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } + ] + }, + { + id: '15', + timestamp: '2019/2/28', + local: true, + from: 'Tim', + object: 'Jen', + header: 'Report #15', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] + }, + { + id: '16', + timestamp: '2019/4/12', + local: true, + from: 'John', // actor nickname + object: 'Bob', // user nickname + header: 'Report #16', // content + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, + { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } + ] + }, + { + id: '17', + timestamp: '2019/4/1', + local: true, + from: 'Max', + object: 'Vic', + header: 'Report #17', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [ + { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, + { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } + ] + }, + { + id: '18', + timestamp: '2019/2/28', + local: true, + from: 'Tim', + object: 'Jen', + header: 'Report #18', + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] + } +] diff --git a/src/store/modules/reports.js b/src/store/modules/reports.js index cb0bdd22..279f1994 100644 --- a/src/store/modules/reports.js +++ b/src/store/modules/reports.js @@ -1,50 +1,35 @@ +import { fetchReports } from '@/api/reports' + const reports = { state: { - fetchedReports: [ - { - id: '1', - timestamp: '2019/4/12', - local: true, - from: 'John', // actor nickname - object: 'Bob', // user nickname - header: 'Report #1', // content - content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', - notes: [ - { author: 'Nick', text: 'Lorem ipsum', timestamp: '2019/4/13' }, - { author: 'Val', text: 'dolor sit amet', timestamp: '2019/4/13' } - ] - }, - { - id: '2', - timestamp: '2019/4/1', - local: true, - from: 'Max', - object: 'Vic', - header: 'Report #2', - content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', - notes: [ - { author: 'Tony', text: 'consectetur adipiscing elit', timestamp: '2019/4/2' }, - { author: 'Zac', text: 'sed do eiusmod tempor incididunt', timestamp: '2019/4/3' } - ] - }, - { - id: '3', - timestamp: '2019/2/28', - local: true, - from: 'Tim', - object: 'Jen', - header: 'Report #3', - content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', - notes: [{ author: 'Bruce', text: 'ut labore et dolore magna aliqua', timestamp: '2019/3/1' }] - } - ] + fetchedReports: [], + indexOfLastShownReport: 0, + size: 3, + loading: true }, mutations: { + SET_REPORTS: (state, reports) => { + state.fetchedReports = state.fetchedReports.concat(reports) + }, SET_REPORT: (state, { index, report }) => { state.fetchedReports[index] = report + }, + SET_LOADING: (state, status) => { + state.loading = status + }, + SET_INDEX: (state) => { + state.indexOfLastShownReport = state.indexOfLastShownReport + state.size } }, actions: { + async FetchReports({ commit, state, getters }) { + commit('SET_LOADING', true) + const response = fetchReports(state.indexOfLastShownReport, state.size) + + commit('SET_REPORTS', response) + commit('SET_INDEX') + commit('SET_LOADING', false) + }, async AddNote({ commit, state, getters }, { reportId, note }) { const report = state.fetchedReports.find(report => report.id === reportId) report.notes = [...report.notes, note] diff --git a/src/store/modules/users.js b/src/store/modules/users.js index 56eb07a0..4e6faf08 100644 --- a/src/store/modules/users.js +++ b/src/store/modules/users.js @@ -48,11 +48,11 @@ const users = { }, actions: { async FetchUsers({ commit, state, getters }, { page }) { + commit('SET_LOADING', true) + const filters = Object.keys(state.filters).filter(filter => state.filters[filter]).join() const response = await fetchUsers(filters, getters.authHost, getters.token, page) - commit('SET_LOADING', true) - loadUsers(commit, page, response.data) }, async ToggleUserActivation({ commit, getters }, nickname) { diff --git a/src/views/reports/index.vue b/src/views/reports/index.vue index 83393c5c..322f9b54 100644 --- a/src/views/reports/index.vue +++ b/src/views/reports/index.vue @@ -3,7 +3,7 @@

{{ $t('reports.reports') }}

- +
@@ -15,9 +15,27 @@ import TimelineItem from './components/TimelineItem' export default { components: { TimelineItem }, computed: { + loading() { + return this.$store.state.users.loading + }, reports() { return this.$store.state.reports.fetchedReports } + }, + mounted() { + this.$store.dispatch('FetchReports') + this.scroll(this.reports) + }, + methods: { + scroll(reports) { + window.onscroll = () => { + const bottomOfWindow = document.documentElement.scrollHeight - document.documentElement.scrollTop === document.documentElement.clientHeight + + if (bottomOfWindow) { + this.$store.dispatch('FetchReports') + } + } + } } }