refactor wall

This commit is contained in:
2025-02-10 22:33:17 +01:00
parent 43bf7d863d
commit 92d9184723
7 changed files with 81 additions and 83 deletions

View File

@@ -25,7 +25,7 @@ pub fn shell(options: LeptosOptions) -> impl IntoView {
}
#[component]
pub fn App() -> impl leptos::IntoView {
pub fn App() -> impl IntoView {
use leptos_meta::Stylesheet;
use leptos_meta::Title;
@@ -51,7 +51,7 @@ pub fn App() -> impl leptos::IntoView {
}
#[component]
pub fn Home() -> impl leptos::IntoView {
pub fn Home() -> impl IntoView {
// TODO: show cards with walls, and a "new wall" button
tracing::debug!("Rendering home component");

View File

@@ -67,6 +67,11 @@ pub fn hydrate() {
console_error_panic_hook::set_once();
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::DEBUG.into())
.from_env_lossy(),
)
.with_writer(
// To avoide trace events in the browser from showing their JS backtrace
tracing_subscriber_wasm::MakeConsoleWriter::default().map_trace_level_to(tracing::Level::DEBUG),

View File

@@ -21,7 +21,7 @@ struct RouteParams {
}
#[component]
pub fn EditWall() -> impl leptos::IntoView {
pub fn EditWall() -> impl IntoView {
let params = leptos_router::hooks::use_params::<RouteParams>();
let wall_uid = Signal::derive(move || {
params
@@ -73,7 +73,7 @@ pub fn EditWall() -> impl leptos::IntoView {
}
#[component]
fn Ready(wall: models::Wall) -> impl leptos::IntoView {
fn Ready(wall: models::Wall) -> impl IntoView {
tracing::debug!("ready");
let mut holds = vec![];
@@ -92,7 +92,7 @@ fn Ready(wall: models::Wall) -> impl leptos::IntoView {
}
#[component]
fn Hold(hold: models::Hold) -> impl leptos::IntoView {
fn Hold(hold: models::Hold) -> impl IntoView {
let hold_position = hold.position;
let file_input_ref = NodeRef::<Input>::new();

View File

@@ -16,7 +16,7 @@ struct RouteParams {
#[component]
#[tracing::instrument(skip_all)]
pub fn Routes() -> impl leptos::IntoView {
pub fn Routes() -> impl IntoView {
tracing::debug!("Enter");
let params = leptos_router::hooks::use_params::<RouteParams>();

View File

@@ -1,4 +1,3 @@
use crate::codec::ron::Ron;
use crate::codec::ron::RonEncoded;
use crate::components::button::Button;
use crate::components::header::HeaderItem;
@@ -31,34 +30,31 @@ pub fn Wall() -> impl IntoView {
let wall = crate::resources::wall_by_uid(wall_uid);
let (problem_uid, set_problem_uid) = signal(None);
let mut init_problem = false;
Effect::new(move || {
if !init_problem {
if let Some(Ok(wall)) = &*wall.read() {
set_problem_uid.set(wall.random_problem());
init_problem = true;
}
}
});
let problem: Resource<Option<models::Problem>, Ron> = Resource::new_with_options(
move || (wall_uid.get(), problem_uid.get()),
move |(wall_uid, problem_uid)| async move {
let Some(problem_uid) = problem_uid else {
return None;
};
let problem_action = Action::new(move |&(wall_uid, problem_uid): &(models::WallUid, models::ProblemUid)| async move {
tracing::info!("fetching");
crate::server_functions::get_problem(wall_uid, problem_uid)
.await
.map(RonEncoded::into_inner)
.inspect_err(|err| {
tracing::error!("{err}");
})
.ok()
},
false,
);
});
let problem_signal = Signal::derive(move || {
let v = problem_action.value().read_only().get();
tracing::debug!("val: {:?}", v);
v.and_then(Result::ok)
});
Effect::new(move |_prev_value| {
problem_action.value().write_only().set(None);
match wall.get() {
Some(Ok(wall)) => {
if let Some(problem_uid) = wall.random_problem() {
tracing::debug!("dispatching from effect");
problem_action.dispatch((wall.uid, problem_uid));
}
}
Some(Err(_err)) => {}
None => {}
}
});
let header_items = move || HeaderItems {
left: vec![],
@@ -83,59 +79,34 @@ pub fn Wall() -> impl IntoView {
<StyledHeader items=Signal::derive(header_items) />
<div class="m-2">
<Suspense fallback=move || {
view! { <p>"Loading..."</p> }
}>
<Suspense fallback=move || view! { <p>"Loading..."</p> } >
{move || Suspend::new(async move {
let wall = wall.await;
let problem = problem.await;
tracing::info!("executing Suspend future");
let wall = wall.await?;
wall.map(move |wall| {
let mut cells = vec![];
for (&hold_position, hold) in &wall.holds {
let problem = problem.clone();
let role = move || {
problem
.clone()
.and_then(|problem| {
problem.holds.get(&hold_position).copied()
})
};
let role = Signal::derive(role);
let cell = view! { <Hold role hold=hold.clone() /> };
cells.push(cell);
}
let grid_classes = format!(
"grid grid-rows-{} grid-cols-{} gap-3",
wall.rows,
wall.cols,
);
view! {
let v = view! {
<div class="grid grid-cols-[auto,1fr] gap-8">
// Render the wall
<div
style="max-height: 90vh; max-width: 90vh;"
class=move || { grid_classes.clone() }
>
{cells}
</div>
<Grid wall=wall.clone() problem=problem_signal />
<div>
// TODO:
// <p>{current_problem.read().as_ref().map(|p| p.name.clone())}</p>
// <p>{current_problem.read().as_ref().map(|p| p.set_by.clone())}</p>
<div></div>
<div>
<p>{move || problem_signal.get().map(|p| p.name.clone())}</p>
<p>{move || problem_signal.get().map(|p| p.set_by.clone())}</p>
</div>
<Button
onclick=move |_| {
set_problem_uid.set(wall.random_problem());
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"
/>
</div>
</div>
}
})
};
Ok::<_, ServerFnError>(v)
})}
</Suspense>
</div>
@@ -145,7 +116,30 @@ pub fn Wall() -> impl IntoView {
#[component]
#[tracing::instrument(skip_all)]
fn Hold(hold: models::Hold, #[prop(into)] role: Signal<Option<HoldRole>>) -> impl leptos::IntoView {
fn Grid(wall: models::Wall, problem: Signal<Option<models::Problem>>) -> impl IntoView {
tracing::debug!("Enter");
let mut cells = vec![];
for (&hold_position, hold) in &wall.holds {
let role = move || problem.get().and_then(|p| p.holds.get(&hold_position).copied());
let role = Signal::derive(role);
let cell = view! { <Hold role hold=hold.clone() /> };
cells.push(cell);
}
let grid_classes = format!("grid grid-rows-{} grid-cols-{} gap-3", wall.rows, wall.cols,);
view! {
<div class="grid grid-cols-[auto,1fr] gap-8">
<div style="max-height: 90vh; max-width: 90vh;" class=move || { grid_classes.clone() }>
{cells}
</div>
</div>
}
}
#[component]
#[tracing::instrument(skip_all)]
fn Hold(hold: models::Hold, role: Signal<Option<HoldRole>>) -> impl IntoView {
tracing::trace!("Enter");
let class = move || {
let role_classes = match role.get() {

View File

@@ -63,7 +63,6 @@ async fn serve(cli: Cli) -> Result<(), Error> {
let leptos_options = leptos_conf_file.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);
dbg!(&routes);
let config = load_config(cli)?;

View File

@@ -9,7 +9,7 @@ fmt:
bc-fmt
serve:
RUST_LOG=debug RUST_BACKTRACE=1 cargo leptos watch -- serve
RUST_BACKTRACE=1 cargo leptos watch -- serve
build-release:
rm -rf dist