Skip to content

Commit

Permalink
fix: submit button
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiya10 committed Jan 29, 2024
1 parent fb2dc70 commit 36e7a87
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
32 changes: 22 additions & 10 deletions src/scripts/change.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const password_strength = document.getElementById("password-strength");
/**
* @type {HTMLInputElement}
*/
const new_password = document.getElementById("new-password");
/**
* @type {HTMLInputElement}
*/
const repeat_password = document.getElementById("repeat-password");
const success_view = document.getElementById("success-view");
const form_view = document.getElementById("form-view");
const container = document.getElementById("container");
const submit = document.getElementById("submit");
/**
* @type {HTMLFormElement}
*/
const changeForm = document.getElementById("change");

document.getElementById("login").onclick = (e) => {
Expand Down Expand Up @@ -38,16 +47,22 @@ const params = Object.fromEntries(urlSearchParams.entries());
if (!params.token)
document.location.replace("./login.html");

changeForm.addEventListener("submit", (e) => {
submit.addEventListener("click", (e) => {
e.preventDefault();

repeat_password.setCustomValidity("");
new_password.setCustomValidity("");

submit.disabled = true;
repeat_password.setCustomValidity("");

console.log("test");

if (repeat_password.value != new_password.value) {
repeat_password.setCustomValidity("與密碼不相符");
repeat_password.reportValidity();
return;
}

submit.disabled = true;

fetch("https://api.exptech.com.tw/api/v1/et/change", {
method : "POST",
headers : { "Content-Type": "application/json" },
Expand Down Expand Up @@ -76,21 +91,16 @@ changeForm.addEventListener("submit", (e) => {
switch (await res.text()) {
case "Invaild new pass!": {
new_password.setCustomValidity("新密碼無效。");
new_password.reportValidity();
break;
}

case "New pass format error!": {
new_password.setCustomValidity("新密碼格式錯誤。");
new_password.reportValidity();
break;
}

case "This account was not found!": {
new_password.setCustomValidity(
"找不到此帳戶,可能尚未註冊。",
);
new_password.reportValidity();
new_password.setCustomValidity("找不到此帳戶,可能尚未註冊。");
break;
}

Expand All @@ -100,6 +110,8 @@ changeForm.addEventListener("submit", (e) => {
}
}
submit.disabled = false;
new_password.reportValidity();
repeat_password.reportValidity();
}
})
.catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/forget.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ document.getElementById("login").onclick = (e) => {
window.location.href = "./login.html";
};

forgetForm.addEventListener("submit", (e) => {
submit.addEventListener("click", (e) => {
e.preventDefault();
email.setCustomValidity("");
email.reportValidity();
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const email = document.getElementById("email");
const password = document.getElementById("password");
const submit = document.getElementById("submit");
const loginForm = document.getElementById("login");

const base_url = "https://api.exptech.com.tw";
Expand All @@ -11,7 +12,7 @@ document.getElementById("register").addEventListener("click", (e) => {
window.location.href = "./register.html";
});

loginForm.addEventListener("submit", (e) => {
submit.addEventListener("click", (e) => {
e.preventDefault();

email.setCustomValidity("");
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const repeat_password = document.getElementById("repeat-password");
const success_view = document.getElementById("success-view");
const form_view = document.getElementById("form-view");
const container = document.getElementById("container");
const submit = document.getElementById("submit");
const registerForm = document.getElementById("register");

const base_url = "https://api.exptech.com.tw";
Expand Down Expand Up @@ -34,7 +35,7 @@ password.addEventListener("input", function(e) {
password_strength.className = "error empty";
});

registerForm.addEventListener("submit", (e) => {
submit.addEventListener("click", (e) => {
e.preventDefault();

repeat_password.setCustomValidity("");
Expand Down

0 comments on commit 36e7a87

Please sign in to comment.