Skip to content

Commit

Permalink
y
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonLGH committed Sep 18, 2024
1 parent d650911 commit c930ec2
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
57 changes: 56 additions & 1 deletion src/mpk.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,62 @@
<title>Pemilihan Ketua MPK-OSIS SMAN 1 Cikampek 2024/2025</title>
<link href="https://fonts.googleapis.com/css?family=Poppins:400,500,600&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<script src="./assets/js/mpk.js"></script>
<!-- <script src="./assets/js/mpk.js"></script> -->
<script>
init()

async function init(){
const data = JSON.parse(localStorage.getItem("loginObject"))
console.log(data)

if(!data || !localStorage.getItem("loginObject")) {
alert("NO USERDATA, REDIRECTING TO LOGIN")
document.location.href = "./index.html"
}
try {
const response = await fetch("https://redesigned-lamp-wx969x4q6g6f599x-8080.app.github.dev/api/user/login", {
method: "POST", // HTTP method
headers: {
"Content-Type": "application/json", // Specify the content type
},
body: JSON.stringify({ username:data.username, password:data.password }), // Convert the data to a JSON string
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const result = await response.json(); // Parse the response as JSON

if (result.error) {
} else {
// Handle successful login
document.querySelector("#usn").textContent = result.user.username
}
} catch (error) {
console.error("An error occurred while logging in", error);
alert("An error occurred while logging in. Please try again.");
}
}


async function kirim() {
if(!document.querySelector("input[name='kandidat']:checked")) alert("Pilihlah 1 kandidat")
const selectedKandidat = document.querySelector("input[name='kandidat']:checked").value;
alert(`Kandidat yang dipilih: ${selectedKandidat}`);
localStorage.setItem("Pilihan", JSON.stringify({"MPK":selectedKandidat}))

// const response = await fetch("https://redesigned-lamp-wx969x4q6g6f599x-8080.app.github.dev/api/user/login", {
// method: "POST", // HTTP method
// headers: {
// "Content-Type": "application/json", // Specify the content type
// },
// body: JSON.stringify({ username:data.username, password:data.password }), // Convert the data to a JSON string
// });

document.location.href = "./osis.html"
}
</script>
<script>
document.addEventListener("DOMContentLoaded", function() {
const kandidatCards = document.querySelectorAll("input[name='kandidat']");
Expand Down
101 changes: 100 additions & 1 deletion src/osis.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,106 @@
<link href="https://fonts.googleapis.com/css?family=Poppins:400,500,600&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="./assets/css/main.css">
<script src="./assets/js/osis.js"></script>
<!-- <script src="./assets/js/osis.js"></script> -->
<script>
// Inisialisasi fungsi
init();

async function init() {
// Ambil data login dari localStorage
const data = JSON.parse(localStorage.getItem("loginObject"));
console.log(data);

// Cek apakah data login tersedia
if (!data || !localStorage.getItem("loginObject")) {
alert("NO USERDATA, REDIRECTING TO LOGIN");
document.location.href = "./index.html";
return; // Hentikan eksekusi lebih lanjut jika data tidak ada
}

try {
// Lakukan permintaan POST untuk memverifikasi pengguna
const response = await fetch("https://redesigned-lamp-wx969x4q6g6f599x-8080.app.github.dev/api/user/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username: data.username, password: data.password }),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const result = await response.json();

if (result.error) {
// Tangani kesalahan jika ada
console.error("Login failed: ", result.error);
alert("Login failed. Please try again.");
} else {
// Tampilkan nama pengguna jika login berhasil
document.querySelector("#usn").textContent = result.user.username;
}
} catch (error) {
console.error("An error occurred while logging in", error);
alert("An error occurred while logging in. Please try again.");
}
}

// Fungsi untuk menangani pemilihan kandidat
async function kirim() {
// Cek apakah ada kandidat yang dipilih
const selectedKandidat = document.querySelector("input[name='osis']:checked");

if (!selectedKandidat) {
alert("Pilihlah 1 kandidat");
return; // Hentikan eksekusi lebih lanjut jika tidak ada kandidat yang dipilih
}

const kandidatValue = selectedKandidat.value;
alert(`Kandidat yang dipilih: ${kandidatValue}`);

try {
let pilihan = JSON.parse(localStorage.getItem("Pilihan"))

if(!pilihan) {
alert("Kamu harus pilih MPK Dahulu")
document.location.href = "./mpk.html"
}

localStorage.setItem("Pilihan",{"MPK":pilihan.mpk,"OSIS":kandidatValue})

// Kirim data pemilihan ke server
// const response = await fetch("https://redesigned-lamp-wx969x4q6g6f599x-8080.app.github.dev/api/vote", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({ username: data.username, password: data.password, kandidat: kandidatValue }),
// });

// if (!response.ok) {
// throw new Error(`HTTP error! status: ${response.status}`);
// }

// const result = await response.json();

// if (result.error) {
// console.error("Voting failed: ", result.error);
// alert("Voting failed. Please try again.");
// } else {
// Tindakan setelah pemilihan berhasil, misalnya mengalihkan halaman
// alert("Vote submitted successfully!");
document.location.href = "./success.html"; // Ganti dengan halaman yang sesuai
// }
} catch (error) {
console.error("An error occurred while voting", error);
alert("An error occurred while voting. Please try again.");
}
}

</script>
<style>
body {
font-family: 'Poppins', sans-serif;
Expand Down

0 comments on commit c930ec2

Please sign in to comment.