From 71d278927b7c85a813ca5556cf877824df7e2af2 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 30 Dec 2022 15:44:25 -0500 Subject: [PATCH] Update examples to new action APIs --- examples/counter-isomorphic/src/counters.rs | 13 +--- examples/todo-app-sqlite-axum/src/todo.rs | 75 ++++++--------------- examples/todo-app-sqlite/src/todo.rs | 51 +++++--------- 3 files changed, 43 insertions(+), 96 deletions(-) diff --git a/examples/counter-isomorphic/src/counters.rs b/examples/counter-isomorphic/src/counters.rs index 673336317..c70122dab 100644 --- a/examples/counter-isomorphic/src/counters.rs +++ b/examples/counter-isomorphic/src/counters.rs @@ -90,7 +90,7 @@ pub fn Counter(cx: Scope) -> impl IntoView { let clear = create_action(cx, |_| clear_server_count()); let counter = create_resource( cx, - move || (dec.version.get(), inc.version.get(), clear.version.get()), + move || (dec.version().get(), inc.version().get(), clear.version().get()), |_| get_server_count(), ); @@ -130,14 +130,9 @@ pub fn FormCounter(cx: Scope) -> impl IntoView { let counter = create_resource( cx, - { - let adjust = adjust.version; - let clear = clear.version; - move || (adjust.get(), clear.get()) - }, + move || (adjust.version().get(), clear.version().get()), |_| { log::debug!("FormCounter running fetcher"); - get_server_count() }, ); @@ -151,8 +146,6 @@ pub fn FormCounter(cx: Scope) -> impl IntoView { .unwrap_or(0) }; - let adjust2 = adjust.clone(); - view! { cx,
@@ -172,7 +165,7 @@ pub fn FormCounter(cx: Scope) -> impl IntoView { "Value: " {move || value().to_string()} "!" - + diff --git a/examples/todo-app-sqlite-axum/src/todo.rs b/examples/todo-app-sqlite-axum/src/todo.rs index b13562ec8..f74c51ddc 100644 --- a/examples/todo-app-sqlite-axum/src/todo.rs +++ b/examples/todo-app-sqlite-axum/src/todo.rs @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize}; cfg_if! { if #[cfg(feature = "ssr")] { use sqlx::{Connection, SqliteConnection}; - use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode}; pub async fn db() -> Result { Ok(SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string()))?) @@ -38,10 +37,9 @@ cfg_if! { #[server(GetTodos, "/api")] pub async fn get_todos(cx: Scope) -> Result, ServerFnError> { // this is just an example of how to access server context injected in the handlers - // http::Request doesn't implement Clone, so more work will be needed to do use_context() on this - let req_parts = use_context::(cx).unwrap(); - println!("\ncalling server fn"); - println!("Uri = {:?}", req_parts.uri); + let req = + use_context::(cx).expect("couldn't get HttpRequest from context"); + println!("req.path = {:?}", req.path()); use futures::TryStreamExt; @@ -57,20 +55,6 @@ pub async fn get_todos(cx: Scope) -> Result, ServerFnError> { todos.push(row); } - // Add a random header(because why not) - let mut res_headers = HeaderMap::new(); - res_headers.insert(SET_COOKIE, HeaderValue::from_str("fizz=buzz").unwrap()); - - let res_parts = leptos_axum::ResponseParts { - headers: res_headers, - status: Some(StatusCode::IM_A_TEAPOT), - }; - - let res_options_outer = use_context::(cx); - if let Some(res_options) = res_options_outer { - res_options.overwrite(res_parts).await; - } - Ok(todos) } @@ -86,7 +70,7 @@ pub async fn add_todo(title: String) -> Result<(), ServerFnError> { .execute(&mut conn) .await { - Ok(_row) => Ok(()), + Ok(row) => Ok(()), Err(e) => Err(ServerFnError::ServerError(e.to_string())), } } @@ -120,7 +104,7 @@ pub fn TodoApp(cx: Scope) -> impl IntoView { }/> - + } } @@ -130,14 +114,10 @@ pub fn Todos(cx: Scope) -> impl IntoView { let delete_todo = create_server_action::(cx); let submissions = add_todo.submissions(); - // track mutations that should lead us to refresh the list - let add_changed = add_todo.version; - let todo_deleted = delete_todo.version; - // list of todos is loaded from the server in reaction to changes let todos = create_resource( cx, - move || (add_changed(), todo_deleted()), + move || (add_todo.version().get(), delete_todo.version().get()), move |_| get_todos(cx), ); @@ -152,17 +132,11 @@ pub fn Todos(cx: Scope) -> impl IntoView { "Loading..."

}> - { - let delete_todo = delete_todo.clone(); - move || { + {move || { let existing_todos = { - let delete_todo = delete_todo.clone(); move || { - todos - .read() - .map({ - let delete_todo = delete_todo.clone(); - move |todos| match todos { + todos.read() + .map(move |todos| match todos { Err(e) => { vec![view! { cx,
"Server Error: " {e.to_string()}
}.into_any()] } @@ -172,29 +146,24 @@ pub fn Todos(cx: Scope) -> impl IntoView { } else { todos .into_iter() - .map({ - let delete_todo = delete_todo.clone(); - move |todo| { - let delete_todo = delete_todo.clone(); - view! { - cx, -
  • - {todo.title} - - - - -
  • - } - .into_any() + .map(move |todo| { + view! { + cx, +
  • + {todo.title} + + + + +
  • } + .into_any() }) .collect::>() } } - } - }) - .unwrap_or_default() + }) + .unwrap_or_default() } }; diff --git a/examples/todo-app-sqlite/src/todo.rs b/examples/todo-app-sqlite/src/todo.rs index 485bdbb08..f74c51ddc 100644 --- a/examples/todo-app-sqlite/src/todo.rs +++ b/examples/todo-app-sqlite/src/todo.rs @@ -104,7 +104,7 @@ pub fn TodoApp(cx: Scope) -> impl IntoView { }/> - + } } @@ -114,14 +114,10 @@ pub fn Todos(cx: Scope) -> impl IntoView { let delete_todo = create_server_action::(cx); let submissions = add_todo.submissions(); - // track mutations that should lead us to refresh the list - let add_changed = add_todo.version; - let todo_deleted = delete_todo.version; - // list of todos is loaded from the server in reaction to changes let todos = create_resource( cx, - move || (add_changed(), todo_deleted()), + move || (add_todo.version().get(), delete_todo.version().get()), move |_| get_todos(cx), ); @@ -136,17 +132,11 @@ pub fn Todos(cx: Scope) -> impl IntoView { "Loading..."

    }> - { - let delete_todo = delete_todo.clone(); - move || { + {move || { let existing_todos = { - let delete_todo = delete_todo.clone(); move || { - todos - .read() - .map({ - let delete_todo = delete_todo.clone(); - move |todos| match todos { + todos.read() + .map(move |todos| match todos { Err(e) => { vec![view! { cx,
    "Server Error: " {e.to_string()}
    }.into_any()] } @@ -156,29 +146,24 @@ pub fn Todos(cx: Scope) -> impl IntoView { } else { todos .into_iter() - .map({ - let delete_todo = delete_todo.clone(); - move |todo| { - let delete_todo = delete_todo.clone(); - view! { - cx, -
  • - {todo.title} - - - - -
  • - } - .into_any() + .map(move |todo| { + view! { + cx, +
  • + {todo.title} + + + + +
  • } + .into_any() }) .collect::>() } } - } - }) - .unwrap_or_default() + }) + .unwrap_or_default() } };