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 {
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 || {
params
route_params
.get()
.expect("gets wall_uid from URL")
.wall_uid
@ -44,20 +47,38 @@ pub fn Wall() -> impl IntoView {
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| {
problem_action.value().write_only().set(None);
match wall.get() {
match &*wall.read() {
Some(Ok(wall)) => {
if let Some(problem_uid) = wall.random_problem() {
tracing::debug!("dispatching from effect");
problem_action.dispatch((wall.uid, problem_uid));
if problem_uid.get().is_none() {
tracing::debug!("Setting next problem");
fn_next_problem(wall);
}
}
Some(Err(_err)) => {}
Some(Err(err)) => {
tracing::error!("Error getting wall: {err}");
}
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 {
left: vec![],
middle: vec![HeaderItem {
@ -93,12 +114,7 @@ pub fn Wall() -> impl IntoView {
<div>
<Button
onclick=move |_| {
if let Some(problem_uid) = wall.random_problem() {
tracing::info!("dispatching from button click handler");
problem_action.dispatch((wall.uid, problem_uid));
}
}
onclick=move |_| fn_next_problem(&wall)
text="➤ Next problem"
/>