From 1650b22407aa91959251903b172bb06714922dbd Mon Sep 17 00:00:00 2001 From: sto Date: Fri, 24 Apr 2026 09:05:59 +0200 Subject: [PATCH] Prefill public completion form with current timer if present, and allow refresh --- app/views/completions/_form.html.slim | 37 ++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/app/views/completions/_form.html.slim b/app/views/completions/_form.html.slim index 70b9d23..0352010 100644 --- a/app/views/completions/_form.html.slim +++ b/app/views/completions/_form.html.slim @@ -48,19 +48,26 @@ = form.check_box :completed, class: "form-check-input" = form.label :completed javascript: + savetime = ""; + ispatch = #{method == :patch}; completedEl = document.getElementById('completion_completed'); completedEl.addEventListener('change', (e) => { const timeEl = document.getElementById('time'); const missingPiecesEl = document.getElementById('missing_pieces'); const remainingPiecesEl = document.getElementById('remaining_pieces'); + const refreshEl = document.getElementById('refresh'); if (e.target.checked) { - timeEl.value = '#{@completion.display_time_from_start}'; + if (ispatch) timeEl.value = '#{@completion.display_time_from_start}'; + else timeEl.value = savetime; missingPiecesEl.style.display = 'block'; remainingPiecesEl.style.display = 'none'; + if (refreshEl) refreshEl.style.display = ''; } else { + savetime = timeEl.value; timeEl.value = '#{display_time(@contest.duration_seconds)}'; missingPiecesEl.style.display = 'none'; remainingPiecesEl.style.display = 'block'; + if (refreshEl) refreshEl.style.display = 'none'; } }) .row.mb-3 @@ -68,8 +75,32 @@ .form-floating = form.text_field :display_time_from_start, autocomplete: "off", class: "form-control", id: "time" = form.label :display_time_from_start, class: "required" - .form-text - = t("activerecord.attributes.completion.display_time_from_start_description") + - unless method == :patch + .col.mt-2 + a.btn.btn-primary id="refresh" + = t("helpers.buttons.refresh") + .form-text + = t("activerecord.attributes.completion.display_time_from_start_description") + - if @contest.start_time.present? && method != :patch + javascript: + startTime = #{@contest.start_time.present? ? @contest.start_time.to_i : "null"}; + pauseTime = #{@contest.pause_time.present? ? @contest.pause_time.to_i : "null"}; + function updateTime() { + const timeEl = document.getElementById('time'); + if (timeEl) { + if (startTime) { + let s = Math.floor((Date.now() - 1000 * startTime) / 1000); + if (pauseTime) s = Math.floor(pauseTime - startTime); + let ss = s % 60; + let mm = Math.floor(s / 60) % 60; + let hh = Math.floor(s / 3600); + timeEl.value = `${hh}:${mm < 10 ? `0${mm}` : mm}:${ss < 10 ? `0${ss}` : ss}`; + } + } + } + refreshButton = document.getElementById("refresh"); + refreshButton?.addEventListener('click', updateTime); + updateTime(); .row.mb-3 id="missing_pieces" .col .form-floating