]> git repositories - QuestLog.git/commitdiff
added how btn react accordingly based on ticked checkboxes
authorbochard <mail@tenkyuu.dev>
Tue, 10 Jun 2025 11:12:39 +0000 (19:12 +0800)
committerbochard <mail@bochard.net>
Thu, 17 Jul 2025 11:41:16 +0000 (19:41 +0800)
Classes/Tasks.php
includes/dashboard.php
includes/edit_task.php
js/tasks.js
login.php
signup.php

index 5280b5a78b71d694e7c3876f0628fa4f4029679d..236d7bf5991c4a91c13bf77310cecf6df7919114 100644 (file)
@@ -34,6 +34,13 @@ class Tasks extends DbConn {
     $stmt->execute();
   }
 
+  public function editTask($pdo, $taskId, $newInput){
+    $query = 'UPDATE tasks SET task =  WHERE task_id = ;taskId;';
+    $stmt = $pdo->prepare($query);
+    $stmt->bindParam(':taskId', $taskId);
+    $stmt->execute();
+  }
+
   public function deleteTask($pdo, $taskId){
     $query = 'DELETE FROM tasks WHERE task_id = :taskId;';
     $stmt = $pdo->prepare($query);
index 20ba694e75a47eb97c1d8f49d9beba9fe2277d86..453db9479c62ed101411a0803727d6506911bc53 100644 (file)
   <link href="./dist/rpgui.css" rel="stylesheet" type="text/css" >
   <script src="./dist/rpgui.js"></script>
 <style>
-  #new-task-popup-window {
+  #new-task-popup-window,
+  #edit-task-popup-window {
     width: 350px;
-    position: absolute;
+    position: fixed;
     z-index: 999;
     top: 50%;
     left: 50%;
 </style>
 </head>
 <body class="rpgui-content" style="display: flex; justify-content: center;">
-  <div class="rpgui-container framed" style="margin: 32px; width: 100%; height: fit-content; max-width: 600px;">
+  <div class="rpgui-container framed" style="margin: 36px 0; width: 90%; height: fit-content; max-width: 600px;">
     <div style="text-align: right;">
       <form action="../includes/logout.php" method="POST" class="title-bar-controls">
-        <button class="rpgui-button" type="submit">Exit</button>
+        <button class="rpgui-button" type="submit">Log Out</button>
       </form>
     </div>
     
@@ -63,7 +64,7 @@
             <button class="rpgui-button" type="button" id="new-task-popup-open">Add task</button>
           </div>
           <div>
-            <button class="rpgui-button" type="button" id="edit-task" disabled>Edit</button>
+            <button class="rpgui-button" type="button" id="edit-task-popup-open" disabled>Edit</button>
             <button class="rpgui-button" type="button" id="delete-task" disabled>Delete</button>
           </div>  
         </div>
         <h2 style="text-align: center;">Tasks</h2>
         <hr>
         <div style="overflow-y: auto; height: 400px;">
-          <div id="task-body" style="width: 80%; margin: auto; word-break: break-all;">
+          <div id="task-body" style="width: 90%; margin: auto; word-break: break-all;">
             <!-- ajax will insert the fetched task data here... -->
           </div>
         </div>
       </div>
       
-      <!-- popup -->
-      <div class="rpgui-container framed golden rpgui-draggable" id="new-task-popup-window">
+      <!-- popup for add task -->
+      <div class="rpgui-container framed golden" id="new-task-popup-window">
         <h3>New Task</h3>
         <p>Add the task you need to do.</p>
         <form id="add-task-form">
-          <input type="text" id='task-name' name="task-name" placeholder="New task...">
+          <input type="text" id="task-name" name="task-name" placeholder="New task...">
           <div style="display: flex; justify-content: center; align-items: center;">
             <button class="rpgui-button" type="button" id="new-task-popup-close">Close</button>
             <button class="rpgui-button" type="submit">Add</button>
           </div>
         </form>
       </div>
+
+      <!-- popup for edit task -->
+      <div class="rpgui-container framed golden" id="edit-task-popup-window">
+        <h3>Edit Task</h3>
+        <p>Apply a new value.</p>
+        <form id="edit-task-form">
+          <input type="text" id="edit-input-task-name" name="edit-input-task-name" placeholder="Add new value...">
+          <div style="display: flex; justify-content: center; align-items: center;">
+            <button class="rpgui-button" type="button" id="edit-task-popup-close">Close</button>
+            <button class="rpgui-button" type="submit">Edit</button>
+          </div>
+        </form>
+      </div>
     </main>
   </div>
 
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0c8001e38a5745fbe1a7b1cc91045dee9c568471 100644 (file)
@@ -0,0 +1,34 @@
+<?php
+
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+
+require_once __DIR__ . '/init.php';
+require_once __DIR__ . '/../Classes/Tasks.php';
+
+header('Content-Type: application/json');
+
+if($_SERVER['REQUEST_METHOD'] === 'POST'){
+  $taskIds = json_decode(file_get_contents('php://input'), true);
+  $newInput = null;
+
+  try{
+    $dbconn = new DbConn();
+    $pdo = $dbconn->getPdo();
+    
+    $userId = $_SESSION['user_data']['user_id'];
+    $tasks = new Tasks($pdo, $userId);
+    $error = $tasks->error;
+    
+    foreach($taskIds as $taskId){
+      $tasks->isInputEmpty($taskId);
+      if(empty($error)){
+        $tasks->editTask($pdo, $taskId, $newInput);
+      }
+    }
+
+  } catch(PDOException $e){
+    die("Query failed: {$e}");
+  }
+}
\ No newline at end of file
index 03a6a8ae41767e31322fc3e175e0a6bd280add03..893fa8d4221e4e71b13c1cb5bb53392dc7bc5da7 100644 (file)
@@ -1,4 +1,6 @@
 const addTaskForm = document.getElementById('add-task-form');
+const editTaskForm = document.getElementById('edit-task-form');
+let isCheckedArray = [];
 
 //===== LOAD TASKS =====//
 function loadTasks(){
@@ -38,30 +40,49 @@ function addCheckboxListeners(){
   const checkboxes = document.querySelectorAll('#task-body input[type="checkbox"]');
 
   checkboxes.forEach(function(checkbox){
-    checkbox.addEventListener('change', function(){
-      const isAnyChecked = Array.from(checkboxes).some(function(checkbox){
-        return checkbox.checked;
-      });
+    checkbox.addEventListener('change', evaluateCheckedCheckboxes);
+  });
+  
+  function evaluateCheckedCheckboxes(){
+    const checked = Array.from(checkboxes).filter(function(cb){
+      return cb.checked;
+    });
 
-      if(isAnyChecked){
-        removeDisabledAttributes();
-      } else{
-        setDisabledAttributes();
-      }
+    isCheckedArray = checked.map(function(cb){
+      return cb.id;
     });
-  });
-}
 
-function removeDisabledAttributes(){
-  document.getElementById('edit-task').removeAttribute('disabled');
+    if (checked.length === 1) {
+      console.log('only one ticked');
+      removeDisabledAttributesOnDeleteBtn();
+      removeDisabledAttributesOnEditBtn();
+    } else if (checked.length > 1) {
+      console.log('multiple ticked');
+      removeDisabledAttributesOnDeleteBtn();
+      setDisabledAttributesOnEditBtn();
+    } else {
+      setDisabledAttributesOnDeleteBtn();
+      setDisabledAttributesOnEditBtn();
+    }
+  }
+};
+
+function removeDisabledAttributesOnDeleteBtn(){
   document.getElementById('delete-task').removeAttribute('disabled');
 };
 
-function setDisabledAttributes(){
-  document.getElementById('edit-task').setAttribute('disabled', true);
+function setDisabledAttributesOnDeleteBtn(){
   document.getElementById('delete-task').setAttribute('disabled', true);
 };
 
+function removeDisabledAttributesOnEditBtn(){
+  document.getElementById('edit-task-popup-open').removeAttribute('disabled');
+};
+
+function setDisabledAttributesOnEditBtn(){
+  document.getElementById('edit-task-popup-open').setAttribute('disabled', true);
+};
+
 //===== ADD TASK =====//
 function addTask(){
   const taskInput = document.getElementById('task-name');
@@ -89,7 +110,8 @@ function addTask(){
     setTimeout(function(){
     loadTasks();
     }, 500);
-    setDisabledAttributes();
+    setDisabledAttributesOnDeleteBtn();
+    setDisabledAttributesOnEditBtn();
     popupNewTaskClose();
   })
   .catch(function(error){
@@ -99,21 +121,41 @@ function addTask(){
 
 //===== EDIT TASK =====//
 function editTask(){
-  
+  const taskInput = document.getElementById('edit-input-task-name');
+  const formData = new FormData(editTaskForm);
+  const taskName = formData.get('edit-input-task-name').trim();
+
+  // replace placeholder text when no input
+  if(!taskName){
+    taskInput.placeholder = 'Please fill this input...';
+    return;
+  }
+
+  fetch('./includes/edit_task.php', {
+    method: 'POST',
+    headers: { 'Content-Type': 'application/json' },
+    body: JSON.stringify(isCheckedArray)
+  })
+  .then(function(response){
+    console.log(response);
+    return response.json();
+  })
+  .then(function(data){
+    console.log(data);
+    setTimeout(function(){
+    loadTasks();
+    }, 500);
+    // setDisabledAttributesOnEditBtn();
+  })
+  .catch(function(error){
+    console.error(error);
+  });
 }
 
 //===== DELETE TASK =====//
 function deleteTask(){
-  const checkboxes = document.querySelectorAll('#task-body input[type="checkbox"]');
-  let isCheckedArray = [];
-
-  for(let checkbox of checkboxes){
-    if(checkbox.checked){
-      isCheckedArray.push(checkbox.id);
-    }
-  }
+  console.log(isCheckedArray);
 
-  // after iterating and finding the id of a checkbox that is checked...
   fetch('./includes/delete_task.php', {
     method: 'POST',
     headers: { 'Content-Type': 'application/json' },
@@ -129,19 +171,25 @@ function deleteTask(){
     setTimeout(function(){
     loadTasks();
     }, 500);
-    setDisabledAttributes();
+    setDisabledAttributesOnDeleteBtn();
   })
   .catch(function(error){
     console.error(error);
   });
 }
 
-function resetTaskInputPlaceholder(){
+function resetTaskInputPlaceholderForAddBtn(){
   const taskInput = document.getElementById('task-name');
   setTimeout(function(){
     taskInput.placeholder = 'New task...';
   }, 200);
 }
+function resetTaskInputPlaceholderForEditBtn(){
+  const taskInput = document.getElementById('edit-input-task-name');
+  setTimeout(function(){
+    taskInput.placeholder = 'New task...';
+  }, 200);
+}
 
 function popupNewTaskOpen(){
   popupWindow = document.getElementById('new-task-popup-window');
@@ -157,6 +205,20 @@ function popupNewTaskClose(){
   popupWindow.style.visibility = 'hidden';
 }
 
+function popupEditTaskOpen(){
+  popupWindow = document.getElementById('edit-task-popup-window');
+  popupWindow.style.visibility = 'visible';
+  popupWindow.style.opacity = 1;
+}
+
+function popupEditTaskClose(){
+  popupWindow = document.getElementById('edit-task-popup-window');
+  setTimeout(function(){
+    editTaskForm.reset();
+  }, 200);
+  popupWindow.style.visibility = 'hidden';
+}
+
 
 // event listeners
 document.getElementById('new-task-popup-open').addEventListener('click', function(){
@@ -164,17 +226,33 @@ document.getElementById('new-task-popup-open').addEventListener('click', functio
 });
 
 document.getElementById('new-task-popup-close').addEventListener('click', function(){
-  resetTaskInputPlaceholder();
+  resetTaskInputPlaceholderForAddBtn();
   popupNewTaskClose();
 });
 
-document.getElementById('task-name').addEventListener('input', resetTaskInputPlaceholder);
+document.getElementById('edit-task-popup-open').addEventListener('click', function(){
+  popupEditTaskOpen();
+});
+
+document.getElementById('edit-task-popup-close').addEventListener('click', function(){
+  resetTaskInputPlaceholderForEditBtn();
+  popupEditTaskClose();
+});
+
+document.getElementById('task-name').addEventListener('input', resetTaskInputPlaceholderForAddBtn);
+
+document.getElementById('edit-input-task-name').addEventListener('input', resetTaskInputPlaceholderForEditBtn);
 
 document.getElementById('add-task-form').addEventListener('submit', function(e){
   e.preventDefault();
   addTask();
 });
 
+document.getElementById('edit-task-form').addEventListener('submit', function(e){
+  e.preventDefault();
+  editTask();
+});
+
 document.getElementById('delete-task').addEventListener('click', function(e){
   e.preventDefault();
   deleteTask();
index 4a43e7b6f890ff3918f608c6325742e421d07fa3..5099f2187edb3e50bfbad5c732393d79a130b63e 100644 (file)
--- a/login.php
+++ b/login.php
@@ -53,7 +53,7 @@
   <script src="./dist/rpgui.js"></script>
 </head>
 <body class="rpgui-content" style="display: flex; justify-content: center;">
-  <div class="rpgui-container framed" style="margin: 32px; width: 100%; height: fit-content; max-width: 600px;">
+  <div class="rpgui-container framed" style="margin: 32px 0; width: 90%; height: fit-content; max-width: 600px;">
 
     <main>
       <h1 style="text-align: center;">Log in</h1>
index fd46f192920b18af15012f813e0d45d05b1a5267..7466c7b4ebdc615d1eb9e12af5f215a45d53616f 100644 (file)
@@ -47,7 +47,7 @@
   <script src="./dist/rpgui.js"></script>
 </head>
 <body class="rpgui-content" style="display: flex; justify-content: center;">
-  <div class="rpgui-container framed" style="margin: 32px; width: 100%; height: fit-content; max-width: 600px;">
+  <div class="rpgui-container framed" style="margin: 32px 0; width: 90%; height: fit-content; max-width: 600px;">
 
     <main>
       <h1 style="text-align: center;">Sign up</h1>