From 1d79676f513115e93593835e6cef87d6a71201eb Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Tue, 3 Nov 2020 01:08:39 +0100 Subject: [PATCH] Add button to add 5 minutes to session start time --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 1 + src/routes/api.rs | 30 ++++++++++++++++++++++++++++++ templates/index.hbs | 9 +++++++-- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7be6e05..57235f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1450,7 +1450,7 @@ checksum = "7345c971d1ef21ffdbd103a75990a15eb03604fc8b8852ca8cb418ee1a099028" [[package]] name = "stl" -version = "0.1.0" +version = "0.1.1" dependencies = [ "bincode", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 2d1b2cd..80b4ed4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "stl" description = "studielogg, pronounced 'stell', aka scuffed toggl" -version = "0.1.0" +version = "0.1.1" authors = ["Joakim Hulthe "] license = "MPL-2.0" edition = "2018" diff --git a/src/main.rs b/src/main.rs index f0bf41c..5df66dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ fn main() -> io::Result<()> { routes::api::create_category, routes::api::start_session, routes::api::end_session, + routes::api::bump_session, routes::api::delete_session, ], ); diff --git a/src/routes/api.rs b/src/routes/api.rs index 98d765d..73800ce 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -52,6 +52,36 @@ pub fn end_session( toggle_category_session(category_uuid, false, db) } +#[post("/category//bump_session/minutes/")] +pub fn bump_session( + category_uuid: String, + minutes: i64, + db: State<'_, sled::Db>, +) -> Result { + let duration = Duration::minutes(minutes); + let category_uuid = Uuid::parse_str(&category_uuid)?; + let category_uuid_s = sled::IVec::from(serialize(&category_uuid)?); + + let categories_tree = db.open_tree(categories::NAME)?; + + Ok((&categories_tree).transaction(|tx_categories| { + match tx_categories.get(&category_uuid_s)? { + None => return Ok(Err(Status::NotFound.into())), + Some(data) => { + let mut category: categories::V = deserialize(&data).unwrap(); + match category.started.as_mut() { + Some(started) => { + *started -= duration; + tx_categories.insert(&category_uuid_s, serialize(&category).unwrap())?; + Ok(Ok(Redirect::to(uri!(pages::index)))) + } + None => return Ok(Err(StatusJson::new(Status::BadRequest, "No active session"))), + } + } + } + })??) +} + pub fn toggle_category_session( category_uuid: String, set_active: bool, diff --git a/templates/index.hbs b/templates/index.hbs index 69d7d3c..4442896 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -14,7 +14,7 @@ function toggle_category(id) { // Find out whether the button is in active (play) or inactive (paused) state let toggled_class = "category_button_toggled"; - let cl = document.getElementById("button-" + id).classList; + let cl = document.getElementById("toggle-button-" + id).classList; let active = cl.contains(toggled_class); // Get the corresponding route to activate/inactivate the category @@ -49,9 +49,14 @@ style="background-color: {{this.1.color}}" > {{this.1.name}} + {{#if this.1.started}} +
+ +
+ {{/if}}