add dedupe logic before saving collection to localStorage

This commit is contained in:
Abhinav 2022-01-28 17:34:06 +05:30
parent 3291b2c005
commit 7649ee1894

View file

@ -18,7 +18,7 @@ const ENDPOINT = getEndpoint();
const PUBLIC_COLLECTION_FILES_TABLE = 'public-collection-files'; const PUBLIC_COLLECTION_FILES_TABLE = 'public-collection-files';
const PUBLIC_COLLECTIONS_TABLE = 'public-collections'; const PUBLIC_COLLECTIONS_TABLE = 'public-collections';
const getCollectionUID = (collection: Collection) => `${collection.id}`; const getCollectionUID = (collection: Collection) => `${collection.key}`;
export const getLocalPublicFiles = async (collectionUID: string) => { export const getLocalPublicFiles = async (collectionUID: string) => {
const localSavedPublicCollectionFiles = ( const localSavedPublicCollectionFiles = (
@ -39,19 +39,21 @@ export const savePublicCollectionFiles = async (
(await localForage.getItem<LocalSavedPublicCollectionFiles[]>( (await localForage.getItem<LocalSavedPublicCollectionFiles[]>(
PUBLIC_COLLECTION_FILES_TABLE PUBLIC_COLLECTION_FILES_TABLE
)) ?? []; )) ?? [];
await localForage.setItem(PUBLIC_COLLECTION_FILES_TABLE, [ await localForage.setItem(
PUBLIC_COLLECTION_FILES_TABLE,
dedupeCollectionFiles([
...publicCollectionFiles, ...publicCollectionFiles,
{ collectionUID, files }, { collectionUID, files },
]); ])
);
}; };
export const getLocalPublicCollection = async (collectionKey: string) => { export const getLocalPublicCollection = async (collectionKey: string) => {
const localCollections =
(await localForage.getItem<Collection[]>(PUBLIC_COLLECTIONS_TABLE)) ??
[];
const publicCollection = const publicCollection =
( localCollections.find(
(await localForage.getItem<Collection[]>(
PUBLIC_COLLECTIONS_TABLE
)) ?? []
).find(
(localSavedPublicCollection) => (localSavedPublicCollection) =>
localSavedPublicCollection.key === collectionKey localSavedPublicCollection.key === collectionKey
) || null; ) || null;
@ -62,10 +64,36 @@ export const savePublicCollection = async (collection: Collection) => {
const publicCollections = const publicCollections =
(await localForage.getItem<Collection[]>(PUBLIC_COLLECTIONS_TABLE)) ?? (await localForage.getItem<Collection[]>(PUBLIC_COLLECTIONS_TABLE)) ??
[]; [];
await localForage.setItem(PUBLIC_COLLECTIONS_TABLE, [ await localForage.setItem(
...publicCollections, PUBLIC_COLLECTIONS_TABLE,
collection, dedupeCollections([...publicCollections, collection])
]); );
};
const dedupeCollections = (collections: Collection[]) => {
const keySet = new Set([]);
return collections.filter((collection) => {
if (!keySet.has(collection.key)) {
keySet.add(collection.key);
return true;
} else {
return false;
}
});
};
const dedupeCollectionFiles = (
collectionFiles: LocalSavedPublicCollectionFiles[]
) => {
const keySet = new Set([]);
return collectionFiles.filter(({ collectionUID }) => {
if (!keySet.has(collectionUID)) {
keySet.add(collectionUID);
return true;
} else {
return false;
}
});
}; };
const getPublicCollectionLastSyncTime = async (collectionUID: string) => const getPublicCollectionLastSyncTime = async (collectionUID: string) =>