From 03f9fd4beb2bd3efa8f88c71bdbaf52dbec82e66 Mon Sep 17 00:00:00 2001
From: Nathan-SR <144961007+Nathan-SR@users.noreply.github.com>
Date: Mon, 10 Jun 2024 14:56:42 -0400
Subject: sub-collection doc adding refactor complete except for small bug

---
 .../collectionSchema/CollectionSchemaView.tsx      | 51 ++++++++++------------
 1 file changed, 24 insertions(+), 27 deletions(-)

(limited to 'src/client/views/collections/collectionSchema/CollectionSchemaView.tsx')

diff --git a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
index 48287c3ec..1952f59f5 100644
--- a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
+++ b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
@@ -65,7 +65,6 @@ export class CollectionSchemaView extends CollectionSubView() {
     constructor(props: any) {
         super(props);
         makeObservable(this);
-        this.importAddedDocs();
     }
 
     static _rowHeight: number = 50;
@@ -97,7 +96,6 @@ export class CollectionSchemaView extends CollectionSubView() {
     @observable _colBeingDragged: boolean = false;
     @observable _colKeysFiltered: boolean = false;
     @observable _cellTags: ObservableMap = new ObservableMap<Doc, Array<string>>();
-    @observable _lentDocs: Doc[] = [];
     @observable _docs: Doc[] = this.childDocs;
 
     // target HTMLelement portal for showing a popup menu to edit cell values.
@@ -150,7 +148,7 @@ export class CollectionSchemaView extends CollectionSubView() {
     }
 
     @computed get rowHeights() {
-        return this.childDocs.concat(this._lentDocs).map(() => this.rowHeightFunc());
+        return this.docs.map(() => this.rowHeightFunc());
     }
 
     @computed get displayColumnWidths() {
@@ -198,14 +196,6 @@ export class CollectionSchemaView extends CollectionSubView() {
     // ViewBoxInterface overrides
     override isUnstyledView = returnTrue; // used by style provider : turns off  opacity, animation effects, scaling
 
-    importAddedDocs = () => {
-        const collections = this.childDocs.filter((doc: Doc) => doc.type === 'collection');
-        collections.forEach((doc: Doc) => {
-            const childDocs = DocListCast(doc[Doc.LayoutFieldKey(doc)]);
-            doc._childrenSharedWithSchema && this.addDocsFromOtherCollection(childDocs);
-        });
-    }
-
     removeDoc = (doc: Doc) => {
         this.removeDocument(doc);
         this._docs = this._docs.filter(d => d !== doc)
@@ -278,7 +268,6 @@ export class CollectionSchemaView extends CollectionSubView() {
                     break;
                 }
                 case 'P': {
-                    this.importAddedDocs();
                     break;
                 }
                 default:
@@ -651,16 +640,6 @@ export class CollectionSchemaView extends CollectionSubView() {
         return undefined;
     };
 
-    addDocsFromOtherCollection = (docs: Doc[]) => {
-        docs.forEach((doc: Doc) => !this.displayedDocsFunc().includes(doc) && this._lentDocs.push(doc));
-        this._docs = this.childDocs.slice().concat(this._lentDocs);
-    }
-
-    removeDocsFromOtherCollection = (docs: Doc[]) => {
-        this._lentDocs = this._lentDocs.filter((doc: Doc) => !docs.includes(doc));
-        this._docs = this.childDocs.slice().concat(this._lentDocs);
-    }
-
     @computed get fieldDefaultInput() {
         switch (this._newFieldType) {
             case ColumnType.Number:
@@ -739,7 +718,7 @@ export class CollectionSchemaView extends CollectionSubView() {
     };
 
     setCellValues = (key: string, value: string) => {
-        if (this._selectedCells.length === 1) this.childDocs.forEach(doc => !doc._lockedSchemaEditing && Doc.SetField(doc, key, value)); // if only one cell selected, fill all
+        if (this._selectedCells.length === 1) this.childDocs.forEach(doc => !doc._lockedSchemaEditing &&Doc.SetField(doc, key, value)); // if only one cell selected, fill all
         else this._selectedCells.forEach(doc => !doc._lockedSchemaEditing && Doc.SetField(doc, key, value)); // else only fill selected cells
         return true;
     };
@@ -1038,16 +1017,36 @@ export class CollectionSchemaView extends CollectionSubView() {
         }
     };
 
+    displayedSubCollectionDocs = (doc: Doc) => {
+        const childDocs = DocListCast(doc[Doc.LayoutFieldKey(doc)]);
+        const displayedCollections = childDocs.filter(d => d.type === 'collection' && d._childrenSharedWithSchema);
+        let toReturn: Doc[] = [...childDocs];
+        displayedCollections.forEach(d => toReturn = toReturn.concat(this.displayedSubCollectionDocs(d)));
+        return toReturn;
+    }
+    
+    @computed get docs() {
+        let docsFromChildren: Doc[] = [];
+        const displayedCollections = this.childDocs.filter(d => d.type === 'collection' && d._childrenSharedWithSchema);
+        displayedCollections.forEach(d => {
+            let docsNotAlreadyDisplayed = this.displayedSubCollectionDocs(d).filter(dc => !this._docs.includes(dc));
+            docsFromChildren = docsFromChildren.concat(docsNotAlreadyDisplayed);
+        });
+        let docs = this._docs.concat(docsFromChildren);
+        return docs;
+    }
+    
     @computed get displayedDocs() {
         const draggedDocs = this.isContentActive() ? DragManager.docsBeingDragged : [];
-        const docs = this._docs.filter(d => !draggedDocs.includes(d));
+        let docs = [...this.docs];
+        docs = docs.filter(d => !draggedDocs.includes(d));
         docs.splice(this.rowDropIndex, 0, ...draggedDocs);
         return { docs };
     }
 
     @action 
     sortDocs = (docs: Doc[], field: string, desc: boolean) => {
-        docs = docs.sort((docA, docB) => {
+        this._docs = docs.sort((docA, docB) => {
             // this sorts the documents based on the selected field.  returning -1 for a before b, 0 for a = b, 1 for a > b
             const aStr = Field.toString(docA[field] as FieldType);
             const bStr = Field.toString(docB[field] as FieldType);
@@ -1115,8 +1114,6 @@ export class CollectionSchemaView extends CollectionSubView() {
                                 columnIndex={index}
                                 columnKeys={this.columnKeys}
                                 columnWidths={this.displayColumnWidths}
-                                sortField={this.sortField}
-                                sortDesc={this.sortDesc}
                                 setSort={this.setColumnSort}
                                 rowHeight={this.rowHeightFunc}
                                 removeColumn={this.removeColumn}
-- 
cgit v1.2.3-70-g09d2

