Browse Source

create folder

juan 1 month ago
parent
commit
55de9ea8b9
2 changed files with 74 additions and 2 deletions
  1. 23 2
      lib/api_service.dart
  2. 51 0
      lib/folder_drawer.dart

+ 23 - 2
lib/api_service.dart

@@ -9,9 +9,7 @@ import 'dart:ui_web' as ui;
 import 'augment.dart';
 import 'dart:html' as html;
 
-
 class ApiService {
-
   Future<List<GetThreadResponse>> fetchEmailsFromFolder(
       String folder, int pagenitaion) async {
     try {
@@ -106,6 +104,29 @@ class ApiService {
       return [];
     }
   }
+
+  Future<void> createFolder(String folderName) async {
+    var url = Uri.http('127.0.0.1:3001', 'create_folder');
+
+    Map<String, String> requestBody = {'name': folderName};
+
+    try {
+      var response = await http.post(
+        url,
+        headers: {
+          'Content-Type': 'application/json',
+        },
+        body: jsonEncode(requestBody),
+      );
+      if (response.statusCode == 200) {
+        print('response body: ${response.body}');
+      } else {
+        print('Error: ${response.statusCode}, response body: ${response.body}');
+      }
+    } catch (e) {
+      print('error making post req: $e');
+    }
+  }
 }
 
 class EmailView extends StatefulWidget {

+ 51 - 0
lib/folder_drawer.dart

@@ -1,6 +1,7 @@
 //drawer with the folders for emails a.k.a mailboxes
 
 import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
 import 'api_service.dart';
 
 class FolderDrawer extends StatefulWidget {
@@ -50,6 +51,19 @@ class _FolderDrawerState extends State<FolderDrawer> {
               },
             );
           }).toList(),
+          ListTile(
+            leading: Icon(Icons.add),
+            title: Text("New Mailbox"),
+            onTap: () {
+              showDialog(
+                context: context,
+                builder: (BuildContext context) {
+                  return NewMailbox(apiService:  widget.apiService);
+                },
+              );
+              // Navigator.of(context).pop();
+            },
+          ),
           ListTile(
             leading: Icon(Icons.refresh),
             title: Text('Refresh Folders'),
@@ -63,3 +77,40 @@ class _FolderDrawerState extends State<FolderDrawer> {
     );
   }
 }
+
+class NewMailbox extends StatelessWidget {
+  final ApiService apiService;
+  // final Function(String) onFolderCreated;
+  final TextEditingController _textFieldController = TextEditingController();
+
+  NewMailbox({required this.apiService});
+
+  @override
+  Widget build(BuildContext context) {
+    return AlertDialog(
+      title: const Text("Create a new Mailbox"),
+      content: TextField(
+        controller: _textFieldController,
+        decoration: const InputDecoration(
+          hintText: "EPIC FOLDER", // Your custom hint text here
+        ),
+      ),
+      actions: <Widget>[
+        TextButton(
+          onPressed: () {
+            print("Folder name: ${_textFieldController.text}");
+            String folderName = _textFieldController.text;
+
+            if (folderName.isNotEmpty) {
+              apiService.createFolder(folderName);
+              // onFolderCreated(folderName);
+            }
+            // apiService.createFolder(_textFieldController.text);
+            Navigator.of(context).pop();
+          },
+          child: const Text("Approve"),
+        ),
+      ],
+    );
+  }
+}