فهرست منبع

Merge branch 'collection-ops' into file-loading-single-update

Abhinav-grd 4 سال پیش
والد
کامیت
053568980e
2فایلهای تغییر یافته به همراه24 افزوده شده و 20 حذف شده
  1. 6 6
      src/pages/gallery/index.tsx
  2. 18 14
      src/services/collectionService.ts

+ 6 - 6
src/pages/gallery/index.tsx

@@ -129,20 +129,20 @@ export default function Gallery(props) {
             setCollections(collections);
             setCollections(collections);
             setLoading(false);
             setLoading(false);
             setProgress(80);
             setProgress(80);
-            await loadData();
+            await syncWithRemote();
             setProgress(100);
             setProgress(100);
         };
         };
         main();
         main();
         props.setUploadButtonView(true);
         props.setUploadButtonView(true);
     }, []);
     }, []);
 
 
-    const loadData = async () => {
+    const syncWithRemote = async () => {
         const token = getToken();
         const token = getToken();
         const encryptionKey = await getActualKey();
         const encryptionKey = await getActualKey();
         const updatedCollections = await fetchUpdatedCollections(token, encryptionKey);
         const updatedCollections = await fetchUpdatedCollections(token, encryptionKey);
         const data = await fetchData(token, updatedCollections);
         const data = await fetchData(token, updatedCollections);
         const collections = await getLocalCollections();
         const collections = await getLocalCollections();
-        const collectionLatestFile = await getCollectionLatestFile(collections, token);
+        const collectionLatestFile = await getCollectionLatestFile(collections, data);
         const favItemIds = await getFavItemIds(data);
         const favItemIds = await getFavItemIds(data);
         if (updatedCollections.length > 0) {
         if (updatedCollections.length > 0) {
             setCollections(collections);
             setCollections(collections);
@@ -150,7 +150,7 @@ export default function Gallery(props) {
         }
         }
         setCollectionLatestFile(collectionLatestFile);
         setCollectionLatestFile(collectionLatestFile);
         setFavItemIds(favItemIds);
         setFavItemIds(favItemIds);
-       
+
         props.setUploadButtonView(true);
         props.setUploadButtonView(true);
     }
     }
     if (!data || loading) {
     if (!data || loading) {
@@ -206,7 +206,7 @@ export default function Gallery(props) {
 
 
     const handleClose = () => {
     const handleClose = () => {
         setOpen(false);
         setOpen(false);
-        // setReload(Math.random());
+        // syncWithRemote();
     };
     };
 
 
     const onThumbnailClick = (index: number) => () => {
     const onThumbnailClick = (index: number) => () => {
@@ -322,7 +322,7 @@ export default function Gallery(props) {
                 closeUploadModal={props.closeUploadModal}
                 closeUploadModal={props.closeUploadModal}
                 showUploadModal={props.showUploadModal}
                 showUploadModal={props.showUploadModal}
                 collectionLatestFile={collectionLatestFile}
                 collectionLatestFile={collectionLatestFile}
-                refetchData={loadData}
+                refetchData={syncWithRemote}
 
 
             />
             />
             {filteredData.length ? (
             {filteredData.length ? (

+ 18 - 14
src/services/collectionService.ts

@@ -128,21 +128,25 @@ export const fetchUpdatedCollections = async (token: string, key: string) => {
     return updatedCollections;
     return updatedCollections;
 };
 };
 
 
-export const getCollectionLatestFile = async (
+export const getCollectionLatestFile = (
     collections: collection[],
     collections: collection[],
-    token
-): Promise<collectionLatestFile[]> => {
-    return Promise.all(
-        collections.map(async collection => {
-            const sinceTime: string = (Number(await localForage.getItem<string>(`${collection.id}-time`)) - 1).toString();
-            const files: file[] = await getFiles([collection], sinceTime, "1", token) || [];
-
-            return {
-                file: files[0],
-                collection,
-            }
-        }))
-};
+    files: file[]
+): collectionLatestFile[] => {
+    const latestFile = new Map<number, file>();
+    const collectionMap = new Map<number, collection>();
+
+    collections.forEach(collection => collectionMap.set(Number(collection.id), collection));
+    files.forEach(file => {
+        if (!latestFile.has(file.collectionID)) {
+            latestFile.set(file.collectionID, file)
+        }
+    });
+    let allCollectionLatestFile: collectionLatestFile[] = [];
+    for (const [collectionID, file] of latestFile) {
+        allCollectionLatestFile.push({ collection: collectionMap.get(collectionID), file });
+    }
+    return allCollectionLatestFile;
+}
 
 
 export const getFavItemIds = async (files: file[]): Promise<Set<number>> => {
 export const getFavItemIds = async (files: file[]): Promise<Set<number>> => {