From 1603dc0736bed2d47024712183a1af697efc1498 Mon Sep 17 00:00:00 2001 From: Bekir Pehlivan Date: Fri, 20 Oct 2023 10:15:50 +0300 Subject: [PATCH] user table refresh --- controller/sync_controller.go | 18 ++++++++ internal/cancel.go | 12 ++++++ internal/sync_loop.go | 4 +- templates/index.html | 21 +++++++-- templates/table.html | 2 +- templates/tbody.html | 81 +++++++++++++++++++++++------------ 6 files changed, 104 insertions(+), 34 deletions(-) create mode 100644 internal/cancel.go diff --git a/controller/sync_controller.go b/controller/sync_controller.go index bfcc9a2..7561733 100644 --- a/controller/sync_controller.go +++ b/controller/sync_controller.go @@ -12,6 +12,9 @@ func HandleSync(ctx *gin.Context) { if ctx.Request.FormValue("retry") != "" { handleRetry(ctx) return + } else if ctx.Request.FormValue("cancel") != "" { + handleCancel(ctx) + return } sourceServer := ctx.PostForm("source_server") @@ -47,6 +50,21 @@ func HandleSync(ctx *gin.Context) { ctx.HTML(200, "sync_success.html", creds) } +func handleCancel(ctx *gin.Context) { + id := ctx.Request.FormValue("cancel") + + val, err := strconv.Atoi(id) + if err != nil { + log.Errorf("Error converting %s to int", id) + } + + task := internal.GetTaskFromID(val) + + internal.CancelTask(task) + + log.Infof("%#v", task) +} + func handleRetry(ctx *gin.Context) { id := ctx.Request.FormValue("retry") diff --git a/internal/cancel.go b/internal/cancel.go new file mode 100644 index 0000000..ea8782a --- /dev/null +++ b/internal/cancel.go @@ -0,0 +1,12 @@ +package internal + +func CancelTask(task *Task) { + if task.Status != "In Progress" { + updateTaskStatus(task, "Cancelled") + log.Debug(len(taskChan)) + _ = <-taskChan + log.Debug(len(taskChan)) + return + } + +} diff --git a/internal/sync_loop.go b/internal/sync_loop.go index c59c7c3..3e51cee 100644 --- a/internal/sync_loop.go +++ b/internal/sync_loop.go @@ -16,8 +16,8 @@ func processPendingTasks() { continue } - syncIMAP(task) - // simulateTask(task) + // syncIMAP(task) + simulateTask(task) time.Sleep(100 * time.Millisecond) } } diff --git a/templates/index.html b/templates/index.html index 9825843..b6c3015 100644 --- a/templates/index.html +++ b/templates/index.html @@ -111,6 +111,10 @@

Destination Details

+ +
@@ -209,10 +213,9 @@

User queue

toggleDark.addEventListener("click", darkMode, false); toggleLight.addEventListener("click", lightMode, false); - - document.addEventListener('alpine:init', () => { Alpine.data('data', () => ({ + model_open: '', source_server: '', source_account: '', destination_server: '', @@ -220,11 +223,19 @@

User queue

source_validated: false, destination_validated: false, + showQueue(){ if (this.source_validated === true && this.destination_validated === true) { htmx.ajax('POST', "/api/search?exact=true&source_server="+this.source_server+"&source_account="+this.source_account+"&destination_server="+this.destination_server+"&destination_account="+this.destination_account+"", {target:'#table-body',swap:'innerHTML'}) } }, + + init() { + setInterval(() => { + this.showQueue(); + }, 2000); + }, + update_source_status(result) { this.source_validated = result; this.showQueue(); @@ -233,9 +244,13 @@

User queue

this.destination_validated = result; this.showQueue(); }, - })) + } + + )) + }) + \ No newline at end of file diff --git a/templates/table.html b/templates/table.html index 85cc82c..161485a 100644 --- a/templates/table.html +++ b/templates/table.html @@ -10,6 +10,6 @@ + hx-swap="innerHTML transition:true" hx-trigger="every 2s"> {{template "tbody.html" .}} \ No newline at end of file diff --git a/templates/tbody.html b/templates/tbody.html index 264e6f9..84513f4 100644 --- a/templates/tbody.html +++ b/templates/tbody.html @@ -1,31 +1,56 @@ {{range .Tasks}} - {{.ID}} - {{.SourceServer}} - {{.SourceAccount}} - {{.DestinationServer}} - {{.DestinationAccount}} - - {{if eq .Status "Done"}} - {{.Status}} - {{else if eq .Status "Error" "Cancelled" }} - {{.Status}} - {{else}} - {{.Status}} - {{end}} - - - {{if (eq .Status "Done" "Error" "Cancelled")}} -
- - - -
- {{else}} - - {{end}} - + {{.ID}} + {{.SourceServer}} + {{.SourceAccount}} + {{.DestinationServer}} + {{.DestinationAccount}} + + {{if eq .Status "Done"}} + {{.Status}} + {{else if eq .Status "Error" "Cancelled" }} + {{.Status}} + {{else}} + {{.Status}} + {{end}} + + + {{if (eq .Status "Done" "Error" "Cancelled")}} +
+ + + +
+ {{else}} + + {{end}} + -{{end}} \ No newline at end of file +{{end}}