Compare commits

..

1 Commits

Author SHA1 Message Date
Greg Johnston
8aa21d1085 remove unnecessary "openssl" feature from Actix examples 2023-02-06 07:40:38 -05:00
10 changed files with 29 additions and 151 deletions

View File

@@ -30,7 +30,7 @@ pub fn ErrorTemplate(
.into_iter()
.filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
.collect();
log!("Errors: {errors:#?}");
println!("Errors: {errors:#?}");
// Only the response code for the first error is actually sent from the server
// this may be customized by the specific application

View File

@@ -12,7 +12,8 @@ cfg_if! { if #[cfg(feature = "ssr")] {
use tower_http::services::ServeDir;
use std::sync::Arc;
use leptos::{LeptosOptions, Errors, view};
use crate::landing::{App, AppProps};
use crate::error_template::{ErrorTemplate, ErrorTemplateProps};
use crate::errors::AppError;
pub async fn file_and_error_handler(uri: Uri, Extension(options): Extension<Arc<LeptosOptions>>, req: Request<Body>) -> AxumResponse {
let options = &*options;
@@ -22,10 +23,9 @@ cfg_if! { if #[cfg(feature = "ssr")] {
if res.status() == StatusCode::OK {
res.into_response()
} else{
let handler = leptos_axum::render_app_to_stream(
options.to_owned(),
move |cx| view!{ cx, <App/> }
);
let mut errors = Errors::default();
errors.insert_with_default_key(AppError::NotFound);
let handler = leptos_axum::render_app_to_stream(options.to_owned(), move |cx| view!{cx, <ErrorTemplate outside_errors=errors.clone()/>});
handler(req).await.into_response()
}
}

View File

@@ -29,14 +29,7 @@ pub fn App(cx: Scope) -> impl IntoView {
cx,
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/pkg/errors_axum.css"/>
<Router fallback=|cx| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
view! { cx,
<ErrorTemplate outside_errors/>
}
.into_view(cx)
}>
<Router>
<header>
<h1>"Error Examples:"</h1>
</header>

View File

@@ -86,19 +86,21 @@ pub fn ContactList(cx: Scope) -> impl IntoView {
}
}
#[derive(Params, PartialEq, Clone, Debug)]
pub struct ContactParams {
id: usize,
}
#[component]
pub fn Contact(cx: Scope) -> impl IntoView {
log::debug!("rendering <Contact/>");
let params = use_params::<ContactParams>(cx);
let params = use_params_map(cx);
let contact = create_resource(
cx,
move || params().map(|params| params.id).ok(),
move || {
params()
.get("id")
.cloned()
.unwrap_or_default()
.parse::<usize>()
.ok()
},
// any of the following would work (they're identical)
// move |id| async move { get_contact(id).await }
// move |id| get_contact(id),

View File

@@ -176,25 +176,3 @@ pub type ChildrenFn = Box<dyn Fn(Scope) -> Fragment>;
/// A type for the `children` property on components that can be called
/// more than once, but may mutate the children.
pub type ChildrenFnMut = Box<dyn FnMut(Scope) -> Fragment>;
/// A type for taking anything that implements [`IntoAttribute`].
/// Very usefull inside components.
///
/// ## Example
/// ```rust
/// use leptos::*;
///
/// #[component]
/// pub fn MyHeading(
/// cx: Scope,
/// text: String,
/// #[prop(optional, into)]
/// class: Option<AttributeValue>
/// ) -> impl IntoView {
/// view!{
/// cx,
/// <h1 class=class>{text}</h1>
/// }
/// }
/// ```
pub type AttributeValue = Box<dyn IntoAttribute>;

View File

@@ -102,67 +102,24 @@ impl std::fmt::Debug for Attribute {
pub trait IntoAttribute {
/// Converts the object into an [Attribute].
fn into_attribute(self, cx: Scope) -> Attribute;
/// Helper function for dealing with [Box<dyn IntoAttribute>]
fn into_attribute_boxed(self: Box<Self>, cx: Scope) -> Attribute;
}
impl<T: IntoAttribute + 'static> From<T> for Box<dyn IntoAttribute> {
fn from(value: T) -> Self {
Box::new(value)
}
}
impl IntoAttribute for Attribute {
#[inline]
fn into_attribute(self, _: Scope) -> Attribute {
self
}
#[inline]
fn into_attribute_boxed(self: Box<Self>, _: Scope) -> Attribute {
*self
}
}
macro_rules! impl_into_attr_boxed {
() => {
#[inline]
fn into_attribute_boxed(self: Box<Self>, cx: Scope) -> Attribute {
self.into_attribute(cx)
}
};
}
impl IntoAttribute for Option<Attribute> {
fn into_attribute(self, cx: Scope) -> Attribute {
self.unwrap_or(Attribute::Option(cx, None))
}
impl_into_attr_boxed! {}
}
impl IntoAttribute for String {
fn into_attribute(self, _: Scope) -> Attribute {
Attribute::String(self)
}
impl_into_attr_boxed! {}
}
impl IntoAttribute for bool {
fn into_attribute(self, _: Scope) -> Attribute {
Attribute::Bool(self)
}
impl_into_attr_boxed! {}
}
impl IntoAttribute for Option<String> {
fn into_attribute(self, cx: Scope) -> Attribute {
Attribute::Option(cx, self)
}
impl_into_attr_boxed! {}
}
impl<T, U> IntoAttribute for T
@@ -174,35 +131,12 @@ where
let modified_fn = Rc::new(move || (self)().into_attribute(cx));
Attribute::Fn(cx, modified_fn)
}
impl_into_attr_boxed! {}
}
impl<T: IntoAttribute> IntoAttribute for (Scope, T) {
fn into_attribute(self, _: Scope) -> Attribute {
self.1.into_attribute(self.0)
}
impl_into_attr_boxed! {}
}
impl IntoAttribute for (Scope, Option<Box<dyn IntoAttribute>>) {
fn into_attribute(self, _: Scope) -> Attribute {
match self.1 {
Some(bx) => bx.into_attribute_boxed(self.0),
None => Attribute::Option(self.0, None),
}
}
impl_into_attr_boxed! {}
}
impl IntoAttribute for (Scope, Box<dyn IntoAttribute>) {
fn into_attribute(self, _: Scope) -> Attribute {
self.1.into_attribute_boxed(self.0)
}
impl_into_attr_boxed! {}
}
macro_rules! attr_type {
@@ -211,22 +145,12 @@ macro_rules! attr_type {
fn into_attribute(self, _: Scope) -> Attribute {
Attribute::String(self.to_string())
}
#[inline]
fn into_attribute_boxed(self: Box<Self>, cx: Scope) -> Attribute {
self.into_attribute(cx)
}
}
impl IntoAttribute for Option<$attr_type> {
fn into_attribute(self, cx: Scope) -> Attribute {
Attribute::Option(cx, self.map(|n| n.to_string()))
}
#[inline]
fn into_attribute_boxed(self: Box<Self>, cx: Scope) -> Attribute {
self.into_attribute(cx)
}
}
};
}

View File

@@ -29,7 +29,7 @@ pub fn impl_params(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
let gen = quote! {
impl Params for #name {
fn from_map(map: &::leptos_router::ParamsMap) -> Result<Self, ::leptos_router::ParamsError> {
fn from_map(map: &::leptos_router::ParamsMap) -> Result<Self, ::leptos_router::RouterError> {
Ok(Self {
#(#fields,)*
})

View File

@@ -4,9 +4,6 @@ use std::{error::Error, rc::Rc};
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
type OnFormData = Rc<dyn Fn(&web_sys::FormData)>;
type OnResponse = Rc<dyn Fn(&web_sys::Response)>;
/// An HTML [`form`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) progressively
/// enhanced to use client-side routing.
#[component]
@@ -33,14 +30,13 @@ pub fn Form<A>(
error: Option<RwSignal<Option<Box<dyn Error>>>>,
/// A callback will be called with the [FormData](web_sys::FormData) when the form is submitted.
#[prop(optional)]
on_form_data: Option<OnFormData>,
/// Sets the `class` attribute on the underlying `<form>` tag, making it easier to style.
#[prop(optional, into)]
class: Option<AttributeValue>,
#[allow(clippy::type_complexity)]
on_form_data: Option<Rc<dyn Fn(&web_sys::FormData)>>,
/// A callback will be called with the [Response](web_sys::Response) the server sends in response
/// to a form submission.
#[prop(optional)]
on_response: Option<OnResponse>,
#[allow(clippy::type_complexity)]
on_response: Option<Rc<dyn Fn(&web_sys::Response)>>,
/// Component children; should include the HTML of the form elements.
children: Children,
) -> impl IntoView
@@ -54,9 +50,8 @@ where
enctype: Option<String>,
version: Option<RwSignal<usize>>,
error: Option<RwSignal<Option<Box<dyn Error>>>>,
on_form_data: Option<OnFormData>,
on_response: Option<OnResponse>,
class: Option<Attribute>,
#[allow(clippy::type_complexity)] on_form_data: Option<Rc<dyn Fn(&web_sys::FormData)>>,
#[allow(clippy::type_complexity)] on_response: Option<Rc<dyn Fn(&web_sys::Response)>>,
children: Children,
) -> HtmlElement<Form> {
let action_version = version;
@@ -133,7 +128,6 @@ where
action=move || action.get()
enctype=enctype
on:submit=on_submit
class=class
>
{children(cx)}
</form>
@@ -141,7 +135,6 @@ where
}
let action = use_resolved_path(cx, move || action.to_href()());
let class = class.map(|bx| bx.into_attribute_boxed(cx));
inner(
cx,
method,
@@ -151,7 +144,6 @@ where
error,
on_form_data,
on_response,
class,
children,
)
}
@@ -166,9 +158,6 @@ pub fn ActionForm<I, O>(
/// by default using [create_server_action](leptos_server::create_server_action) or added
/// manually using [leptos_server::Action::using_server_fn].
action: Action<I, Result<O, ServerFnError>>,
/// Sets the `class` attribute on the underlying `<form>` tag, making it easier to style.
#[prop(optional, into)]
class: Option<AttributeValue>,
/// Component children; should include the HTML of the form elements.
children: Children,
) -> impl IntoView
@@ -219,7 +208,7 @@ where
action.set_pending(false);
});
});
let class = class.map(|bx| bx.into_attribute_boxed(cx));
Form(
cx,
FormProps::builder()
@@ -228,7 +217,6 @@ where
.on_form_data(on_form_data)
.on_response(on_response)
.method("post")
.class(class)
.children(children)
.build(),
)
@@ -244,9 +232,6 @@ pub fn MultiActionForm<I, O>(
/// by default using [create_server_action](leptos_server::create_server_action) or added
/// manually using [leptos_server::Action::using_server_fn].
action: MultiAction<I, Result<O, ServerFnError>>,
/// Sets the `class` attribute on the underlying `<form>` tag, making it easier to style.
#[prop(optional, into)]
class: Option<AttributeValue>,
/// Component children; should include the HTML of the form elements.
children: Children,
) -> impl IntoView
@@ -280,12 +265,10 @@ where
}
};
let class = class.map(|bx| bx.into_attribute_boxed(cx));
view! { cx,
<form
method="POST"
action=action
class=class
on:submit=on_submit
>
{children(cx)}

View File

@@ -63,7 +63,7 @@ pub fn A<H>(
replace: bool,
/// Sets the `class` attribute on the underlying `<a>` tag, making it easier to style.
#[prop(optional, into)]
class: Option<AttributeValue>,
class: Option<MaybeSignal<String>>,
/// The nodes or elements to be shown inside the link.
children: Children,
) -> impl IntoView
@@ -76,7 +76,7 @@ where
exact: bool,
state: Option<State>,
replace: bool,
class: Option<AttributeValue>,
class: Option<MaybeSignal<String>>,
children: Children,
) -> HtmlElement<A> {
let location = use_location(cx);
@@ -104,7 +104,7 @@ where
prop:state={state.map(|s| s.to_js_value())}
prop:replace={replace}
aria-current=move || if is_active.get() { Some("page") } else { None }
class=class
class=move || class.as_ref().map(|class| class.get())
>
{children(cx)}
</a>

View File

@@ -45,10 +45,8 @@
//! // LR will enhance the active <a> link with the [aria-current] attribute
//! // we can use this for styling them with CSS like `[aria-current] { font-weight: bold; }`
//! <A href="contacts">"Contacts"</A>
//! // But we can also use a normal class attribute like it is a normal component
//! <A href="settings" class="my-class">"Settings"</A>
//! // It also supports signals!
//! <A href="about" class=move || "my-class">"About"</A>
//! <A href="about">"About"</A>
//! <A href="settings">"Settings"</A>
//! </nav>
//! <main>
//! // <Routes/> both defines our routes and shows them on the page