From 3e5b5d732ab2314ebea37f48d5f719c03a371565 Mon Sep 17 00:00:00 2001
From: bewe <93899302+bernarduswillson@users.noreply.github.com>
Date: Fri, 17 Nov 2023 11:19:22 +0700
Subject: [PATCH] feat: toast

---
 api/exercise/submit.php          |  4 ++--
 api/merch/buy.php                |  2 +-
 api/merch/redeem.php             |  6 +++++-
 app/controllers/Exercise.php     | 14 +++++++-------
 app/views/confirmation/index.php |  2 +-
 app/views/merchandise/index.php  | 27 ++++++++++++++++++++++++---
 app/views/question/index.php     | 23 +++++++++++++++++++++--
 public/js/modalIter.js           | 14 ++++++++++++++
 8 files changed, 75 insertions(+), 17 deletions(-)
 create mode 100644 public/js/modalIter.js

diff --git a/api/exercise/submit.php b/api/exercise/submit.php
index a4f34b9..deaabed 100644
--- a/api/exercise/submit.php
+++ b/api/exercise/submit.php
@@ -19,7 +19,7 @@ function submitQuiz($exerciseId, $exerciseName, $selectedOptions, $userId, $isDo
 
     $ch = curl_init();
     // curl_setopt($ch, CURLOPT_URL, "http://express:5000/exercise/result/" . $exerciseId);
-    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.11:5000/exercise/result/" . $exerciseId);
+    curl_setopt($ch, CURLOPT_URL, "http://172.20.10.2:5000/exercise/result/" . $exerciseId);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt(
@@ -61,7 +61,7 @@ function submitQuiz($exerciseId, $exerciseName, $selectedOptions, $userId, $isDo
     // rest add progress
     $ch = curl_init();
     // curl_setopt($ch, CURLOPT_URL, "http://express:5000/progress/create");
-    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.11:5000/progress/create");
+    curl_setopt($ch, CURLOPT_URL, "http://172.20.10.2:5000/progress/create");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt(
diff --git a/api/merch/buy.php b/api/merch/buy.php
index 2005928..911721c 100644
--- a/api/merch/buy.php
+++ b/api/merch/buy.php
@@ -4,7 +4,7 @@ function buyItem($merchId, $userId, $email)
     // rest buy merchandise
     $ch = curl_init();
     // curl_setopt($ch, CURLOPT_URL, "http://express:5000/merch/buy/" . $merchId);
-    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.11:5000/merch/buy/" . $merchId);
+    curl_setopt($ch, CURLOPT_URL, "http://172.20.10.2:5000/merch/buy/" . $merchId);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt(
diff --git a/api/merch/redeem.php b/api/merch/redeem.php
index 039c07f..2b2881f 100644
--- a/api/merch/redeem.php
+++ b/api/merch/redeem.php
@@ -35,10 +35,14 @@ function redeem($voucher, $userId)
 }
 
 
-if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['redeemVoucher'])) {
+if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['redeemVoucher']) && isset($_POST['voucher'])) {
     $userId = $_POST['userId'];
     $voucher = $_POST['voucher'];
 
+    if (empty($voucher)) {
+        header('Location: ../../merchandise');
+    }
+
     $isSuccess = redeem($voucher, $userId);
 
     header('Location: ../../merchandise');
diff --git a/app/controllers/Exercise.php b/app/controllers/Exercise.php
index a842071..ca992ec 100644
--- a/app/controllers/Exercise.php
+++ b/app/controllers/Exercise.php
@@ -12,7 +12,7 @@ class Exercise extends Controller
 
     // get exercises
     // $baseUrl = 'http://express:5000/exercise';
-    $baseUrl = 'http://192.168.0.11:5000/exercise';
+    $baseUrl = 'http://172.20.10.2:5000/exercise';
     $ch = curl_init($baseUrl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $response = curl_exec($ch);
@@ -22,7 +22,7 @@ class Exercise extends Controller
 
     // progress
     // $baseUrl = 'http://express:5000/progress/user/' . $data["user_id"];
-    $baseUrl = 'http://192.168.0.11:5000/progress/user/' . $data["user_id"];
+    $baseUrl = 'http://172.20.10.2:5000/progress/user/' . $data["user_id"];
     $ch = curl_init($baseUrl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $response = curl_exec($ch);
@@ -49,7 +49,7 @@ class Exercise extends Controller
 
       // selected exercise
       // $baseUrl = 'http://express:5000/exercise';
-      $baseUrl = 'http://192.168.0.11:5000/exercise';
+      $baseUrl = 'http://172.20.10.2:5000/exercise';
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -66,7 +66,7 @@ class Exercise extends Controller
 
       // progress
       // $baseUrl = 'http://express:5000/progress/user/' . $data["user_id"];
-      $baseUrl = 'http://192.168.0.11:5000/progress/user/' . $data["user_id"];
+      $baseUrl = 'http://172.20.10.2:5000/progress/user/' . $data["user_id"];
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -83,7 +83,7 @@ class Exercise extends Controller
 
       // questions
       // $baseUrl = 'http://express:5000/question/' . $data["exercise_id"];
-      $baseUrl = 'http://192.168.0.11:5000/question/' . $data["exercise_id"];
+      $baseUrl = 'http://172.20.10.2:5000/question/' . $data["exercise_id"];
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -94,7 +94,7 @@ class Exercise extends Controller
       // options
       foreach ($data["questions"] as &$question) {
         // $baseUrl = 'http://express:5000/option/' . $question["question_id"];
-        $baseUrl = 'http://192.168.0.11:5000/option/' . $question["question_id"];
+        $baseUrl = 'http://172.20.10.2:5000/option/' . $question["question_id"];
         $ch = curl_init($baseUrl);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($ch);
@@ -187,7 +187,7 @@ class Exercise extends Controller
   {
     if (isset($exerciseId) && !empty($exerciseId)) {
       // $baseUrl = 'http://express:5000/exercise/validate/' . $exerciseId;
-      $baseUrl = 'http://192.168.0.11:5000/exercise/validate/' . $exerciseId;
+      $baseUrl = 'http://172.20.10.2:5000/exercise/validate/' . $exerciseId;
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
diff --git a/app/views/confirmation/index.php b/app/views/confirmation/index.php
index a401c7a..902c1d3 100644
--- a/app/views/confirmation/index.php
+++ b/app/views/confirmation/index.php
@@ -11,7 +11,7 @@
         <? endif; ?>
         <div class="conf-items">
             <div class="merch-item"
-                style="background-image: url('http://192.168.0.11:5000/image/?filename=<?= $data['merch']['image'] ?>');">
+                style="background-image: url('http://172.20.10.2:5000/image/?filename=<?= $data['merch']['image'] ?>');">
                 <div class="merch-image"></div>
                 <div class="merch-details">
                     <div class="merch-name">
diff --git a/app/views/merchandise/index.php b/app/views/merchandise/index.php
index 49ae797..519a346 100644
--- a/app/views/merchandise/index.php
+++ b/app/views/merchandise/index.php
@@ -31,7 +31,7 @@
         <div class="merchandise-items">
             <? foreach ($data['merch'] as $merch): ?>
                 <div class="merch-item"
-                    style="background-image: url('http://192.168.0.11:5000/image/?filename=<?= $merch['image'] ?>');">
+                    style="background-image: url('http://172.20.10.2:5000/image/?filename=<?= $merch['image'] ?>');">
                     <div class="merch-image"></div>
                     <div class="merch-details">
                         <div class="merch-name">
@@ -45,10 +45,30 @@
                         </div>
                         <div>
                             <form action="../../../api/merch/buy.php" method="post">
+                                <!-- Modal -->
+                                <div class="confirm-container close-modal-trigger">
+                                    <div class="confirm-card">
+                                        <div class="confirm-content">
+                                            <h2 class="text-md text-red font-reg">Are you sure?</h2>
+                                            <p class="text-sm text-black font-reg">Please re-check your purchase before buying</p>
+                                        </div>
+                                        <div class="modal-button-container">
+                                            <button type="button"
+                                                class="secondary-btn font-reg text-sm close-modal-trigger" data-index="0">
+                                                Cancel
+                                            </button>
+                                            <button class="primary-btn font-reg text-sm" id="logout-btn" name="buyMerch"
+                                                type="submit">
+                                                Buy
+                                            </button>
+                                        </div>
+                                    </div>
+                                </div>
+
                                 <input type="hidden" name="merchId" value="<?= $merch['merchandise_id'] ?>">
                                 <input type="hidden" name="userId" value="<?= $data['user_id'] ?>">
                                 <input type="hidden" name="email" value="<?= $data['email'] ?>">
-                                <button class="buy-button" type="submit" name="buyMerch">Buy</button>
+                                <button class="buy-button modal-trigger" type="button" name="buyMerch">Buy</button>
                             </form>
                         </div>
                     </div>
@@ -56,4 +76,5 @@
             <? endforeach; ?>
         </div>
     </div>
-</div>
\ No newline at end of file
+</div>
+<script src="../../../public/js/modalIter.js"></script>
\ No newline at end of file
diff --git a/app/views/question/index.php b/app/views/question/index.php
index 5cdec55..bad1736 100644
--- a/app/views/question/index.php
+++ b/app/views/question/index.php
@@ -4,6 +4,24 @@
             <?= $data['currentExercise']['exe_name'] ?>
         </h1>
         <form method="post" action="../../../api/exercise/submit.php">
+            <!-- Modal -->
+            <div class="confirm-container close-modal-trigger">
+                <div class="confirm-card">
+                    <div class="confirm-content">
+                        <h2 class="text-md text-red font-reg">Are you sure?</h2>
+                        <p class="text-sm text-black font-reg">Please re-check your answers before submitting</p>
+                    </div>
+                    <div class="modal-button-container">
+                        <button type="button" class="secondary-btn font-reg text-sm close-modal-trigger">
+                            Cancel
+                        </button>   
+                        <button class="primary-btn font-reg text-sm" id="logout-btn" name="submitQuiz" type="submit">
+                            Submit
+                        </button>   
+                    </div>
+                </div>  
+            </div>
+
             <div class="question-content">
                 <?php $questionNumber = 1; ?>
                 <?php foreach ($data["questions"] as $question): ?>
@@ -38,7 +56,7 @@
                 <input type="hidden" name="userId" value="<?= $data["user_id"] ?>">
                 <input type="hidden" name="isDone" value="<?= $data["isDone"] ?>">
                 <div class="button-container">
-        <button class="primary-button submit-button" type="submit" name="submitQuiz" onclick="submitFormAndRedirect()">Submit</button>
+        <button class="primary-button submit-button modal-trigger" type="button" name="submitQuiz">Submit</button>
     </div>
             </div>
         </form>
@@ -65,4 +83,5 @@
             <input type="hidden" name="selectedOptions" value="${listString}">
         `;
     }
-</script>
\ No newline at end of file
+</script>
+<script src="../../../public/js/modal.js"></script>
\ No newline at end of file
diff --git a/public/js/modalIter.js b/public/js/modalIter.js
new file mode 100644
index 0000000..6bcc2f3
--- /dev/null
+++ b/public/js/modalIter.js
@@ -0,0 +1,14 @@
+const modalTriggers = document.querySelectorAll(".modal-trigger");
+
+modalTriggers.forEach((trigger, index) => {
+  const modalBox = trigger.parentNode.querySelector(".confirm-container");
+  const closeBtn = modalBox.querySelector(".close-modal-trigger");
+
+  trigger.addEventListener("click", () => {
+    modalBox.classList.add("active");
+  });
+
+  closeBtn.addEventListener("click", () => {
+    modalBox.classList.remove("active");
+  });
+});
\ No newline at end of file
-- 
GitLab