From 1664b369522c4f06add4ec0c9895dbae36d0d16a Mon Sep 17 00:00:00 2001
From: Jose Manuel Serrano Amaut <a20122128@pucp.pe>
Date: Sun, 5 Mar 2023 23:24:37 -0500
Subject: [PATCH] [REF]: Imporove support for fake upload progress. Also
 improved ExtFileObject response to avoid copying undefined attributes from
 ExtFileInstance

---
 src/files-ui/core/types/ExtFile.ts | 100 +++++++----------------------
 1 file changed, 24 insertions(+), 76 deletions(-)

diff --git a/src/files-ui/core/types/ExtFile.ts b/src/files-ui/core/types/ExtFile.ts
index 1a69b3a..2e840ac 100644
--- a/src/files-ui/core/types/ExtFile.ts
+++ b/src/files-ui/core/types/ExtFile.ts
@@ -200,85 +200,33 @@ export class ExtFileInstance {
 
     }
 
+    /**
+     * Copies all non undefined attributes from ExtFileInstance to a new ExtFile object
+     * @param extFileInstance the instance of ExtFile
+     * @returns an ExtFile object
+     */
     static toExtFile(extFileInstance: ExtFileInstance): ExtFile {
-        const
-            {
-                id,
-                file,
-                name,
-                size,
-                type,
-                imageUrl,
-                valid,
-                errors,
-                uploadMessage,
-                uploadStatus,
-                progress,
-                xhr,
-                extraData,
-                extraUploadData,
-                serverResponse,
-                downloadUrl
-            } = extFileInstance;
-        return {
-            id,
-            file,
-            name,
-            size,
-            type,
-            imageUrl,
-            valid,
-            errors,
-            uploadMessage,
-            uploadStatus,
-            progress,
-            xhr,
-            extraData,
-            extraUploadData,
-            serverResponse,
-            downloadUrl
-        };
+        console.log("before toExtFile()", extFileInstance);
+
+        let extFileClone: ExtFile = {}; // the new empty object
+        const extFileInstanceKeys = Object.keys(extFileInstance) as [keyof ExtFile];
+        const extFileInstanceValues = Object.values(extFileInstance);
+        // let's copy all user properties into it
+        for (let i = 0; i < extFileInstanceKeys.length; i++) {
+            if (extFileInstanceValues[i] !== undefined) {
+                extFileClone[extFileInstanceKeys[i]] = extFileInstanceValues[i];
+            }
+        }
+        console.log("after toExtFile()", extFileClone);
+
+        return extFileClone;
     }
+    /**
+     * Copies all non undefined attributes from ExtFileInstance to a new ExtFile object.
+     * @returns an ExtFile object
+     */
     toExtFile(): ExtFile {
-        const {
-            id,
-            file,
-            name,
-            size,
-            type,
-            imageUrl,
-            valid,
-            errors,
-            uploadMessage,
-            uploadStatus,
-            progress,
-            xhr,
-            extraData,
-            extraUploadData,
-            serverResponse,
-            downloadUrl
-        } = this;
-
-        const result: ExtFile = {
-            id,
-            file,
-            name,
-            size,
-            type,
-            imageUrl,
-            valid,
-            errors,
-            uploadMessage,
-            uploadStatus,
-            progress,
-            xhr,
-            extraData,
-            extraUploadData,
-            serverResponse,
-            downloadUrl
-        } as ExtFile;
-        console.log("incomming result", { ...result });
-        return { ...result };
+        return ExtFileInstance.toExtFile(this);
     }
 
     static mock = (): ExtFileInstance => {
-- 
GitLab