From ad16675edc144896ec24388552baf51434b02bc1 Mon Sep 17 00:00:00 2001
From: henryanandsr <13521004@std.stei.itb.ac.id>
Date: Tue, 14 Nov 2023 00:52:16 +0700
Subject: [PATCH] feat : add file upload into drive

---
 app/views/assignments/index.php  |  4 +++-
 app/views/assignments/submit.php |  1 +
 public/js/assignments.js         |  4 +++-
 public/js/submit.js              | 37 +++++++++++++++++---------------
 4 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/app/views/assignments/index.php b/app/views/assignments/index.php
index cc9eb0e..34219a4 100644
--- a/app/views/assignments/index.php
+++ b/app/views/assignments/index.php
@@ -9,6 +9,8 @@
 </div>
 
 <script>
-    const sid = <?php echo $_GET['sid'];?>
+    const sid = <?php echo json_encode($_GET['sid']); ?>;
+    const uid = <?php echo json_encode($data['user_id']); ?>;
 </script>
+
 <script src="/public/js/assignments.js"></script>
\ No newline at end of file
diff --git a/app/views/assignments/submit.php b/app/views/assignments/submit.php
index decc23c..24de200 100644
--- a/app/views/assignments/submit.php
+++ b/app/views/assignments/submit.php
@@ -18,5 +18,6 @@
 <script>
     var sid = <?php echo $_GET['sid'];?>;
     var aid = <?php echo $_GET['aid'];?>;
+    const uid = <?php echo json_encode($data['user_id']); ?>;
 </script>
 <script src="/public/js/submit.js"></script>
\ No newline at end of file
diff --git a/public/js/assignments.js b/public/js/assignments.js
index 66e80af..4a7c1d1 100644
--- a/public/js/assignments.js
+++ b/public/js/assignments.js
@@ -7,7 +7,9 @@ function renderAssignments(){
 
     const xhr = new XMLHttpRequest();
     xhr.open("GET", `http://localhost:5001/api/assignment/${sid}`)
-
+    xhr.setRequestHeader("user_id", uid);
+    console.log(uid)
+    console.log("ASSIGNMENT JS")
     xhr.onload = () => {
         const res = JSON.parse(xhr.response);
 
diff --git a/public/js/submit.js b/public/js/submit.js
index ab0b85c..d0071ea 100644
--- a/public/js/submit.js
+++ b/public/js/submit.js
@@ -1,15 +1,15 @@
 const render = () => {
     const xhr = new XMLHttpRequest();
     xhr.open("GET", `http://localhost:5001/api/assignment/${sid}/${aid}`)
-
+    xhr.setRequestHeader("user_id", uid);
     xhr.onload = () => {
         const res = JSON.parse(xhr.response);
         console.log(res.data);
-
+        console.log(res.data.assignments[0])
         if (res.status === 'success') {
 
-            title.innerHTML = res.data.assignment_name
-            description.innerHTML = res.data.assignment_description
+            title.innerHTML = res.data.assignments[0].assignment_name
+            description.innerHTML = res.data.assignments[0].assignment_description
 
         }
     }
@@ -18,29 +18,32 @@ const render = () => {
 }
 const title = document.getElementById("assignment_name");
 const description = document.getElementById("description");
-
 const submitForm = () => {
     const xhr = new XMLHttpRequest();
-    xhr.open("POST", `http://localhost:5001/api/files/scholarship/${sid}/assignment/${aid}`)
+    const formData = new FormData();
+
+    // Get the file input element
+    const fileInput = document.getElementById('file');
+
+    // Append data to the FormData object
+    formData.append("uid", uid);
+    formData.append("file", fileInput.files[0]); // Assuming fileInput is your file input element
+
+    xhr.open("POST", `http://localhost:5001/api/files/scholarship/${sid}/assignment/${aid}`);
 
     xhr.onload = () => {
         const res = JSON.parse(xhr.response);
-        console.log(res.data);
 
         if (res.status === 'success') {
-
-            title.innerHTML = res.data.assignment_name
-            description.innerHTML = res.data.assignment_description
-
+            // Update the HTML elements with the response data
+            document.getElementById('assignment_name').innerHTML = res.data.assignment_name;
+            document.getElementById('description').innerHTML = res.data.assignment_description;
         }
     }
 
-    const body = {
-        "uid": uid,
-        "file_path": file_path
-    }
-
-    xhr.send(body)
+    xhr.send(formData);
 }
 
+
+
 document.addEventListener("DOMContentLoaded", render);
\ No newline at end of file
-- 
GitLab