diff --git a/src/routes/pages.rs b/src/routes/pages.rs index fd39c3e..8d2df6a 100644 --- a/src/routes/pages.rs +++ b/src/routes/pages.rs @@ -46,6 +46,7 @@ pub fn router() -> Router { get(entry_row).put(update_entry).delete(delete_entry), ) .route("/ui/entries/{id}/edit", get(entry_edit)) + .route("/ui/entries/{id}/relancer", post(restart_entry)) .route( "/ui/entries/{id}/dupliquer", get(entry_duplicate).post(duplicate_entry), @@ -99,6 +100,16 @@ fn with_entries_changed(resp: Response) -> Response { (AppendHeaders([("HX-Trigger", "entries-changed")]), resp).into_response() } +/// Same, plus `timer-changed` so the dashboard timer follows a start/stop +/// made elsewhere (header, entry lists). +fn with_timer_changed(resp: Response) -> Response { + ( + AppendHeaders([("HX-Trigger", "entries-changed, timer-changed")]), + resp, + ) + .into_response() +} + struct ProjectOption { id: i64, name: String, @@ -363,13 +374,7 @@ async fn stop_timer(State(pool): State) -> PageResult { Ok(_) | Err(AppError::Conflict(_)) => {} Err(e) => return Err(e.into()), } - // `timer-changed` refreshes the dashboard timer when stopped from the header. - let resp = render(timer_state(&pool).await?)?; - Ok(( - AppendHeaders([("HX-Trigger", "entries-changed, timer-changed")]), - resp, - ) - .into_response()) + Ok(with_timer_changed(render(timer_state(&pool).await?)?)) } #[derive(Deserialize)] @@ -727,6 +732,13 @@ async fn update_entry( Ok(with_entries_changed(render(EntryRowTpl { e })?)) } +/// Stops the running timer and starts a new one on this entry's project and note. +async fn restart_entry(State(pool): State, Path(id): Path) -> PageResult { + let e = entries::get(&pool, id).await?; + entries::start(&pool, e.project_id, &e.note).await?; + Ok(with_timer_changed(StatusCode::OK.into_response())) +} + async fn delete_entry(State(pool): State, Path(id): Path) -> PageResult { entries::delete(&pool, id).await?; // htmx swaps the row with this empty body, removing it. diff --git a/static/app.css b/static/app.css index 119464d..e0ec7dc 100644 --- a/static/app.css +++ b/static/app.css @@ -99,6 +99,7 @@ a.btn { color: var(--text); text-decoration: none; } .btn:hover { border-color: var(--muted); } .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; } .btn.danger { color: var(--danger); } +.btn.play { color: var(--accent); } .btn.small { padding: .2rem .55rem; font-size: .85rem; } .btn.icon { display: inline-flex; align-items: center; justify-content: center; flex: none; diff --git a/templates/_entry_row.html b/templates/_entry_row.html index 52efb75..933acee 100644 --- a/templates/_entry_row.html +++ b/templates/_entry_row.html @@ -9,6 +9,11 @@ {{ e.duration() }}
+ {% if e.running() %} + + {% else %} + + {% endif %} diff --git a/templates/_icons.html b/templates/_icons.html index e6251d3..2021988 100644 --- a/templates/_icons.html +++ b/templates/_icons.html @@ -6,6 +6,7 @@ {%- else if name == "check" -%} {%- else if name == "plus" -%} {%- else if name == "reset" -%} +{%- else if name == "play" -%} {%- else if name == "stop" -%} {%- else if name == "link" -%} {%- else if name == "archive" -%} diff --git a/tests/api.rs b/tests/api.rs index d205765..f758a63 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -802,3 +802,46 @@ async fn header_mini_timer() { let (_, html) = get_html(&app, "/ui/timer/entete").await; assert!(html.contains("Site web")); } + +#[tokio::test] +async fn restart_entry_from_list() { + let app = app().await; + let a = project(&app, "Acme", "Site web").await; + let b = project(&app, "Globex", "API").await; + add_entry(&app, a, local(2026, 3, 10, 9, 0), local(2026, 3, 10, 10, 0)).await; + send( + &app, + "PATCH", + "/api/entries/1", + Some(json!({ "note": "réunion" })), + ) + .await; + send( + &app, + "POST", + "/api/timer/start", + Some(json!({ "project_id": b })), + ) + .await; + + // ▶ on entry 1: the Globex timer stops, a new one runs on Acme / "réunion". + let (status, _) = send_form(&app, "/ui/entries/1/relancer", "").await; + assert_eq!(status, StatusCode::OK); + let (_, running) = send(&app, "GET", "/api/timer", None).await; + assert_eq!(running["project_id"], a); + assert_eq!(running["note"], "réunion"); + let (_, list) = send( + &app, + "GET", + "/api/entries?from=2020-01-01&to=2100-01-01", + None, + ) + .await; + let open = list + .as_array() + .unwrap() + .iter() + .filter(|e| e["ended_at"].is_null()) + .count(); + assert_eq!(open, 1); +}