Skip to content

Commit

Permalink
user table refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Oct 20, 2023
1 parent 547fce3 commit 1603dc0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 34 deletions.
18 changes: 18 additions & 0 deletions controller/sync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")

Expand Down
12 changes: 12 additions & 0 deletions internal/cancel.go
Original file line number Diff line number Diff line change
@@ -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
}

}
4 changes: 2 additions & 2 deletions internal/sync_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func processPendingTasks() {
continue
}

syncIMAP(task)
// simulateTask(task)
// syncIMAP(task)
simulateTask(task)
time.Sleep(100 * time.Millisecond)
}
}
Expand Down
21 changes: 18 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ <h2 class="text-2xl font-semibold mb-4">Destination Details</h2>
</div>
</form>

<div class="modal w-screen" id="detailsmodal" :class="model_open ? 'visible opacity-100' : '' ">
{{ template "log_window.html"}}
</div>

<!-- Log output -->
<div class="container mx-auto flex px-4" x-show='source_validated && destination_validated'>
<div class="w-full bg-backgroundSecondary p-4 shadow-md border border-border rounded-lg">
Expand Down Expand Up @@ -209,22 +213,29 @@ <h2 class="text-2xl font-semibold">User queue</h2>
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: '',
destination_account: '',

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();
Expand All @@ -233,9 +244,13 @@ <h2 class="text-2xl font-semibold">User queue</h2>
this.destination_validated = result;
this.showQueue();
},
}))
}

))

})


</script>

</body>
2 changes: 1 addition & 1 deletion templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</tr>
</thead>
<tbody id="table-body" hx-boost="true" hx-get="/api/queue?page={{if .Index}}{{.Index}}{{else}}1{{end}}"
hx-swap="innerHTML transition:true" hx-trigger="every 60s">
hx-swap="innerHTML transition:true" hx-trigger="every 2s">
{{template "tbody.html" .}}
</tbody>
81 changes: 53 additions & 28 deletions templates/tbody.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
{{range .Tasks}}
<tr>
<th>{{.ID}}</th>
<td>{{.SourceServer}}</td>
<td>{{.SourceAccount}}</td>
<td>{{.DestinationServer}}</td>
<td>{{.DestinationAccount}}</td>
<td>
{{if eq .Status "Done"}}
<span class="badge badge-outline-success">{{.Status}}</span>
{{else if eq .Status "Error" "Cancelled" }}
<span class="badge badge-outline-error">{{.Status}}</span>
{{else}}
<span class="badge badge-outline-warning">{{.Status}}</span>
{{end}}
</td>
<td>
{{if (eq .Status "Done" "Error" "Cancelled")}}
<div class="btn-group btn-group-rounded btn-group-scrollable">
<label hx-get="/api/details?id={{.ID}}" hx-target="#detailsmodal" hx-swap="innerHTML" hx-trigger="click"
class="btn btn-solid-primary" for="modal-details">Details</label>
<input class="modal-state" id="modal-details" type="checkbox" x-model="model_open" />
<button hx-get="/api/sync?retry={{.ID}}" hx-target="#error-notification" hx-swap="outerHTML"
class="btn btn-solid-warning">Retry</button>
</div>
{{else}}
<button class="btn btn-solid-error btn-rounded" hx-trigger="none">Cancel</button>
{{end}}
</td>
<th>{{.ID}}</th>
<td>{{.SourceServer}}</td>
<td>{{.SourceAccount}}</td>
<td>{{.DestinationServer}}</td>
<td>{{.DestinationAccount}}</td>
<td>
{{if eq .Status "Done"}}
<span class="badge badge-outline-success">{{.Status}}</span>
{{else if eq .Status "Error" "Cancelled" }}
<span class="badge badge-outline-error">{{.Status}}</span>
{{else}}
<span class="badge badge-outline-warning">{{.Status}}</span>
{{end}}
</td>
<td>
{{if (eq .Status "Done" "Error" "Cancelled")}}
<div class="btn-group btn-group-rounded btn-group-scrollable">
<label
hx-get="/api/details?id={{.ID}}"
hx-target="#detailsmodal"
hx-swap="innerHTML"
hx-trigger="click"
class="btn btn-solid-primary"
for="modal-details"
>Details</label
>
<input
class="modal-state"
id="modal-details"
type="checkbox"
x-model="model_open"
/>
<button
hx-get="/api/sync?retry={{.ID}}"
hx-target="#error-notification"
hx-swap="outerHTML"
class="btn btn-solid-warning"
>
Retry
</button>
</div>
{{else}}
<button
hx-get="/api/sync?cancel={{.ID}}"
hx-target="#error-notification"
hx-swap="outerHTML"
class="btn btn-solid-error btn-rounded"
>
Cancel
</button>
{{end}}
</td>
</tr>
{{end}}
{{end}}

0 comments on commit 1603dc0

Please sign in to comment.