From 71daf48aaaa8a51590fa0033c140d5a13a90791f Mon Sep 17 00:00:00 2001 From: shahrul Date: Mon, 1 Jan 2024 23:02:18 +0800 Subject: [PATCH] cleanup & fix --- src/fragments.rs | 2 +- src/main.rs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/fragments.rs b/src/fragments.rs index a4dcefb..ac80321 100644 --- a/src/fragments.rs +++ b/src/fragments.rs @@ -149,7 +149,7 @@ fn todoapp(filters: &[Filter], todos: &[Todo], checked: bool) -> Markup { section class="todoapp" hx-get="/get-hash" - hx-vals="js:{hash: window.location.hash}" + hx-vals="js:{hash: window.location.hash.slice(2)}" hx-trigger="load" hx-target=".filters" hx-swap="outerHTML" diff --git a/src/main.rs b/src/main.rs index f88545b..92ee33f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,10 +156,24 @@ fn handle_request( .uri() .query() .and_then(|query| extract_query_param(query, "name")); + let filter_hash = _req + .uri() + .query() + .and_then(|query| extract_query_param(query, "hash")); let vector_response; if let Some(name) = filter_name { // call to update_selected Filter::update_selected_by_property(name, &filters, |f| &f.name); + } else { + if let Some(hash) = filter_hash { + if !hash.is_empty() { + Filter::update_selected_by_property(hash, &filters, |f| &f.name); + } else { + Filter::update_selected_by_property("all".to_string(), &filters, |f| { + &f.name + }); + } + } } vector_response = build_str_vector(|filters| filter_bar(filters), &filters); response(200, vector_response) @@ -232,13 +246,9 @@ fn handle_request( if let Some(todo) = todos_lock.iter_mut().find(|t| t.id == todo_id) { if !task.trim().is_empty() { todo.task = task; - let struct_response = build_str_struct(|todo| todo_item(todo), todo); - return response(200, struct_response); - } else { - // return the original string if client try to make it as empty task - let struct_response = build_str_struct(|todo| todo_item(todo), todo); - return response(200, struct_response); } + let struct_response = build_str_struct(|todo| todo_item(todo), todo); + return response(200, struct_response); } } }