Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added loading bar when adding teams or refreshing data from TBA #181

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions templates/setup_teams.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{define "body"}}
{{if .ShowErrorMessage}}
<div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<button type="button" class="close" data-bs-dismiss="alert">×</button>
You can't modify the team list once the qualification schedule has been generated. If you need to change
the team list, clear all other data first on the Settings page.
</div>
Expand All @@ -24,11 +24,11 @@
placeholder="One team number per line"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Teams</button>
<button type="submit" class="btn btn-info" onclick="$('#loadingFromTBA').modal('show');">Add Teams</button>
</div>
{{if .EventSettings.TbaDownloadEnabled}}
<div class="form-group">
<a href="/setup/teams/refresh" class="btn btn-info">Refresh Team Data from TBA</a>
<a href="/setup/teams/refresh" class="btn btn-info" onclick="$('#loadingFromTBA').modal('show');">Refresh Team Data from TBA</a>
</div>
{{end}}
<div class="form-group">
Expand Down Expand Up @@ -96,15 +96,15 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<button type="button" class="close" data-bs-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Confirm</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to clear the team list?</p>
</div>
<div class="modal-footer">
<form class="form-horizontal" action="/setup/teams/clear" method="POST">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Clear Team List</button>
</form>
</div>
Expand All @@ -115,7 +115,7 @@ <h4 class="modal-title">Confirm</h4>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<button type="button" class="close" data-bs-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Confirm</h4>
</div>
<div class="modal-body">
Expand All @@ -124,12 +124,51 @@ <h4 class="modal-title">Confirm</h4>
</div>
<div class="modal-footer">
<form class="form-horizontal" action="/setup/teams/publish" method="POST">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Publish Team List</button>
</form>
</div>
</div>
</div>
</div>
<div id="loadingFromTBA" class="modal fade" style="top: 20%;" data-bs-backdrop="static" data-bs-keyboard="false">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Loading Teams<h4>
</div>
<div class="modal-body">
<div class="progress">
<div id="loadProgressBar"
class="progress-bar progress-bar-striped progress-bar-animated"
role="progressbar"
style="width: 10%;"
aria-valuemin="0"
aria-valuemax="100">
5%
</div>
</div>
</div>
</div>
</div>
</div>
{{end}}
{{define "script"}}
<script>
$(function worker() {
$.ajax({
url: '/setup/teams/progress',
success: function(data) {
$("#loadProgressBar").css("width", (data + "%").replace("\n", ""))
$("#loadProgressBar").text(data.replace("\n", "").split(".")[0] + "%")
},
error: function(error) {
console.log(error)
},
complete: function() {
setTimeout(worker, 100)
}
})
})
</script>
{{end}}
{{define "script"}}{{end}}
19 changes: 19 additions & 0 deletions web/setup_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"strconv"
"strings"
"time"
"encoding/json"
)

const wpaKeyLength = 8
var progress float64 = 5

// Shows the team list.
func (web *Web) teamsGetHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -46,6 +48,8 @@ func (web *Web) teamsPostHandler(w http.ResponseWriter, r *http.Request) {
}
}

progInc := 95.00 / float64(len(teamNumbers))

for _, teamNumber := range teamNumbers {
team := model.Team{Id: teamNumber}
if web.arena.EventSettings.TbaDownloadEnabled {
Expand All @@ -58,8 +62,12 @@ func (web *Web) teamsPostHandler(w http.ResponseWriter, r *http.Request) {
handleWebErr(w, err)
return
}

progress += progInc
}
progress = 100
http.Redirect(w, r, "/setup/teams", 303)
progress = 5
}

// Re-downloads the data for all teams from TBA and overwrites any local edits.
Expand All @@ -74,6 +82,8 @@ func (web *Web) teamsRefreshHandler(w http.ResponseWriter, r *http.Request) {
return
}

progInc := 95.00 / float64(len(teams))

for _, team := range teams {
if err = web.populateOfficialTeamInfo(&team); err != nil {
handleWebErr(w, err)
Expand All @@ -83,9 +93,13 @@ func (web *Web) teamsRefreshHandler(w http.ResponseWriter, r *http.Request) {
handleWebErr(w, err)
return
}

progress += progInc
}

progress = 100
http.Redirect(w, r, "/setup/teams", 303)
progress = 5
}

// Clears the team list.
Expand Down Expand Up @@ -312,3 +326,8 @@ func (web *Web) populateOfficialTeamInfo(team *model.Team) error {

return nil
}

// Returns current TBA Load Progress
func (web *Web) checkProgress(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(progress)
}
1 change: 1 addition & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func (web *Web) newHandler() http.Handler {
mux.HandleFunc("POST /setup/teams/clear", web.teamsClearHandler)
mux.HandleFunc("GET /setup/teams/generate_wpa_keys", web.teamsGenerateWpaKeysHandler)
mux.HandleFunc("GET /setup/teams/refresh", web.teamsRefreshHandler)
mux.HandleFunc("GET /setup/teams/progress", web.checkProgress)
return mux
}

Expand Down
Loading