Skip to content

Commit

Permalink
add paginated request when reach end of list
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Sep 20, 2023
1 parent 2b2d8ba commit 14dd511
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,20 @@ export default {
const fileInfo = this.generateViewerObject(this)

if (this.isSharedItemsTab && this.sharedItemsType === SHARED_ITEM.TYPES.MEDIA) {
// Get available media files from store and put them to the list to navigate through slides
const mediaFiles = this.$store.getters.sharedItems(this.$store.getters.getToken())?.media
const list = Object.values(mediaFiles).reverse()
const token = this.$store.getters.getToken()
const getRevertedList = (items) => Object.values(items).reverse()
.map(item => this.generateViewerObject(item.messageParameters.file))

this.openViewer(this.internalAbsolutePath, list, fileInfo)
// Get available media files from store and put them to the list to navigate through slides
const mediaFiles = this.$store.getters.sharedItems(token).media
const list = getRevertedList(mediaFiles)
const loadMore = async () => {
const { messages } = await this.$store.dispatch('getSharedItems',
{ token, type: SHARED_ITEM.TYPES.MEDIA })
return getRevertedList(messages)
}

this.openViewer(this.internalAbsolutePath, list, fileInfo, loadMore)
} else {
this.openViewer(this.internalAbsolutePath, [fileInfo], fileInfo)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ export default {
}
},

fetchItems(type) {
async fetchItems(type) {
this.isRequestingMoreItems[this.activeTab] = true
const hasMoreItems = this.$store.dispatch('getSharedItems', {
const { hasMoreItems } = await this.$store.dispatch('getSharedItems', {
token: this.token,
type,
})
Expand Down
5 changes: 4 additions & 1 deletion src/composables/useViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useStore } from './useStore.js'
* @param {string} path - The path to the file to be open
* @param {Array<object>} list - The list of the files to be opened
* @param {object} [fileInfo] - The known file info
* @param {Function} [loadMore] - The callback to load additional content
*/

/**
Expand Down Expand Up @@ -130,7 +131,7 @@ export function useViewer() {
/**
* @type {OpenViewer}
*/
const openViewer = async (path, list, fileInfo) => {
const openViewer = async (path, list, fileInfo, loadMore) => {
if (!OCA.Viewer) {
return false
}
Expand All @@ -152,6 +153,8 @@ export function useViewer() {
isViewerOpen.value = false
store.dispatch('setCallViewMode', { isViewerOverlay: false })
},
loadMore,
canLoop: false,
})

// Wait Viewer to be mounted
Expand Down
8 changes: 4 additions & 4 deletions src/store/sharedItemsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ const actions = {
if (!state.sharedItemsByConversationAndType[token]
|| !state.sharedItemsByConversationAndType[token][type]) {
console.error('Missing overview for shared items in ', token)
return false
return { hasMoreItems: false, messages: [] }
}

const limit = 20
const lastKnownMessageId = Math.min.apply(Math, Object.keys(state.sharedItemsByConversationAndType[token][type]))
try {
const response = await getSharedItems(token, type, lastKnownMessageId, limit)
const messages = response.data.ocs.data
const hasMore = messages.length >= limit
const hasMoreItems = messages.length >= limit
// loop over the response elements and add them to the store
for (const message in messages) {

Expand All @@ -143,10 +143,10 @@ const actions = {
message: messages[message],
})
}
return hasMore
return { hasMoreItems, messages }
} catch (error) {
console.error(error)
return false
return { hasMoreItems: false, messages: [] }
}
},

Expand Down

0 comments on commit 14dd511

Please sign in to comment.