From 0c23e63bec5bfe59f7371a741d9e72ee843eb486 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Wed, 3 Feb 2021 13:32:54 +0530 Subject: [PATCH] corrected to have only unique instances of collection in localDB --- src/services/collectionService.ts | 21 ++++++++++++++++----- src/services/fileService.ts | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index e4a648040..e53be289a 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -97,7 +97,7 @@ const getCollections = async ( return await Promise.all(promises); } catch (e) { - console.log("getCollections falied- " + e); + console.log("getCollections failed- " + e); } }; @@ -107,13 +107,24 @@ export const getLocalCollections = async (): Promise => { } export const fetchUpdatedCollections = async (token: string, key: string) => { const collectionUpdateTime = await localForage.getItem('collection-update-time') as string; - const updatedCollections = await getCollections(token, collectionUpdateTime ?? '0', key); + const updatedCollections = await getCollections(token, collectionUpdateTime ?? '0', key) || []; const favCollection = await localForage.getItem('fav-collection') as collection[] ?? updatedCollections.filter(collection => collection.type === CollectionType.favorites); const localCollections = await getLocalCollections(); - + const allCollectionsInstances = [...localCollections, ...updatedCollections]; + var latestCollectionsInstances = new Map(); + allCollectionsInstances.forEach((collection) => { + if (!latestCollectionsInstances.has(collection.id) || latestCollectionsInstances.get(collection.id).updationTime < collection.updationTime) { + latestCollectionsInstances.set(collection.id, collection); + } + }); + let collections = [], updationTime = 0; + for (const [_, collection] of latestCollectionsInstances) { + collections.push(collection); + updationTime = Math.max(updationTime, collection.updationTime); + } await localForage.setItem('fav-collection', favCollection); - await localForage.setItem('collection-update-time', Date.now() * 1000); - await localForage.setItem('collections', [...localCollections, ...updatedCollections]); + await localForage.setItem('collection-update-time', updationTime); + await localForage.setItem('collections', collections); return updatedCollections; }; diff --git a/src/services/fileService.ts b/src/services/fileService.ts index d8412a59c..59da44b61 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -87,7 +87,7 @@ export const fetchFiles = async ( } }); files = []; - for (const [_, file] of latestFiles.entries()) { + for (const [_, file] of latestFiles) { if (!file.isDeleted) files.push(file); }