Refactor toggle_category into (dis)activate
This commit is contained in:
@ -35,7 +35,8 @@ fn main() -> io::Result<()> {
|
|||||||
routes::index,
|
routes::index,
|
||||||
routes::history,
|
routes::history,
|
||||||
routes::create_category,
|
routes::create_category,
|
||||||
routes::toggle_category,
|
routes::activate_category,
|
||||||
|
routes::deactivate_category,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -32,8 +32,18 @@ pub fn create_category(category: Form<NewCategory>, db: State<'_, sled::Db>) ->
|
|||||||
Ok(Redirect::to(uri!(index)))
|
Ok(Redirect::to(uri!(index)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/toggle_category/<category_uuid>")]
|
|
||||||
pub fn toggle_category(category_uuid: String, db: State<'_, sled::Db>) -> Result<Redirect, StatusJson> {
|
#[post("/set_category/<category_uuid>/active")]
|
||||||
|
pub fn activate_category(category_uuid: String, db: State<'_, sled::Db>) -> Result<StatusJson, StatusJson> {
|
||||||
|
toggle_category(category_uuid, true, db)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/set_category/<category_uuid>/inactive")]
|
||||||
|
pub fn deactivate_category(category_uuid: String, db: State<'_, sled::Db>) -> Result<StatusJson, StatusJson> {
|
||||||
|
toggle_category(category_uuid, false, db)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_category(category_uuid: String, set_active: bool, db: State<'_, sled::Db>) -> Result<StatusJson, StatusJson> {
|
||||||
let category_uuid = Uuid::parse_str(&category_uuid)?;
|
let category_uuid = Uuid::parse_str(&category_uuid)?;
|
||||||
let category_uuid_s = sled::IVec::from(serialize(&category_uuid)?);
|
let category_uuid_s = sled::IVec::from(serialize(&category_uuid)?);
|
||||||
|
|
||||||
@ -49,8 +59,8 @@ pub fn toggle_category(category_uuid: String, db: State<'_, sled::Db>) -> Result
|
|||||||
let mut category: categories::V = dbg!(deserialize(&data).unwrap());
|
let mut category: categories::V = dbg!(deserialize(&data).unwrap());
|
||||||
let now = Utc::now().naive_utc();
|
let now = Utc::now().naive_utc();
|
||||||
|
|
||||||
match category.started.take() {
|
match (set_active, category.started.take()) {
|
||||||
Some(started) => {
|
(false, Some(started)) => {
|
||||||
// only save sessions longer than 5 minutes
|
// only save sessions longer than 5 minutes
|
||||||
let duration = now - started;
|
let duration = now - started;
|
||||||
if duration > Duration::minutes(5) {
|
if duration > Duration::minutes(5) {
|
||||||
@ -63,16 +73,20 @@ pub fn toggle_category(category_uuid: String, db: State<'_, sled::Db>) -> Result
|
|||||||
tx_past_sessions.insert(session_uuid, serialize(&past_session).unwrap())?;
|
tx_past_sessions.insert(session_uuid, serialize(&past_session).unwrap())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
(true, None) => {
|
||||||
category.started = Some(now);
|
category.started = Some(now);
|
||||||
}
|
}
|
||||||
|
_ => {
|
||||||
|
// Category is already in the correct state
|
||||||
|
return Ok(Ok(Status::Ok.into()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tx_categories.insert(&category_uuid_s, serialize(&category).unwrap())?;
|
tx_categories.insert(&category_uuid_s, serialize(&category).unwrap())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Ok(Redirect::to(uri!(index))))
|
Ok(Ok(Status::Ok.into()))
|
||||||
})??)
|
})??)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,21 @@
|
|||||||
<title>stl</title>
|
<title>stl</title>
|
||||||
<script>
|
<script>
|
||||||
function toggle_category(id) {
|
function toggle_category(id) {
|
||||||
var url = "/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 active = cl.contains(toggled_class);
|
||||||
|
|
||||||
|
// Get the corresponding route to activate/inactivate the category
|
||||||
|
let url;
|
||||||
|
if(active) {
|
||||||
|
url = "/set_category/" + id + "/inactive";
|
||||||
|
cl.remove(toggled_class);
|
||||||
|
} else {
|
||||||
|
url = "/set_category/" + id + "/active";
|
||||||
|
cl.add(toggled_class);
|
||||||
|
}
|
||||||
|
|
||||||
//var params = "lorem=ipsum&name=alpha";
|
//var params = "lorem=ipsum&name=alpha";
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open("POST", url, true);
|
xhr.open("POST", url, true);
|
||||||
@ -21,14 +35,6 @@
|
|||||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
xhr.send();
|
xhr.send();
|
||||||
|
|
||||||
let toggled_class = "category_button_toggled";
|
|
||||||
let cl = document.getElementById("button-" + id).classList;
|
|
||||||
if(cl.contains(toggled_class)) {
|
|
||||||
cl.remove(toggled_class);
|
|
||||||
} else {
|
|
||||||
cl.add(toggled_class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Reference in New Issue
Block a user