From 223432f898b781f1b46e5c0c6245b8dc5c47c0fa Mon Sep 17 00:00:00 2001 From: unknown <13521043@std.stei.itb.ac.id> Date: Wed, 15 Nov 2023 19:39:42 +0700 Subject: [PATCH] feat: add ajax to soap service (not done yet) --- src/admin/gym/index.php | 4 +- src/public/javascript/gym/application.js | 119 +++++++++++++++++------ src/public/javascript/gym/trainer.js | 6 +- 3 files changed, 93 insertions(+), 36 deletions(-) diff --git a/src/admin/gym/index.php b/src/admin/gym/index.php index cc79f2b..76b8109 100644 --- a/src/admin/gym/index.php +++ b/src/admin/gym/index.php @@ -27,6 +27,8 @@ AuthMiddleware::getInstance()->secureRoute(true); <div class="app"> <script> const apikey = <?php echo json_encode($_ENV['API_KEY']) ?>; + const soapkey = <?php echo json_encode($_ENV['AUTHORIZATION']) ?>; + const soapurl = <?php echo json_encode($_ENV['SOAP_URL']) ?>; </script> <?php echo NavbarAdmin(); @@ -118,7 +120,7 @@ AuthMiddleware::getInstance()->secureRoute(true); <script src="/public/javascript/gym/application.js"></script> <script src="/public/javascript/gym/trainer.js"></script> <script> - getGymTrainers(); + // getGymTrainers(<?php echo $gym_id ?>); getGymApplication(<?php echo $gym_id ?>); </script> </body> diff --git a/src/public/javascript/gym/application.js b/src/public/javascript/gym/application.js index d4ebfe8..f597e91 100644 --- a/src/public/javascript/gym/application.js +++ b/src/public/javascript/gym/application.js @@ -4,23 +4,33 @@ function hidePopupRej() { document.querySelector(`#pop-up-reject`).style.display function showPopupRej() { document.querySelector(`#pop-up-reject`).style.display = "flex";} // dummy data -let trainer = { - username: "johndoe", - gym_id: 43, - acceptance: 0, - trainer_name: "John Doe", - trainer_description: "Aku ingin bekerja", - application_description: "Aku ingin bekerja di sebuah gym, aku memiliki semangat di gym 43 ini semoga bisa keterima. Sekian dan terima kasih atas perhatian dan kesan dan pesannya", -} -let trainer2 = { - username: "johndoedfdf", - gym_id: 43, - acceptance: 0, - trainer_name: "John Doe", - trainer_description: "Aku ingin bekerja", - application_description: "Aku ingin bekerja", -} -let data = [trainer, trainer2]; +// let trainer = { +// username: "johndoe", +// gym_id: 43, +// acceptance: 0, +// trainer_name: "John Doe", +// trainer_description: "Aku ingin bekerja", +// application_description: "Aku ingin bekerja di sebuah gym, aku memiliki semangat di gym 43 ini semoga bisa keterima. Sekian dan terima kasih atas perhatian dan kesan dan pesannya", +// } +// let trainer2 = { +// username: "johndoedfdf", +// gym_id: 43, +// acceptance: 0, +// trainer_name: "John Doe", +// trainer_description: "Aku ingin bekerja", +// application_description: "Aku ingin bekerja", +// } +// let data = [trainer, trainer2]; +const response = `<?xml version="1.0"?> +<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> + <S:Body> + <ns2:getAllAplicationResponse xmlns:ns2="http://service.gymtracker.com/"> + <return>[nerbi1, 37, 0, Rayhan, Let's get some gainz brah, Give me a job] +[nerbi2, 37, 1, Rayhan, Let's get some gainz brah, Give me a job] +[nerbi3, 37, 0, Nigel, Let's get some gainz brah, Give me a job]</return> + </ns2:getAllAplicationResponse> + </S:Body> +</S:Envelope>` function modifyGymApplicationList(data) { @@ -29,7 +39,7 @@ function modifyGymApplicationList(data) { if (data.length == 0) { gymApplicationList.innerHTML = ` <div class="space"></div> - <div class="gym-card-text"> No Aplication found </div> + <div class="gym-card-text"> No Application found </div> `; } data.forEach((trainer) => { @@ -38,24 +48,39 @@ function modifyGymApplicationList(data) { } function getGymApplication(gymId) { - modifyGymApplicationList(data); - const xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function () { + modifyGymApplicationList(filterApplication(parseXmlResponse(response), 0)); + showGymTrainers(filterApplication(parseXmlResponse(response), 1)); + + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { if (this.readyState === 4) { - if (this.status === 200) { - modifyGymApplicationList(JSON.parse(this.responseText)["trainers"]); - } else { - const json = JSON.parse(this.responseText); - alert(json["error"]); - } + if (this.status === 200) { + const applications = parseXmlResponse(this.responseText); + alert("trainer : " + applications); + modifyGymApplicationList(filterApplication(applications, 0)); + showGymTrainers(filterApplication(applications, 1)); + } else { + const xml = this.responseXML; + alert("error status : " + this.status + " error text : " + xml); + } } - }; - - // xhr.open("GET", `/api/gym/rating?gym_id=${gymId}`, true); - // xhr.setRequestHeader("X-API-KEY", apikey); - xhr.send(); + }; + + const soapBody = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> + <Body> + <getAllAplication xmlns="http://service.gymtracker.com/"> + <gym_id xmlns="">${gymId}</gym_id> + </getAllAplication> + </Body> + </Envelope>`; + // alert("soapbody : " + soapBody); + xhr.open("POST", soapurl, true); + xhr.setRequestHeader("Content-Type", "text/xml"); + xhr.setRequestHeader("Authorization", soapkey); + xhr.send(soapBody); } + function getApplication(username, gymId) { // alert("get application with username: " + username + " and gym id: " + gymId); modifyApplication(trainer); @@ -128,4 +153,34 @@ function modifyApplication(trainer) { </tr> </table> ` +} + +function parseXmlResponse(xmlString) { + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(xmlString, "text/xml"); + const returnElement = xmlDoc.getElementsByTagName("return")[0]; + const returnText = returnElement.textContent; + + // Split the return text into an array of trainer strings + const trainerStrings = returnText.split("\n").filter(str => str.trim() !== ""); + + // Parse each trainer string into a trainer object + const trainers = trainerStrings.map(trainerString => { + const values = trainerString.replace("[", "").replace("]", "").split(", "); + return { + username: values[0], + gym_id: parseInt(values[1]), + acceptance: parseInt(values[2]), + trainer_name: values[3], + trainer_description: values[4], + application_description: values[5] + }; + }); + + return trainers; +} + +function filterApplication(Applications, acc){ + // filter Application with Acceptance acc + return Applications.filter(application => application.acceptance == acc); } \ No newline at end of file diff --git a/src/public/javascript/gym/trainer.js b/src/public/javascript/gym/trainer.js index 39daf32..d54618d 100644 --- a/src/public/javascript/gym/trainer.js +++ b/src/public/javascript/gym/trainer.js @@ -29,9 +29,9 @@ let trainer_1 = { let trainerData = [trainer_1, trainer_2]; // Functions -function getGymTrainers() { - showGymTrainers(trainerData); -} +// function getGymTrainers(gymId) { +// showGymTrainers(trainerData); +// } function showGymTrainers(data) { const gymTrainerList = document.getElementById("gym-trainer-list"); -- GitLab