From c789df5ae7a9e364f0d95b54f4a2f330b536a393 Mon Sep 17 00:00:00 2001
From: Sam Wilkins <samuel_wilkins@brown.edu>
Date: Tue, 11 Jun 2019 13:29:48 -0400
Subject: some inline documentation and new template skeletons

---
 src/client/views/collections/CollectionBaseView.tsx | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

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

diff --git a/src/client/views/collections/CollectionBaseView.tsx b/src/client/views/collections/CollectionBaseView.tsx
index 734669893..a3019f23e 100644
--- a/src/client/views/collections/CollectionBaseView.tsx
+++ b/src/client/views/collections/CollectionBaseView.tsx
@@ -106,14 +106,19 @@ export class CollectionBaseView extends React.Component<CollectionViewProps> {
         }
         if (!this.createsCycle(doc, props.Document)) {
             //TODO This won't create the field if it doesn't already exist
-            const value = Cast(props.Document[props.fieldKey], listSpec(Doc));
+            const childDocs = DocListCast(props.Document[props.fieldKey]);
             let alreadyAdded = true;
-            if (value !== undefined) {
-                if (allowDuplicates || !value.some(v => v instanceof Doc && v[Id] === doc[Id])) {
+            if (childDocs !== undefined) {
+                // if this is not the first document added to the collection
+                if (allowDuplicates || !childDocs.some(v => v instanceof Doc && v[Id] === doc[Id])) {
                     alreadyAdded = false;
-                    value.push(doc);
+                    childDocs.push(doc);
                 }
+                // if we're here, we've tried to add a duplicate
             } else {
+                // if we are the first, set up a new list for this and all
+                // future child documents stored in the associated collection document at the fieldKey (likely .data)
+                // passed in via props
                 alreadyAdded = false;
                 Doc.SetOnPrototype(this.props.Document, this.props.fieldKey, new List([doc]));
             }
-- 
cgit v1.2.3-70-g09d2

