include axios and show toasts

This commit is contained in:
Florian Hoss 2022-04-05 21:21:48 +02:00
parent 712af41c28
commit 5b2252d6bd
5 changed files with 69 additions and 19 deletions

View file

@ -6,5 +6,6 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="bootstrap" level="application" /> <orderEntry type="library" name="bootstrap" level="application" />
<orderEntry type="library" name="axios" level="application" />
</component> </component>
</module> </module>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="JavaScriptLibraryMappings"> <component name="JavaScriptLibraryMappings">
<file url="PROJECT" libraries="{bootstrap}" /> <file url="PROJECT" libraries="{axios, bootstrap}" />
</component> </component>
</project> </project>

View file

@ -10,6 +10,31 @@
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
{{end}} {{end}}
{{define "toasts"}}
<div class="position-absolute bottom-0 end-0 mb-3 me-3">
<div id="errorToast" class="toast align-items-center text-white bg-danger border-0" role="alert"
aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div id="errorToastContent" class="toast-body">
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
<div class="position-absolute bottom-0 end-0 mb-3 me-3">
<div id="successToast" class="toast align-items-center text-white bg-success border-0" role="alert"
aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div id="successToastContent" class="toast-body">
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
{{end}}
{{define "navbar"}} {{define "navbar"}}
<nav class="navbar navbar-expand-md navbar-light bg-light"> <nav class="navbar navbar-expand-md navbar-light bg-light">
<div class="container-fluid"> <div class="container-fluid">
@ -63,12 +88,33 @@
{{end}} {{end}}
{{define "scripts"}} {{define "scripts"}}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script> <script>
const button_logout = document.getElementById("button-logout"); const button_logout = document.getElementById("button-logout");
const username = getCookie("username"); const username = getCookie("username");
const myHeaders = new Headers(); const myHeaders = new Headers();
myHeaders.append('username', username); myHeaders.append('username', username);
const errorToastEl = document.getElementById('errorToast');
const errorToastContent = document.getElementById('errorToastContent');
const errorToast = new bootstrap.Toast(errorToastEl, {delay: 3000})
const successToastEL = document.getElementById('successToast');
const successToastContent = document.getElementById('successToastContent');
const successToast = new bootstrap.Toast(successToastEL, {delay: 3000});
function showErrorToast(message) {
errorToastContent.innerText = message;
errorToast.show();
}
function showSuccessToast(message) {
successToastContent.innerText = message;
successToast.show();
}
function setCookie(cookie_name, cookie_value, cookie_expiry) { function setCookie(cookie_name, cookie_value, cookie_expiry) {
const d = new Date(); const d = new Date();
d.setTime(d.getTime() + (cookie_expiry * 24 * 60 * 60 * 1000)); d.setTime(d.getTime() + (cookie_expiry * 24 * 60 * 60 * 1000));
@ -126,7 +172,6 @@
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
} else { } else {
document.getElementById("button").disabled = true;
event.preventDefault(); event.preventDefault();
let data = new FormData(); let data = new FormData();
let form_element = document.getElementsByClassName('form-control'); let form_element = document.getElementsByClassName('form-control');
@ -139,4 +184,4 @@
}, false); }, false);
} }
</script> </script>
{{end}} {{end}}

View file

@ -6,6 +6,7 @@
{{template "head" .}} {{template "head" .}}
</head> </head>
<body> <body>
{{template "toasts" .}}
{{template "navbar" .}} {{template "navbar" .}}
<div class="position-absolute top-50 start-50 translate-middle"> <div class="position-absolute top-50 start-50 translate-middle">
@ -20,15 +21,15 @@
<script> <script>
userLoggedIn().then((loggedIn) => loggedIn && redirect("/")); userLoggedIn().then((loggedIn) => loggedIn && redirect("/"));
async function submitForm(formData) { function submitForm(formData) {
const response = await fetch("/auth/login", {method: 'POST', body: formData, redirect: 'follow'}); axios({method: 'POST', url: '/auth/login', data: formData})
if (response.ok) { .then((response) => {
const json = await response.json(); setCookie("username", response.data.username, 1);
setCookie("username", json.username, 1); redirect("/");
redirect("/"); })
} else { .catch((response) => {
redirect("/view/login"); showErrorToast(response.message);
} });
} }
</script> </script>
</body> </body>

View file

@ -6,6 +6,7 @@
{{template "head" .}} {{template "head" .}}
</head> </head>
<body> <body>
{{template "toasts" .}}
{{template "navbar" .}} {{template "navbar" .}}
<div class="position-absolute top-50 start-50 translate-middle"> <div class="position-absolute top-50 start-50 translate-middle">
@ -18,13 +19,15 @@
{{template "scripts" .}} {{template "scripts" .}}
{{template "formScripts" .}} {{template "formScripts" .}}
<script> <script>
async function submitForm(formData) { function submitForm(formData) {
const response = await fetch("/auth/register", {method: 'POST', body: formData, redirect: 'follow'}); axios({method: 'POST', url: '/auth/register', data: formData})
if (response.ok) { .then((response) => {
redirect("/"); showSuccessToast(response.data.message);
} else { redirect("/login");
redirect("/view/register"); })
} .catch((err) => {
showErrorToast(err.message);
});
} }
</script> </script>
</body> </body>