problem UID in query params

This commit is contained in:
Asger Juul Brunshøj 2025-02-25 23:38:01 +01:00
parent fc96105091
commit 91430f8985

View File

@ -22,9 +22,12 @@ struct RouteParams {
pub fn Wall() -> impl IntoView { pub fn Wall() -> impl IntoView {
tracing::debug!("Enter"); tracing::debug!("Enter");
let params = leptos_router::hooks::use_params::<RouteParams>(); let route_params = leptos_router::hooks::use_params::<RouteParams>();
let (problem_uid, set_problem_uid) = leptos_router::hooks::query_signal::<models::ProblemUid>("problem");
let wall_uid = Signal::derive(move || { let wall_uid = Signal::derive(move || {
params route_params
.get() .get()
.expect("gets wall_uid from URL") .expect("gets wall_uid from URL")
.wall_uid .wall_uid
@ -44,20 +47,38 @@ pub fn Wall() -> impl IntoView {
v.and_then(Result::ok) v.and_then(Result::ok)
}); });
let fn_next_problem = move |wall: &models::Wall| {
set_problem_uid.set(wall.random_problem());
};
// Set a problem when wall is set (loaded)
Effect::new(move |_prev_value| { Effect::new(move |_prev_value| {
problem_action.value().write_only().set(None); problem_action.value().write_only().set(None);
match wall.get() {
match &*wall.read() {
Some(Ok(wall)) => { Some(Ok(wall)) => {
if let Some(problem_uid) = wall.random_problem() { if problem_uid.get().is_none() {
tracing::debug!("dispatching from effect"); tracing::debug!("Setting next problem");
problem_action.dispatch((wall.uid, problem_uid)); fn_next_problem(wall);
} }
} }
Some(Err(_err)) => {} Some(Err(err)) => {
tracing::error!("Error getting wall: {err}");
}
None => {} None => {}
} }
}); });
// On change of problem UID, dispatch an action to fetch the problem
Effect::new(move |_prev_value| match problem_uid.get() {
Some(problem_uid) => {
problem_action.dispatch((wall_uid.get(), problem_uid));
}
None => {
problem_action.value().write_only().set(None);
}
});
let header_items = move || HeaderItems { let header_items = move || HeaderItems {
left: vec![], left: vec![],
middle: vec![HeaderItem { middle: vec![HeaderItem {
@ -93,12 +114,7 @@ pub fn Wall() -> impl IntoView {
<div> <div>
<Button <Button
onclick=move |_| { onclick=move |_| fn_next_problem(&wall)
if let Some(problem_uid) = wall.random_problem() {
tracing::info!("dispatching from button click handler");
problem_action.dispatch((wall.uid, problem_uid));
}
}
text="➤ Next problem" text="➤ Next problem"
/> />