Skip to content

Commit

Permalink
Add button to mark the task complete and hide
Browse files Browse the repository at this point in the history
  • Loading branch information
lwileczek committed Apr 16, 2024
1 parent dfcc916 commit ca9ffd7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
33 changes: 33 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,38 @@ func viewTask(w http.ResponseWriter, r *http.Request) {
}
}

func markTaskComplete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(mux.Vars(r)["id"], 10, 64)
if err != nil {
Logr.Error("Could not parse id", "error", err)
fmt.Fprintf(w, "ERROR! Could not get task ID from request")
return
}
if err := toggleStatus(uint32(id)); err != nil {
Logr.Error("Could not mark task complete", "error", err, "id", id)
fmt.Fprintf(w, "<p>ERROR! could not mark complete</p>")
return
}

tsk, err := getTasksByID(uint32(id))
if err != nil {
Logr.Error("could not get task by id", "taskID", id, "error", err)
fmt.Fprintf(w, "Error could not get a task with this ID")
return
}

t1 := template.New("status")
t1, err = t1.Parse("<p id='task-status' class='col-start-10'>Status: {{ if . }}Completed{{ else }}Incomplete{{end -}}</p>")
if err != nil {
panic(err)
}

if err = t1.Execute(w, tsk.Status); err != nil {
Logr.Error("Could not execute the template for marking a task complete", "taskID", id, "error", err)
fmt.Fprintf(w, "couldn't execute template")
}
}

func postComment(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(mux.Vars(r)["id"], 10, 64)
if err != nil {
Expand Down Expand Up @@ -255,6 +287,7 @@ func NewServer() *mux.Router {
mux.HandleFunc("/tasks/{id}", viewTask).Methods("GET")
mux.HandleFunc("/tasks/{id}/score", updateScore).Methods("POST")
mux.HandleFunc("/tasks/{id}/comments", postComment).Methods("POST")
mux.HandleFunc("/tasks/{id}/complete", markTaskComplete).Methods("POST")
mux.HandleFunc("/tasks/{id}/images", displayTaskImages).Methods("GET")

//static assets that are generated like CSS and JavaScript
Expand Down
9 changes: 9 additions & 0 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func getAllTasks(limit int16) ([]Task, error) {
FROM
tasks
WHERE
status = false AND
deleted_at IS NULL
ORDER BY
score DESC`
Expand Down Expand Up @@ -160,3 +161,11 @@ func updateTaskScore(id uint32, inc bool) (int32, error) {

return score, nil
}

func toggleStatus(id uint32) error {
ctx := context.Background()
query := "UPDATE tasks SET status = NOT status WHERE id = $1"
_, err := DB.Exec(ctx, query, id)

return err
}
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1 class="text-3xl mb-5">Feedback Requests</h1>
</button>
<div
id="spinner"
class="htmx-indicator h-48 w-48 animate-spin border-white border-t-sky-500 border-[16px] rounded-[50%]"
class="htmx-indicator h-48 w-48 border-white border-t-sky-500 border-[16px] rounded-[50%]"
></div>
</main>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="icon" type="image/ico" href="/static/favicon.ico" />
<link href="/static/main.css" rel="stylesheet">
<link rel="preconnect" href="https://d36yxaw9xj00lp.cloudfront.net" crossorigin>
<script src="https://unpkg.com/[email protected].8"></script>
<script src="https://unpkg.com/[email protected].11"></script>
<title>Task Manager</title>
<style>
@font-face {
Expand Down
10 changes: 9 additions & 1 deletion templates/show-task.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
<h1 class="fontsemi-bold text-2xl mb-5">{{ .Title }}</h1>
<div class="text-sm grid gird-cols-10">
<p class="col-start-1">Requested: {{ .CreatedAt.Format "2006 Jan 02" -}}</p>
<p class="col-start-10">Status: {{ if .Status }}Completed{{ else }}Incomplete{{end -}}</p>
<p id="task-status" class="col-start-10">Status: {{ if .Status }}Completed{{ else }}Incomplete{{end -}}</p>
</div>
<hr>
<p class="text-lg mb-10">{{ .Body }}</p>
</section>
<button
type="submit"
class="col-start-2 h-9 w-32 btn border cursor-pointer border-sky-300 text-slate-50 bg-sky-500 hover:bg-sky-500/75"
hx-post="/tasks/{{ .ID }}/complete"
hx-target="#task-status"
>
Mark Complete
</button>
<div id="imgs"class="flex col-start-2 col-span-4" hx-get="/tasks/{{ .ID }}/images" hx-trigger="load"></div>
<section id="comment-container" class="col-start-2 col-span-4">
<div class="grid grid-cols-10">
Expand Down

0 comments on commit ca9ffd7

Please sign in to comment.