Give direct access to input and value fields on actions

This commit is contained in:
Greg Johnston 2022-11-18 10:56:00 -05:00
parent 97a7240e26
commit d530b28348
2 changed files with 10 additions and 34 deletions

View file

@ -347,8 +347,11 @@ where
{
/// How many times the action has successfully resolved.
pub version: RwSignal<usize>,
input: RwSignal<Option<I>>,
value: RwSignal<Option<O>>,
/// The current argument that was dispatched to the `async` function.
/// `Some` while we are waiting for it to resolve, `None` if it has resolved.
pub input: RwSignal<Option<I>>,
/// The most recent return value of the `async` function.
pub value: RwSignal<Option<O>>,
pending: RwSignal<bool>,
url: Option<String>,
#[allow(clippy::complexity)]
@ -383,35 +386,6 @@ where
self.pending.read_only()
}
/// The argument that was dispatched to the `async` function,
/// only while we are waiting for it to resolve.
pub fn input(&self) -> ReadSignal<Option<I>> {
self.input.read_only()
}
/// The argument that was dispatched to the `async` function.
///
/// You probably don't need to call this unless you are implementing a form
/// or some other kind of wrapper for an action and need to set the input
/// based on its internal logic.
pub fn set_input(&self, value: I) {
self.input.set(Some(value));
}
/// The most recent return value of the `async` function.
pub fn value(&self) -> ReadSignal<Option<O>> {
self.value.read_only()
}
/// Sets the most recent return value of the `async` function.
///
/// You probably don't need to call this unless you are implementing a form
/// or some other kind of wrapper for an action and need to set the value
/// based on its internal logic.
pub fn set_value(&self, value: O) {
self.value.set(Some(value));
}
/// The URL associated with the action (typically as part of a server function.)
/// This enables integration with the `ActionForm` component in `leptos_router`.
pub fn url(&self) -> Option<&str> {

View file

@ -258,6 +258,8 @@ where
""
}.to_string();
let version = props.action.version;
let value = props.action.value;
let input = props.action.input;
let on_form_data = {
let action = props.action.clone();
@ -267,7 +269,7 @@ where
let data = data.to_string().as_string().unwrap_or_default();
let data = serde_urlencoded::from_str::<I>(&data);
match data {
Ok(data) => action.set_input(data),
Ok(data) => input.set(Some(data)),
Err(e) => log::error!("{e}"),
}
})
@ -291,9 +293,9 @@ where
match O::from_json(
&json.as_string().expect("couldn't get String from JsString"),
) {
Ok(res) => action.set_value(Ok(res)),
Ok(res) => value.set(Some(Ok(res))),
Err(e) => {
action.set_value(Err(ServerFnError::Deserialization(e.to_string())))
value.set(Some(Err(ServerFnError::Deserialization(e.to_string()))))
}
}
}