From 2c9ea10940cd3ff7ffab5087e430e529603a2156 Mon Sep 17 00:00:00 2001
From: christodharma <13521009@std.stei.itb.ac.id>
Date: Sun, 8 Oct 2023 16:29:19 +0700
Subject: [PATCH] feat: make-sale front end js fix: generalizing logged out
 catch

---
 client/css/make-sale.css                      |  7 ++--
 client/js/make-sale.js                        | 38 +++++++++++++++++++
 client/pages/account-page.php                 | 14 ++-----
 client/{protoype/cart.html => pages/cart.php} |  7 +++-
 client/pages/make-sale.php                    | 14 +++++--
 server/controllers/loggedout_catch.php        | 11 ++++++
 6 files changed, 73 insertions(+), 18 deletions(-)
 create mode 100644 client/js/make-sale.js
 rename client/{protoype/cart.html => pages/cart.php} (91%)
 create mode 100644 server/controllers/loggedout_catch.php

diff --git a/client/css/make-sale.css b/client/css/make-sale.css
index 763a29f..003f1c8 100644
--- a/client/css/make-sale.css
+++ b/client/css/make-sale.css
@@ -44,9 +44,10 @@
 }
 
 
-form p {
-    text-align : center;
-    font-size : 20px;
+#price-criteria, #quantity-criteria {
+    text-align : left;
+    font-size : medium;
+    color: red;
     /* color : #ffffffab; */
 }
 
diff --git a/client/js/make-sale.js b/client/js/make-sale.js
new file mode 100644
index 0000000..a1a2af5
--- /dev/null
+++ b/client/js/make-sale.js
@@ -0,0 +1,38 @@
+function checkValid(field, criteria, p_affected, response_if_invalid){
+    if (criteria){
+        //valid
+        resetFieldStyle(field, p_affected);
+    } else {
+        //invalid
+        invalidStyle(field, response_if_invalid, p_affected);
+    }
+}
+
+function checkNumeric(field, field_input, p_affected) {
+    checkValid(field, (/^\d+$/.test(field_input) || field_input==""), p_affected, "please input numerical value");
+}
+
+function checkNumericPrice() {
+    checkNumeric(
+        document.getElementById("product_price-field"),
+        document.getElementById("product_price").value,
+        document.getElementById("price-criteria")
+    )
+}
+function checkNumericQuantity() {
+    checkNumeric(
+        document.getElementById("product_quantity-field"),
+        document.getElementById("product_quantity").value,
+        document.getElementById("quantity-criteria")
+    )
+}
+
+function invalidStyle(param_field, reason, criteria_p) {
+    param_field.style.borderColor = "red";
+    criteria_p.textContent = reason;
+}
+
+function resetFieldStyle(param_field, criteria_p) {
+    param_field.style.borderColor = "black";
+    criteria_p.textContent = ' ';
+}
\ No newline at end of file
diff --git a/client/pages/account-page.php b/client/pages/account-page.php
index 51c2043..960a53c 100644
--- a/client/pages/account-page.php
+++ b/client/pages/account-page.php
@@ -1,13 +1,7 @@
-<?php session_start();
-if (!isset($_SESSION['username']) or !isset($_SESSION['email'])) {
-    echo '<script type = "text/javascript">  
-    function loggedout_catch() {  
-       alert("You are logged in, please logout first if you want to login again");
-       location.href = "../pages/login-page.php"
-    }  
-    loggedout_catch();
-   </script>';
-}?>
+<?php 
+    session_start();
+    require_once "../../server/controllers/loggedout_catch.php";
+?>
 <!DOCTYPE html>
 <html>
     <head>
diff --git a/client/protoype/cart.html b/client/pages/cart.php
similarity index 91%
rename from client/protoype/cart.html
rename to client/pages/cart.php
index 31fff5c..7da1a81 100644
--- a/client/protoype/cart.html
+++ b/client/pages/cart.php
@@ -1,3 +1,7 @@
+<?php session_start();
+    require_once "../../server/controllers/loggedout_catch.php";
+    loggedout_catch();
+?>
 <!DOCTYPE html>
 <html>
     <head>
@@ -7,6 +11,7 @@
         <link rel="stylesheet" href="../css/navbar.css">
         <script src="https://kit.fontawesome.com/8505941c5b.js" crossorigin="anonymous"></script>
         <script src="../js/navbar.js"></script>
+        <script src="../js/cart.js"></script>
     </head>
     <body>
         <div class="container">
@@ -53,7 +58,7 @@
                     <!-- contains item divs according to data -->
                 </div>
                 <div class="button-field">
-                    <button id="checkout-button">
+                    <button id="checkout-button" onclick="submitCheckout()">
                     Checkout
                     </button>
                 </div>
diff --git a/client/pages/make-sale.php b/client/pages/make-sale.php
index edd7d13..dcd3c43 100644
--- a/client/pages/make-sale.php
+++ b/client/pages/make-sale.php
@@ -1,3 +1,6 @@
+<?php session_start();
+    require_once "../../server/controllers/loggedout_catch.php";
+?>
 <!DOCTYPE html>
 <html>
     <head>
@@ -7,6 +10,7 @@
         <link rel="stylesheet" href="../css/make-sale.css">
         <script src="../js/navbar.js"></script>
         <script src="https://kit.fontawesome.com/8505941c5b.js" crossorigin="anonymous"></script>
+        <script src="../js/make-sale.js"></script>
     </head>
     
     <body>
@@ -28,11 +32,13 @@
                             <div class="input-field" id="product_description-field">
                                 <input type="text" id="product_description" placeholder="Product Description">
                             </div>
-                            <div class="input-field">
-                                <input type="text" id="product_price" placeholder="Price">
+                            <div class="input-field" id="product_price-field">
+                                <input type="text" id="product_price" placeholder="Price" oninput="checkNumericPrice()">
+                                <p id="price-criteria"> </p>
                             </div>
-                            <div class="input-field">
-                                <input type="text" id="product_qty" placeholder="Quantity">
+                            <div class="input-field" id="product_quantity-field">
+                                <input type="text" id="product_qty" placeholder="Quantity" oninput="checkNumericQuantity()">
+                                <p id="quantity-criteria"> </p>
                             </div>
                         </div>
                         
diff --git a/server/controllers/loggedout_catch.php b/server/controllers/loggedout_catch.php
new file mode 100644
index 0000000..ac314e5
--- /dev/null
+++ b/server/controllers/loggedout_catch.php
@@ -0,0 +1,11 @@
+<?php
+if (!isset($_SESSION['username']) or !isset($_SESSION['email'])) {
+    echo '<script type = "text/javascript">  
+    function loggedout_catch() {  
+       alert("You are logged out, please login first");
+       location.href = "../pages/login-page.php"
+    }  
+    loggedout_catch();
+   </script>';
+}
+?>
\ No newline at end of file
-- 
GitLab