login user after registration, delete task

This commit is contained in:
Florian Hoss 2022-04-07 15:39:22 +02:00
parent c9063c2fa9
commit 5e63fc1e38
6 changed files with 30 additions and 22 deletions

View file

@ -8,5 +8,6 @@
<orderEntry type="library" name="bootstrap" level="application" />
<orderEntry type="library" name="axios" level="application" />
<orderEntry type="library" name="bootstrap-icons" level="application" />
<orderEntry type="library" name="axios" level="application" />
</component>
</module>

View file

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

View file

@ -50,7 +50,7 @@ func (db *Database) DeleteTask(id string) {
}
func (db *Database) CreateUser(username string, password string) error {
user := User{Username: username, Password: password}
user := User{Username: username, Password: password, LoggedIn: true}
result := db.ORM.Create(&user)
return result.Error
}

View file

@ -22,8 +22,8 @@
function submitForm(formData) {
axios({method: 'POST', url: '/auth/register', data: formData})
.then((response) => {
showSuccessToast(response.data.message);
redirect("/login");
setCookie("username", response.data.username, 1);
redirect("/");
})
.catch((err) => {
showErrorToast(err.response.data.message);

View file

@ -35,11 +35,12 @@
userLoggedIn().then((loggedIn) => !loggedIn ? redirect("/view/login") : getAllTasks());
const tasksEl = document.getElementById("tasks");
const descriptionInput = document.getElementById("description");
let tasks = [];
function submitForm(formData) {
axios.post("/tasks", formData, axiosConfig)
.then((response) => {
addTaskToTasks(response.data.task);
addTaskToTasks(response.data.task, tasks.length + 1);
form.classList.remove('was-validated');
descriptionInput.value = "";
})
@ -50,19 +51,21 @@
function deleteTask(id) {
axios.delete("/tasks/" + id, axiosConfig)
.then((response) => {
console.log(response);
.then(() => {
getAllTasks();
})
.catch((err) => {
showErrorToast(err.response.data.message);
});
}
function addTaskToTasks(task) {
function addTaskToTasks(task, number) {
tasks.push(task);
const newTask = document.createElement('div');
newTask.classList.add('row', 'g-0', 'align-items-center', 'mb-1');
newTask.id = task.ID;
newTask.innerHTML = `
<div class="col-1 text-center">${task.ID}</div>
<div class="col-1 text-center">${number}</div>
<div class="col-8">${task.Description}</div>
<div class="col-2 form-check form-switch d-flex justify-content-center">
<input class="text-center form-check-input" type="checkbox" ${task.Done && "checked"} role="switch" id="flexSwitchCheckDefault">
@ -72,23 +75,27 @@
tasksEl.appendChild(newTask);
}
function getAllTasks() {
axios.get("/tasks", axiosConfig).then((response) => {
const taskHeader = document.createElement("div");
taskHeader.classList.add('row', 'g-0', 'align-items-center', 'mb-1');
taskHeader.innerHTML = `
function addTaskHeader() {
const taskHeader = document.createElement("div");
taskHeader.classList.add('row', 'g-0', 'align-items-center', 'mb-1');
taskHeader.innerHTML = `
<div class="fw-bold col-1 text-center">ID</div>
<div class="fw-bold col-8">Description</div>
<div class="fw-bold col-2 text-center">Done</div>
<div class="fw-bold col-1 text-center">Delete</div>
<hr class="mt-3 mb-2">
`;
tasksEl.appendChild(taskHeader);
const tasks = response.data.tasks;
[...tasks]
.forEach((task) => {
addTaskToTasks(task);
});
tasksEl.appendChild(taskHeader);
}
function getAllTasks() {
tasksEl.innerHTML = "";
addTaskHeader();
axios.get("/tasks", axiosConfig).then((response) => {
tasks = response.data.tasks;
tasks.forEach((task, index) => {
addTaskToTasks(task, index + 1);
});
});
}
</script>

View file

@ -59,7 +59,7 @@ func (wp *Webpage) defineRoutes() {
if wp.isLoggedInMiddleware(c) {
id := c.Param("id")
wp.Database.DeleteTask(id)
c.JSON(200, gin.H{"message": "success", "id": id})
c.JSON(200, gin.H{"message": "success"})
}
})
}
@ -110,7 +110,7 @@ func (wp *Webpage) defineRoutes() {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{"message": "user registered"})
c.JSON(200, gin.H{"message": "user registered", "username": username})
})
}
wp.Router.NoRoute(func(c *gin.Context) {