89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.duckylabs.xyz/duckbox/ducky-dash/db"
|
|
"git.duckylabs.xyz/duckbox/ducky-dash/models"
|
|
"git.duckylabs.xyz/duckbox/ducky-dash/view/tasks"
|
|
"git.duckylabs.xyz/duckbox/ducky-dash/view/ui"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func HandleTaskIndex(w http.ResponseWriter, r *http.Request) error {
|
|
accounts, err := db.GetAllAccounts() // CreateTaskModal
|
|
user := getAuthenticatedUser(r)
|
|
if err != nil {
|
|
slog.Error("error retrieving accounts", err)
|
|
return err
|
|
}
|
|
// tasks_list := []models.Task{}
|
|
tasks_list, err := db.GetUserTasks(user)
|
|
// return tasks.TasksViewIndex(accounts, tasks_list).Render(r.Context(), w)
|
|
return render(r, w, tasks.TasksViewIndex(accounts, tasks_list))
|
|
}
|
|
|
|
func HandleTaskCreateModal(w http.ResponseWriter, r *http.Request) error {
|
|
accounts, err := db.GetAllAccounts() // CreateTaskModal
|
|
task := models.Task{}
|
|
task.Title = r.FormValue("title")
|
|
task.Description = r.FormValue("description")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tasks.CreateTaskModal(accounts).Render(r.Context(), w)
|
|
}
|
|
|
|
func HandleTaskCreate(w http.ResponseWriter, r *http.Request) error {
|
|
// TODO: Implement some form of validation on tasks
|
|
user := getAuthenticatedUser(r)
|
|
account := models.Account{
|
|
UserID: user.ID,
|
|
Username: user.Username,
|
|
}
|
|
completed := false
|
|
if r.FormValue("is_completed") == "on" {
|
|
completed = true
|
|
}
|
|
assignee := uuid.MustParse(r.FormValue("assignee"))
|
|
task := models.Task{
|
|
Title: r.FormValue("title"),
|
|
Description: r.FormValue("description"),
|
|
AuthorID: account.UserID,
|
|
Completed: completed,
|
|
Assignee: assignee,
|
|
// TODO: Show a list of users from the org for the author to choose from
|
|
|
|
}
|
|
if task.Description == "" {
|
|
w.Header().Add("HX-Retarget", "#hidden-toast")
|
|
w.Header().Add("HX-Reswap", "outerHTML")
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
return render(r, w, ui.ToastError("Please enter a description"))
|
|
}
|
|
if task.Title == "" {
|
|
w.Header().Add("HX-Retarget", "#hidden-toast")
|
|
w.Header().Add("HX-Reswap", "outerHTML")
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
return render(r, w, ui.ToastError("Please enter a task name"))
|
|
}
|
|
// TODO: When a task is submitted, if it is not complete return a CreateTaskModal pre-filled with the information the user had already filled out
|
|
|
|
if err := db.CreateTask(&task); err != nil {
|
|
w.Header().Add("HX-Retarget", "#hidden-toast")
|
|
w.Header().Add("HX-Reswap", "outerHTML")
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
return render(r, w, ui.ToastError("Error creating task on the server..."))
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
return render(r, w, tasks.TaskRowItem(task))
|
|
|
|
}
|