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="bootstrap" level="application" />
<orderEntry type="library" name="axios" level="application" /> <orderEntry type="library" name="axios" level="application" />
<orderEntry type="library" name="bootstrap-icons" level="application" /> <orderEntry type="library" name="bootstrap-icons" 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="{axios, bootstrap, bootstrap-icons}" /> <file url="PROJECT" libraries="{axios, bootstrap}" />
</component> </component>
</project> </project>

View file

@ -50,7 +50,7 @@ func (db *Database) DeleteTask(id string) {
} }
func (db *Database) CreateUser(username string, password string) error { 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) result := db.ORM.Create(&user)
return result.Error return result.Error
} }

View file

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

View file

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

View file

@ -59,7 +59,7 @@ func (wp *Webpage) defineRoutes() {
if wp.isLoggedInMiddleware(c) { if wp.isLoggedInMiddleware(c) {
id := c.Param("id") id := c.Param("id")
wp.Database.DeleteTask(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()}) c.JSON(500, gin.H{"message": err.Error()})
return 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) { wp.Router.NoRoute(func(c *gin.Context) {