diff --git a/src/app/components/app/app_page.php b/src/app/components/app/app_page.php index e74a7afc70728c6ee19126be6371533b3069db1d..62933b5248b8fad16034258a90ece162b139af8f 100644 --- a/src/app/components/app/app_page.php +++ b/src/app/components/app/app_page.php @@ -17,8 +17,10 @@ <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/dashboard/pages/episode.css"> <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/dashboard/components/button.css"> <!-- JavaScript Library --> + <script type="module" src="<?= BASE_URL ?>/javascript/toast.mjs" defer></script> <script type="text/javascript" src="<?= BASE_URL ?>/javascript/player.js" defer></script> <script type="text/javascript" src="<?= BASE_URL ?>/javascript/util/debounce.js" defer></script> + <script type="text/javascript" src="<?= BASE_URL ?>/javascript/profile/profile.js" defer></script> <script type="module" src="<?= BASE_URL ?>/javascript/dashboard/layout.mjs" defer></script> <script type="module" src="<?= BASE_URL ?>/javascript/app/app.js" defer></script> <!-- Google Fonts --> diff --git a/src/app/components/common/profile.php b/src/app/components/common/profile.php index fca927afc956675b8fe79054550a933c0f903d85..b8d496db44f4090ecd341d21846008ec2fd41f04 100644 --- a/src/app/components/common/profile.php +++ b/src/app/components/common/profile.php @@ -23,12 +23,12 @@ <input type="text"value="<?=$this->data["username"]?>" id="username-form"> </div> </div> - <button class="sh4" id="submit-profile">Submit</button> + <button class="sh3" id="submit-profile">Save</button> + <p id="save-profile-alert"></p> </div> </div> <?php endif; ?> </section> -<script type="text/javascript" src="<?= BASE_URL ?>/javascript/profile/profile.js" defer></script> <script> const logoutBtn = document.getElementById("logout"); logoutBtn.addEventListener("click", (e) => { diff --git a/src/app/controllers/profile/update_profile.php b/src/app/controllers/profile/update_profile.php index 03822569f8c4e96070b980dad0d53238cf62ac08..b75829b06cb9b68977de57decc97b5ff31ecf698 100644 --- a/src/app/controllers/profile/update_profile.php +++ b/src/app/controllers/profile/update_profile.php @@ -17,29 +17,39 @@ class UpdateProfileController header("Max-Age: 86400"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); if (isset($_SERVER["QUERY_STRING"])) { - if (!isset($_POST['name']) || !isset($_POST['username'])) { + if ((!isset($_POST['name']) || !isset($_POST['username'])) && !(file_get_contents('php://input') != null)) { http_response_code(403); echo json_encode(["message" => "invalid username or name"]); exit; } - $name = $_POST['name']; - $username = $_POST['username']; + $name = ""; + $username = ""; + if (isset($_POST['name']) && isset($_POST['username'])){ + $name = $_POST['name']; + $username = $_POST['username']; + } else if ((file_get_contents('php://input') != null)){ + $data = json_decode(file_get_contents('php://input'), true); + $name = $data['name']; + $username = $data['username']; + } try { - if (!isset($_GET["user_id"])) { + if (!isset($_GET["user_id"]) && !isset($_SESSION["user_id"])) { http_response_code(400); echo json_encode(["message" => "Invalid user id"]); exit; } else { + $user_id = ""; + if (isset($_GET["user_id"])) { + $user_id = $_GET["user_id"]; + } else { + $user_id = $_SESSION["user_id"]; + } $userModel = new UserModel(); - $status = $userModel->updateProfile($_GET["user_id"], $name, $username); + $status = $userModel->updateProfile($user_id, $name, $username); if ($status == 200) { - if ($name== "woy") { - http_response_code(500); - } else { - } - http_response_code(500); + http_response_code(200); echo json_encode(["message" => "Profile updated successfully!", "name" => $name, "username" => $username]); exit; } else { diff --git a/src/public/javascript/profile/profile.js b/src/public/javascript/profile/profile.js index e6239b77072f13a79bcc8aff90db56902b57d0f1..dede16f7187c6d789fd9e88a0504c5f6a11dc12d 100644 --- a/src/public/javascript/profile/profile.js +++ b/src/public/javascript/profile/profile.js @@ -1,7 +1,6 @@ -import { showErrorToast, showSuccessToast } from "../toast.mjs"; - const profile = document.querySelector(".profile"); const profileMenu = document.querySelector(".profile-menu"); +const SaveProfileAlert = document.getElementById("save-profile-alert"); console.log("masuk"); profile && profile.addEventListener("click", (e) => { @@ -27,13 +26,14 @@ menuProfile && window.addEventListener("click", function (e) { if (!editSection.contains(e.target) && !menuProfile.contains(e.target)) { editProfile.style.display = "none"; + SaveProfileAlert.innerHTML = ""; } }); // Handle submit const nameForm = document.getElementById("name-form"); -const usernameForm = document.getElementById("name-form"); +const usernameForm = document.getElementById("username-form"); const submitProfileButton = document.getElementById("submit-profile"); submitProfileButton.addEventListener("click", (e) => { @@ -53,9 +53,11 @@ submitProfileButton.addEventListener("click", (e) => { if (xhr.status === 200) { nameForm.value = JSON.parse(xhr.responseText).name; usernameForm.value = JSON.parse(xhr.responseText).username; - showSuccessToast(JSON.parse(xhr.responseText).message); + SaveProfileAlert.color = "green"; + SaveProfileAlert.innerHTML = JSON.parse(xhr.responseText).message; } else { - showErrorToast(JSON.parse(xhr.responseText).message); + SaveProfileAlert.color = "red"; + SaveProfileAlert.innerHTML = JSON.parse(xhr.responseText).message; } } };