clean up examples

This commit is contained in:
Greg Johnston 2024-01-19 15:14:39 -05:00
parent 2a9e502893
commit fdd576535a
4 changed files with 66 additions and 62 deletions

View File

@ -28,7 +28,9 @@ pub fn App() -> impl IntoView {
} }
#[server] #[server]
async fn do_something(should_error: Option<String>) -> Result<String, ServerFnError> { async fn do_something(
should_error: Option<String>,
) -> Result<String, ServerFnError> {
if should_error.is_none() { if should_error.is_none() {
Ok(String::from("Successful submit")) Ok(String::from("Successful submit"))
} else { } else {
@ -42,7 +44,12 @@ async fn do_something(should_error: Option<String>) -> Result<String, ServerFnEr
#[component] #[component]
fn HomePage() -> impl IntoView { fn HomePage() -> impl IntoView {
let do_something_action = Action::<DoSomething, _>::server(); let do_something_action = Action::<DoSomething, _>::server();
let value = Signal::derive(move || do_something_action.value().get().unwrap_or_else(|| Ok(String::new()))); let value = Signal::derive(move || {
do_something_action
.value()
.get()
.unwrap_or_else(|| Ok(String::new()))
});
Effect::new_isomorphic(move |_| { Effect::new_isomorphic(move |_| {
logging::log!("Got value = {:?}", value.get()); logging::log!("Got value = {:?}", value.get());

View File

@ -1,11 +1,11 @@
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
use action_form_error_handling::app::*;
use actix_files::Files; use actix_files::Files;
use actix_web::*; use actix_web::*;
use leptos::*; use leptos::*;
use leptos_actix::{generate_route_list, LeptosRoutes}; use leptos_actix::{generate_route_list, LeptosRoutes};
use action_form_error_handling::app::*;
let conf = get_configuration(None).await.unwrap(); let conf = get_configuration(None).await.unwrap();
let addr = conf.leptos_options.site_addr; let addr = conf.leptos_options.site_addr;
@ -43,8 +43,8 @@ pub fn main() {
// a client-side main function is required for using `trunk serve` // a client-side main function is required for using `trunk serve`
// prefer using `cargo leptos serve` instead // prefer using `cargo leptos serve` instead
// to run: `trunk serve --open --features csr` // to run: `trunk serve --open --features csr`
use leptos::*;
use action_form_error_handling::app::*; use action_form_error_handling::app::*;
use leptos::*;
use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::prelude::wasm_bindgen;
console_error_panic_hook::set_once(); console_error_panic_hook::set_once();

View File

@ -118,9 +118,9 @@ pub fn Counters() -> impl IntoView {
// This is the typical pattern for a CRUD app // This is the typical pattern for a CRUD app
#[component] #[component]
pub fn Counter() -> impl IntoView { pub fn Counter() -> impl IntoView {
let dec = create_action(|_| adjust_server_count(-1, "decing".into())); let dec = create_action(|_: &()| adjust_server_count(-1, "decing".into()));
let inc = create_action(|_| adjust_server_count(1, "incing".into())); let inc = create_action(|_: &()| adjust_server_count(1, "incing".into()));
let clear = create_action(|_| clear_server_count()); let clear = create_action(|_: &()| clear_server_count());
let counter = create_resource( let counter = create_resource(
move || { move || {
( (
@ -222,9 +222,10 @@ pub fn FormCounter() -> impl IntoView {
#[component] #[component]
pub fn MultiuserCounter() -> impl IntoView { pub fn MultiuserCounter() -> impl IntoView {
let dec = let dec =
create_action(|_| adjust_server_count(-1, "dec dec goose".into())); create_action(|_: &()| adjust_server_count(-1, "dec dec goose".into()));
let inc = create_action(|_| adjust_server_count(1, "inc inc moose".into())); let inc =
let clear = create_action(|_| clear_server_count()); create_action(|_: &()| adjust_server_count(1, "inc inc moose".into()));
let clear = create_action(|_: &()| clear_server_count());
#[cfg(not(feature = "ssr"))] #[cfg(not(feature = "ssr"))]
let multiplayer_value = { let multiplayer_value = {

View File

@ -1,57 +1,53 @@
mod counters; mod counters;
use leptos::*; use crate::counters::*;
use actix_files::{Files}; use actix_files::Files;
use actix_web::*; use actix_web::*;
use crate::counters::*; use leptos::*;
use leptos_actix::{generate_route_list, LeptosRoutes}; use leptos_actix::{generate_route_list, LeptosRoutes};
#[get("/api/events")] #[get("/api/events")]
async fn counter_events() -> impl Responder { async fn counter_events() -> impl Responder {
use futures::StreamExt; use futures::StreamExt;
let stream = let stream = futures::stream::once(async {
futures::stream::once(async { crate::counters::get_server_count().await.unwrap_or(0) }) crate::counters::get_server_count().await.unwrap_or(0)
.chain(COUNT_CHANNEL.clone()) })
.map(|value| { .chain(COUNT_CHANNEL.clone())
Ok(web::Bytes::from(format!( .map(|value| {
"event: message\ndata: {value}\n\n" Ok(web::Bytes::from(format!(
))) as Result<web::Bytes> "event: message\ndata: {value}\n\n"
}); ))) as Result<web::Bytes>
HttpResponse::Ok() });
.insert_header(("Content-Type", "text/event-stream")) HttpResponse::Ok()
.streaming(stream) .insert_header(("Content-Type", "text/event-stream"))
} .streaming(stream)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> { #[actix_web::main]
async fn main() -> std::io::Result<()> {
// Explicit server function registration is no longer required // Setting this to None means we'll be using cargo-leptos and its env vars.
// on the main branch. On 0.3.0 and earlier, uncomment the lines // when not using cargo-leptos None must be replaced with Some("Cargo.toml")
// below to register the server functions. let conf = get_configuration(None).await.unwrap();
// _ = GetServerCount::register();
// _ = AdjustServerCount::register(); let addr = conf.leptos_options.site_addr;
// _ = ClearServerCount::register(); let routes = generate_route_list(Counters);
// Setting this to None means we'll be using cargo-leptos and its env vars. HttpServer::new(move || {
// when not using cargo-leptos None must be replaced with Some("Cargo.toml") let leptos_options = &conf.leptos_options;
let conf = get_configuration(None).await.unwrap(); let site_root = &leptos_options.site_root;
let addr = conf.leptos_options.site_addr; App::new()
let routes = generate_route_list(Counters); .service(counter_events)
.leptos_routes(
HttpServer::new(move || { leptos_options.to_owned(),
let leptos_options = &conf.leptos_options; routes.to_owned(),
let site_root = &leptos_options.site_root; Counters,
)
App::new() .service(Files::new("/", site_root))
.service(counter_events) //.wrap(middleware::Compress::default())
.leptos_routes(leptos_options.to_owned(), routes.to_owned(), Counters) })
.service(Files::new("/", site_root)) .bind(&addr)?
//.wrap(middleware::Compress::default()) .run()
}) .await
.bind(&addr)?
.run()
.await
}
} }