From c358fba1ee2aa54a97373d07e7b218c74dfd9bf0 Mon Sep 17 00:00:00 2001
From: alyssaf16 <alyssa_feinberg@brown.edu>
Date: Tue, 12 Nov 2024 01:01:23 -0500
Subject: flashcards w assistant finally workgit add -A

---
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    | 89 +++++++++++++++++-----
 1 file changed, 71 insertions(+), 18 deletions(-)

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index 68d4383e7..95f3fbc5d 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -463,9 +463,11 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                 doc = DocCast(Docs.Create.TextDocument(data, options));
                 break;
             case 'flashcard':
-                // doc = this.createSingleFlashcard(data, options);
                 doc = this.createFlashcard(data, options);
                 break;
+            case 'deck':
+                doc = this.createDeck(data, options);
+                break;
             case 'image':
                 doc = DocCast(Docs.Create.ImageDocument(data, options));
                 break;
@@ -551,31 +553,82 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
         await DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
     };
 
-    // TODO: DELEGATE TO DIFFERENT CLASS
     @action
-    createFlashcard = (data: string, options: DocumentOptions) => {
+    createDeck = (data: any, options: DocumentOptions) => {
         const flashcardDeck: Doc[] = [];
-        const parsedItems: { [key: string]: string } = JSON.parse(data);
-        Object.entries(parsedItems).forEach(([key, val]) => {
-            console.log('key' + key);
-            console.log('key' + val);
-
-            const side1 = Docs.Create.CenteredTextCreator('question', key, options);
-            const side2 = Docs.Create.CenteredTextCreator('answer', val, options);
-            const doc = DocCast(Docs.Create.FlashcardDocument(data, side1, side2, { _width: 300, _height: 300 }));
-            this._props.addDocument?.(doc);
-            flashcardDeck.push(doc);
+
+        // Parse `data` only if it’s a string
+        const deckData = typeof data === 'string' ? JSON.parse(data) : data;
+        console.log('Parsed Deck Data:', deckData);
+        const flashcardArray = Array.isArray(deckData) ? deckData : Object.values(deckData);
+        console.log(typeof flashcardArray);
+        // Process each flashcard document in the `deckData` array
+        flashcardArray.forEach(doc => {
+            const flashcardDoc = this.createFlashcard(doc, options);
+            if (flashcardDoc) flashcardDeck.push(flashcardDoc);
         });
-        const col = DocCast(
+
+        // Create a carousel to contain the flashcard deck
+        const carouselDoc = DocCast(
             Docs.Create.CarouselDocument(flashcardDeck, {
-                title: options.title,
-                _width: 300,
-                _height: 300,
+                title: options.title || 'Flashcard Deck',
+                _width: options._width || 300,
+                _height: options._height || 300,
                 _layout_fitWidth: false,
                 _layout_autoHeight: true,
             })
         );
-        return col;
+
+        return carouselDoc;
+    };
+    @action
+    createFlashcard = (data: any, options: any) => {
+        // const flashcardDeck: Doc[] = [];
+
+        // Process each flashcard item in the data array
+        // const p = JSON.parse(data);
+        const deckData = typeof data === 'string' ? JSON.parse(data) : data;
+        const flashcardArray = Array.isArray(deckData) ? deckData : Object.values(deckData)[2];
+        console.log(typeof flashcardArray);
+
+        const [front, back] = flashcardArray;
+
+        // Check that both front and back are text documents
+        console.log('DATA' + data);
+        console.log('front' + front);
+        console.log('back' + back);
+        console.log(front.doc_type);
+        console.log(back.doc_type);
+        if (front.doc_type === 'text' && back.doc_type === 'text') {
+            const sideOptions: DocumentOptions = {
+                backgroundColor: options.backgroundColor,
+                _width: options._width,
+                _height: options._height,
+            };
+
+            // Create front and back text documents
+            const side1 = Docs.Create.CenteredTextCreator(front.title, front.data, sideOptions);
+            const side2 = Docs.Create.CenteredTextCreator(back.title, back.data, sideOptions);
+
+            // Create the flashcard document with both sides
+            const flashcardDoc = DocCast(Docs.Create.FlashcardDocument(data.title, side1, side2, sideOptions));
+            return flashcardDoc;
+            // this._props.addDocument?.(flashcardDoc);
+            // flashcardDeck.push(flashcardDoc);
+        }
+
+        // Create a carousel to contain the flashcard deck
+        // const carouselDoc = DocCast(
+        //     Docs.Create.CarouselDocument(flashcardDeck, {
+        //         title: options.title || data.title,
+        //         _width: data.width || 300,
+        //         _height: data.height || 300,
+        //         _layout_fitWidth: false,
+        //         _layout_autoHeight: true,
+        //     })
+        // );
+
+        // return carouselDoc;
     };
 
     /**
-- 
cgit v1.2.3-70-g09d2

