diff options
author | Julian Jørgensen <julian@jtle.dk> | 2024-05-25 11:53:43 +0200 |
---|---|---|
committer | Julian Jørgensen <julian@jtle.dk> | 2024-05-25 11:53:43 +0200 |
commit | 2610f9b67e4a915859c029da9b17e9d262b6564e (patch) | |
tree | 4504bf2d995703f583fff199b1ae7e54eef4e432 /tidsreg.go | |
parent | 6cdd02af0684500b5376d4d4a08feb796fdaf889 (diff) |
Add task ui and editingold
Diffstat (limited to 'tidsreg.go')
-rw-r--r-- | tidsreg.go | 92 |
1 files changed, 85 insertions, 7 deletions
@@ -18,7 +18,46 @@ type Server struct { srv *Service } -func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) { +func parseTaskFromForm(r *http.Request) (*Task, error) { + err := r.ParseForm() + if err != nil { + return nil, err + } + + var id int64 + if r.PostFormValue("id") == "" { + id = -1 + } else { + id, err = strconv.ParseInt(r.PostFormValue("id"), 10, 32) + if err != nil { + return nil, err + } + } + + var group *string + { + groupStr := r.PostFormValue("group") + if groupStr != "" { + group = &groupStr + } + } + var ident *string + { + identStr := r.PostFormValue("ident") + if identStr != "" { + ident = &identStr + } + } + + return &Task{ + Id: int(id), + Group: group, + Ident: ident, + Text: r.PostFormValue("text"), + }, nil +} + +func parseEntryFromForm(r *http.Request, useToday bool) (*Entry, error) { err := r.ParseForm() if err != nil { return nil, err @@ -59,7 +98,7 @@ func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) { task := &Entry{ Id: int(id), From: from, - Date: date, + Date: date, To: to, Comment: r.PostFormValue("comment"), } @@ -67,7 +106,7 @@ func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) { } func (s *Server) renderTemplate(w http.ResponseWriter, name string, page interface{}) { - tmpl, err := template.ParseFiles("templates/index.html", "templates/parts/entry.html", "templates/parts/entryRows.html") + tmpl, err := template.ParseFiles("templates/index.html", "templates/parts/entry.html", "templates/parts/entryRows.html", "templates/parts/task.html", "templates/parts/taskRows.html") if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -127,7 +166,7 @@ func (s *Server) rootHandle(w http.ResponseWriter, r *http.Request) { func (s *Server) postStart(w http.ResponseWriter, r *http.Request) { now := time.Now() - task, err := parseTaskFromForm(r, true) + task, err := parseEntryFromForm(r, true) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) return @@ -150,7 +189,7 @@ func (s *Server) postStart(w http.ResponseWriter, r *http.Request) { func (s *Server) postStop(w http.ResponseWriter, r *http.Request) { now := time.Now() - task, err := parseTaskFromForm(r, true) + task, err := parseEntryFromForm(r, true) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) return @@ -183,7 +222,7 @@ func (s *Server) getTracking(w http.ResponseWriter, _ *http.Request) { } func (s *Server) putSave(w http.ResponseWriter, r *http.Request) { - task, err := parseTaskFromForm(r, false) + task, err := parseEntryFromForm(r, false) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) return @@ -280,7 +319,44 @@ func (s *Server) getTasksList(w http.ResponseWriter, _ *http.Request) { return } - s.renderTemplate(w, "tasksPage.html", page) + s.renderTemplate(w, "taskRows.html", page) +} + +func (s *Server) postTaskSave(w http.ResponseWriter, r *http.Request) { + task, err := parseTaskFromForm(r) + if err != nil { + writeError(w, err.Error(), http.StatusBadRequest) + return + } + + task, err = s.srv.Db.SaveTask(task) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + log.Printf("Hej %+v\n", task) + + page, err := s.srv.GetTaskPage(nil, task) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Add("HX-Trigger", "changedTasks") + + s.renderTemplate(w, "task.html", page) +} + +func (s *Server) getTaskEmpty(w http.ResponseWriter, _ *http.Request) { + task := NewTask() + + page, err := s.srv.GetTaskPage(nil, task) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + + s.renderTemplate(w, "task.html", page) } func main() { @@ -300,6 +376,8 @@ func main() { r.HandleFunc("/entryRows", s.getEntries).Methods("GET") r.HandleFunc("/edit", s.getEdit).Methods("GET") r.HandleFunc("/task/list", s.getTasksList).Methods("GET") + r.HandleFunc("/task/save", s.postTaskSave).Methods("POST") + r.HandleFunc("/task/empty", s.getTaskEmpty).Methods("GET") r.HandleFunc("/", s.rootHandle) http.Handle("/", r) |