@@ -190,7 +189,6 @@
function whitespace(str) {
return str === null || str.match(/^ *$/) !== null;
}
-
function showAlert(type, message) {
if (type === 'error') {
Swal.fire({
@@ -209,6 +207,92 @@
}
}
+ document.getElementById('updateImages').addEventListener('click', async () => {
+ document.getElementById('typexImages').innerHTML = '';
+ const res = await fetch('/api/images?user=' + "<%=user.id%>", {
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ });
+ try {
+ const json = await res.json();
+ if (json.error || json.code) return showAlert('error', json.error)
+ else {
+ json.forEach(image => {
+ $('#typexImages').append(`
+
+ `)
+ });
+ }
+ } catch (e) {
+ console.error(e)
+ }
+ });
+
+ const copyText = (text) => {
+ const el = document.createElement('textarea'); el.value = text; document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el);
+ }
+
+ const deleteImage = (id, url) => {
+ Swal.fire({
+ title: 'Are you sure?',
+ text: `You are proceeding to delete image (${id}), you will not be able to recover it!`,
+ icon: 'warning',
+ showCancelButton: true,
+ confirmButtonColor: '#3085d6',
+ cancelButtonColor: '#d33',
+ confirmButtonText: 'Yes, delete it.'
+ }).then(async (result) => {
+ if (result.value) {
+ const username = document.getElementById('usernameSave').value;
+ const password = document.getElementById('passwordSave').value;
+ if (whitespace(username)) return showAlert('error', 'Please input a username.')
+ const res = await fetch('/api/user', {
+ method: 'PATCH',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ username,
+ password
+ })
+ });
+ try {
+ const json = await res.json();
+ if (json.error || json.code) return showAlert('error', json.error)
+ else {
+ const res = await fetch('/api/images?image=' + id, {
+ method: 'DELETE',
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ });
+ try {
+ const json = await res.json();
+ if (json.error || json.code) return showAlert('error', json.error)
+ else {
+ Swal.fire(
+ 'Deleted!',
+ `Deleted image (${id}) successfully.`,
+ 'success'
+ );
+ }
+ } catch (e) {
+ console.error(e)
+ }
+ }
+ } catch (e) {
+ console.error(e)
+ }
+ }
+ });
+ }
document.getElementById('saveUser').addEventListener('click', async () => {
Swal.fire({
title: 'Are you sure?',
@@ -261,12 +345,7 @@
confirmButtonText: 'Yes, copy it!'
}).then((result) => {
if (result.value) {
- const el = document.createElement('textarea');
- el.value = "<%= user.token %>";
- document.body.appendChild(el);
- el.select();
- document.execCommand('copy');
- document.body.removeChild(el);
+ copyText("<%= user.token %>");
Swal.fire(
'Copied!',
'Your API Token has been copied.',
@@ -310,53 +389,7 @@
}
});
- })
- // async function editUser() {
- // const username = document.getElementById('usernameEdit').value;
- // const password = document.getElementById('passwordEdit').value;
- // if (whitespace(username)) return showAlert('error', 'Please input a username.')
- // const res = await fetch('/api/user', {
- // method: 'PATCH',
- // headers: {
- // 'Content-Type': 'application/json'
- // },
- // body: JSON.stringify({
- // username,
- // password,
- // administrator: document.getElementById('administratorEdit').checked
- // })
- // });
- // try {
- // const json = await res.json();
- // if (json.error || json.code) return showAlert('error', json.error)
- // else {
- // $('#edit-modal').modal('toggle');
- // showAlert('success', `Edited user ${json.username} (${json.id})`)
- // return window.location.href = '/'
- // }
- // } catch (e) {
- // console.error(e)
- // }
- // }
- // document.getElementById('editUser').addEventListener('click', async () => {
- // if (document.getElementById('administratorEdit').checked) {
- // Swal.fire({
- // title: 'Are you sure?',
- // text: "You are proceeding to save a user with administrator permissions, they can do whatever they want!",
- // icon: 'warning',
- // showCancelButton: true,
- // confirmButtonColor: '#3085d6',
- // cancelButtonColor: '#d33',
- // confirmButtonText: 'Yes, edit user!'
- // }).then(async (result) => {
- // if (result.value) {
- // editUser()
- // }
- // });
- // } else {
- // editUser();
- // }
- // })
+ });
async function createUser() {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
diff --git a/views/login.ejs b/views/login.ejs
index b5318394..612f75a7 100644
--- a/views/login.ejs
+++ b/views/login.ejs
@@ -32,7 +32,6 @@