From 6f06781103a55417a9ccd71ca91b10d6c1d0a746 Mon Sep 17 00:00:00 2001
From: afnanramadhan <13521011@std.stei.itb.ac.id>
Date: Thu, 16 Nov 2023 20:55:35 +0700
Subject: [PATCH] feat: bisa subcribe unsubs dari soap

---
 public/js/detailfact.js | 80 +++++++++++++++++++++++++++++------------
 1 file changed, 57 insertions(+), 23 deletions(-)

diff --git a/public/js/detailfact.js b/public/js/detailfact.js
index d3fd0c2..d760379 100644
--- a/public/js/detailfact.js
+++ b/public/js/detailfact.js
@@ -32,7 +32,7 @@ const loadpage = (id) => {
                     <h2 id="author">${content.author}</h2>
                     <h4 id="totSubs">${content.total_subscriber} Subscribers</h4>
                 </div>
-                <button type="button" class=${subs} id="subscribe" onclick=subscribe(${id},${uuid})>${subs}</button>
+                <button type="button" class=${subs} id="subscribe" onclick=subscribe(${id},'${uuid}')>${subs}</button>
             </div>
             <div class="factcontainer">
                 <img src="${content.path_photo}" alt="" id="imageContent">
@@ -49,32 +49,66 @@ const loadpage = (id) => {
 
 }
 
-const subscribe = (id,uuid) => {
+const subscribe = (idContent,uuid) => {
     console.log(uuid);
-    console.log(id);
+    console.log(idContent);
     const subscribeButton = document.getElementById('subscribe');
+
+    let idUser = "";
+    const cookie = getCookie('user');
+    if (cookie !== null) {
+        try {
+            const jsonData = JSON.parse(cookie);
+            idUser = jsonData[0].id;
+            console.log('ID Value:', idUser);
+        } catch (error) {
+            console.error('Error parsing JSON:', error);
+        }
+    } else {
+        console.log('Cookie not found');
+    }
+    
+
+
     if(subscribeButton.innerHTML == "Subscribe"){
-        subscribeButton.innerHTML = "Unsubscribe";
-        // xhttp = new XMLHttpRequest();
-        // xhttp.onreadystatechange = function() {
-        //     if (this.readyState === 4){
-        //         let response = this.response;
-        //         console.log(response);
-        //     }
-        // };   
-        // xhttp.open('GET', `../../server/controller/auth/DetailFact.php?subscribe=1&id=${id}&uuid=${uuid}`, true);
-        // xhttp.send();
+        subscribeButton.innerHTML = "Unsubscribe";  
+        console.log("unsub");
+        xhttp = new XMLHttpRequest();
+        xhttp.onreadystatechange = function() {
+            if (this.readyState === 4){
+                let response = this.response;
+                console.log(response);
+            }
+        };   
+        xhttp.open('GET', `../../server/controller/auth/DetailFact.php?subscribe=1&id=${idUser}&uuid=${uuid}`, true);
+        xhttp.send();
     }else{
         subscribeButton.innerHTML = "Subscribe";
-        // xhttp = new XMLHttpRequest();
-        // xhttp.onreadystatechange = function() {
-        //     if (this.readyState === 4){
-        //         let response = this.response;
-        //         console.log(response);
-        //     }
-        // };   
-        // xhttp.open('GET', `../../server/controller/auth/DetailFact.php?subscribe=0&id=${id}&uuid=${uuid}`, true);
-        // xhttp.send();
+        console.log("sub");
+        xhttp = new XMLHttpRequest();
+        xhttp.onreadystatechange = function() {
+            if (this.readyState === 4){
+                let response = this.response;
+                console.log(response);
+            }
+        };   
+        xhttp.open('GET', `../../server/controller/auth/DetailFact.php?subscribe=0&id=${idUser}&uuid=${uuid}`, true);
+        xhttp.send();
     }
-    // loadpage(id);
+    loadpage(idContent);
+}
+
+function getCookie(cookieName) {
+    const name = cookieName + "=";
+    const decodedCookie = decodeURIComponent(document.cookie);
+    const cookieArray = decodedCookie.split(';');
+
+    for (let i = 0; i < cookieArray.length; i++) {
+        let cookie = cookieArray[i].trim();
+        if (cookie.startsWith(name)) {
+            return cookie.substring(name.length);
+        }
+    }
+
+    return null;
 }
\ No newline at end of file
-- 
GitLab