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

@ -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>