Skip to content

Commit

Permalink
use leptos 0.5: no more cx
Browse files Browse the repository at this point in the history
  • Loading branch information
Arunscape committed Aug 17, 2023
1 parent 4b52c7b commit 5fcd6b5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 84 deletions.
59 changes: 28 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions atb-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,30 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
axum = { version = "0.6.4", optional = true }
axum = { version = "0.6", optional = true }
console_error_panic_hook = "0.1"
console_log = "1"
cfg-if = "1"
leptos = { version = "0.4", features = ["nightly"] }
leptos_axum = { version = "0.4", optional = true }
leptos_meta = { version = "0.4", features = ["nightly"] }
leptos_router = { version = "0.4", features = ["nightly"] }
leptos = { version = "0.5.0-beta", features = ["nightly"] }
leptos_axum = { version = "0.5.0-beta", optional = true }
leptos_meta = { version = "0.5.0-beta", features = ["nightly"] }
leptos_router = { version = "0.5.0-beta", features = ["nightly"] }
log = "0.4"
simple_logger = "4"
tower = { version = "0.4.13", optional = true }
tower = { version = "0.4", optional = true }
tower-http = { version = "0.4", features = ["fs"], optional = true }
wasm-bindgen = "=0.2.87"
thiserror = "1.0.38"
tracing = { version = "0.1.37", optional = true }
http = "0.2.8"
thiserror = "1.0"
tracing = { version = "0.1", optional = true }
http = "0.2"
craftping = { version = "0.4", default-features=false, features = [
"async-tokio",
"sync",
] }
tokio = { version = "1.29.0", features = ["macros", "rt-multi-thread", "full", "tracing"], optional = true }
tokio = { version = "1.29", features = ["macros", "rt-multi-thread", "full", "tracing"], optional = true }
serde = {version = "*", features = ["derive"]}
serde_json = "1.0"
gloo-timers = { version = "0.3.0", features=["futures"] }
gloo = "0.10.0"
gloo = "0.10"
wasm-bindgen = "0.2"


[features]
Expand Down
32 changes: 16 additions & 16 deletions atb-web/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use leptos_router::*;
use crate::components::status::McStatusComponent;

#[component]
pub fn App(cx: Scope) -> impl IntoView {
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context(cx);
provide_meta_context();

view! { cx,
view! {
// injects a stylesheet into the document <head>
// id=leptos means cargo-leptos will hot-reload this stylesheet
<Stylesheet id="leptos" href="/pkg/atb-web.css"/>
Expand All @@ -20,10 +20,10 @@ pub fn App(cx: Scope) -> impl IntoView {
<Html attributes=AdditionalAttributes::from(vec![("data-theme", "forest")])/>

// content for this welcome page
<Router fallback=|cx| {
<Router fallback=|| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
view! { cx, <ErrorTemplate outside_errors/> }.into_view(cx)
view! { <ErrorTemplate outside_errors/> }.into_view()
}>
<Navbar/>
<main>
Expand All @@ -39,12 +39,12 @@ pub fn App(cx: Scope) -> impl IntoView {

/// Renders the home page of your application.
#[component]
fn HomePage(cx: Scope) -> impl IntoView {
fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button
let (count, set_count) = create_signal(cx, 0);
let (count, set_count) = create_signal(0);
let on_click = move |_| set_count.update(|count| *count += 1);

view! { cx,
view! {
<h1>"It worky!"</h1>
<button class="btn btn-primary" on:click=on_click>
"Click Me: "
Expand All @@ -54,21 +54,21 @@ fn HomePage(cx: Scope) -> impl IntoView {
}

#[component]
fn Navbar(cx: Scope) -> impl IntoView {
fn Navbar() -> impl IntoView {

let paths = move || { vec![
("Home", "/"),
("Server Status", "/status"),
("idk", "/idk"),
]};
view! { cx,
view! {
<nav>
<div class="flex flex-row space-x-4">
<For
each=paths
key=|(_l, p)| *p
view=move |cx, (l, p)| {
view! { cx,
view=move |(l, p)| {
view! {
<A href=p class="btn btn-primary btn-outline">
{l}
</A>
Expand All @@ -83,13 +83,13 @@ fn Navbar(cx: Scope) -> impl IntoView {


#[component]
fn Idk(cx: Scope) -> impl IntoView {
view! { cx, <h1>"Idk"</h1> }
fn Idk() -> impl IntoView {
view! { <h1>"Idk"</h1> }
}

#[component]
fn Status(cx: Scope) -> impl IntoView {
view! { cx,
fn Status() -> impl IntoView {
view! {
<h1>"Status"</h1>
<McStatusComponent/>
}
Expand Down
24 changes: 11 additions & 13 deletions atb-web/src/components/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ pub async fn ping_minecraft_server() -> Result<McStatus, ServerFnError> {
}

#[component]
fn Status(cx: Scope, status: McStatus) -> impl IntoView {
fn Status(status: McStatus) -> impl IntoView {
let version = format!("Version: {}", status.version);
let players = move || status.sample.clone();

let imgref = create_node_ref::<html::Img>(cx);
let imgref = create_node_ref::<html::Img>();
//https://github.com/leptos-rs/leptos/discussions/1194#discussioncomment-6196929
create_effect(cx, move |_| {
create_effect(move |_| {
use gloo::file::{Blob, ObjectUrl};

if let Some(img) = imgref.get() {
Expand All @@ -65,41 +65,39 @@ fn Status(cx: Scope, status: McStatus) -> impl IntoView {

});

view! { cx,
view! {
<p>{version}</p>
// <img src=url/>
<img node_ref=imgref/>
<p>{format!("Players: {}/{}", status.online_players, status.max_players)}</p>
<For
each=players
key=|(_name, id)| id.clone()
view=move |cx, (name, _id)| {
view! { cx, <p>{name}</p> }
view=move |(name, _id)| {
view! { <p>{name}</p> }
}
/>
}
}
#[component]
pub fn McStatusComponent(cx: Scope) -> impl IntoView {
pub fn McStatusComponent() -> impl IntoView {


let once = create_resource(cx, move || () , move |_| async move {
let once = create_resource(move || () , move |_| async move {
let r = ping_minecraft_server().await;
let r = r.unwrap_or(McStatus::default());
r
});


//let x = move || { once.read(cx).map(|v| view! { cx, <p>{format!("{:?}", v)}</p> }) };
let x = move || { once.read(cx).map(|v| view! { cx, <Status status=v/> }) };
let x = move || { once.read().map(|v| view! { <Status status=v/> }) };

view! { cx,
view! {
<h1>"Server Status"</h1>
<Suspense fallback=move || view! { cx, <p>"Loading..."</p> }>
<Suspense fallback=move || view! { <p>"Loading..."</p> }>
<div>

{x}
// {move || { once.read(cx).map(|v| view! { cx, <p>{v.to_string()}</p> }) }}

</div>
</Suspense>
Expand Down
Loading

0 comments on commit 5fcd6b5

Please sign in to comment.