|
| 1 | +//! Shared loading/empty/error renderer for any seam-backed read. |
| 2 | +//! |
| 3 | +//! The caller maps its `use_resource` result to `AsyncState<T>` (plain Rust), |
| 4 | +//! renders the `Loaded(T)` case itself, and delegates the three non-loaded |
| 5 | +//! states to this component. Keeps every wired view from re-implementing the |
| 6 | +//! spinner / empty / error+retry markup. |
| 7 | +//! |
| 8 | +//! Calling convention: build one `AsyncState<T>` via `from_value` and drive both |
| 9 | +//! this component and the loaded markup from it — no inline match arms: |
| 10 | +//! ```ignore |
| 11 | +//! let state = AsyncState::from_value(resource.read().clone()); |
| 12 | +//! rsx! { |
| 13 | +//! AsyncView { |
| 14 | +//! loading: state.is_loading(), |
| 15 | +//! empty: state.is_empty(), |
| 16 | +//! error: state.error_message(), |
| 17 | +//! retriable: state.is_retriable(), |
| 18 | +//! on_retry: move |_| resource.restart(), |
| 19 | +//! } |
| 20 | +//! if let Some(data) = state.loaded() { /* caller's own markup for `data` */ } |
| 21 | +//! } |
| 22 | +//! ``` |
| 23 | +//! Discrete props (rather than the generic `AsyncState<T>`) are used because |
| 24 | +//! Dioxus props must be `Clone + PartialEq`; `StudioError`/`T` need not be — so |
| 25 | +//! the caller decodes the state via the accessors and passes plain flags. |
| 26 | +
|
| 27 | +use dioxus::prelude::*; |
| 28 | + |
| 29 | +#[derive(Clone, PartialEq, Props)] |
| 30 | +pub struct AsyncViewProps { |
| 31 | + /// True while the first fetch is pending. |
| 32 | + pub loading: bool, |
| 33 | + /// True when the fetch returned an empty result. |
| 34 | + pub empty: bool, |
| 35 | + /// Some(message) when the fetch failed; None otherwise. |
| 36 | + pub error: Option<String>, |
| 37 | + /// Whether the failed fetch is retriable (gates the Retry button). |
| 38 | + #[props(default = false)] |
| 39 | + pub retriable: bool, |
| 40 | + /// Called when the user clicks Retry (wire to Resource::restart()). |
| 41 | + #[props(default)] |
| 42 | + pub on_retry: EventHandler<()>, |
| 43 | + /// Message shown in the empty state. |
| 44 | + #[props(default = "No records.".to_string())] |
| 45 | + pub empty_message: String, |
| 46 | +} |
| 47 | + |
| 48 | +// Consumed by the notification popover and later wired views. |
| 49 | +#[component] |
| 50 | +pub fn AsyncView(props: AsyncViewProps) -> Element { |
| 51 | + if props.loading { |
| 52 | + return rsx! { div { class: "async-loading", "Loading…" } }; |
| 53 | + } |
| 54 | + if let Some(msg) = props.error { |
| 55 | + return rsx! { |
| 56 | + div { class: "async-error", |
| 57 | + div { class: "async-error-msg", "{msg}" } |
| 58 | + if props.retriable { |
| 59 | + button { |
| 60 | + class: "btn", |
| 61 | + onclick: move |_| props.on_retry.call(()), |
| 62 | + "Retry" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + if props.empty { |
| 69 | + return rsx! { div { class: "async-empty", "{props.empty_message}" } }; |
| 70 | + } |
| 71 | + // Loaded: the caller renders its own markup; AsyncView renders nothing. |
| 72 | + rsx! {} |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use super::*; |
| 78 | + |
| 79 | + // SSR the given root component to HTML. The rsx is built INSIDE the component |
| 80 | + // (i.e. within the VirtualDom runtime) because `AsyncView`'s default |
| 81 | + // `EventHandler` prop can only be constructed while a runtime is in scope. |
| 82 | + fn render(app: fn() -> Element) -> String { |
| 83 | + let mut dom = VirtualDom::new(app); |
| 84 | + dom.rebuild_in_place(); |
| 85 | + dioxus_ssr::render(&dom) |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn loading_renders_spinner() { |
| 90 | + fn app() -> Element { |
| 91 | + rsx! { AsyncView { loading: true, empty: false, error: None } } |
| 92 | + } |
| 93 | + let html = render(app); |
| 94 | + assert!(html.contains("async-loading")); |
| 95 | + assert!(html.contains("Loading")); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn empty_renders_message() { |
| 100 | + fn app() -> Element { |
| 101 | + rsx! { |
| 102 | + AsyncView { |
| 103 | + loading: false, |
| 104 | + empty: true, |
| 105 | + error: None, |
| 106 | + empty_message: "Nothing here".to_string(), |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + let html = render(app); |
| 111 | + assert!(html.contains("async-empty")); |
| 112 | + assert!(html.contains("Nothing here")); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn retriable_error_shows_retry() { |
| 117 | + fn app() -> Element { |
| 118 | + rsx! { |
| 119 | + AsyncView { |
| 120 | + loading: false, |
| 121 | + empty: false, |
| 122 | + error: Some("boom".to_string()), |
| 123 | + retriable: true, |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + let html = render(app); |
| 128 | + assert!(html.contains("async-error")); |
| 129 | + assert!(html.contains("boom")); |
| 130 | + assert!(html.contains("Retry")); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn non_retriable_error_hides_retry() { |
| 135 | + fn app() -> Element { |
| 136 | + rsx! { |
| 137 | + AsyncView { |
| 138 | + loading: false, |
| 139 | + empty: false, |
| 140 | + error: Some("boom".to_string()), |
| 141 | + retriable: false, |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + let html = render(app); |
| 146 | + assert!(html.contains("async-error")); |
| 147 | + assert!(html.contains("boom")); |
| 148 | + assert!(!html.contains("Retry")); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn loaded_renders_nothing() { |
| 153 | + fn app() -> Element { |
| 154 | + rsx! { AsyncView { loading: false, empty: false, error: None } } |
| 155 | + } |
| 156 | + let html = render(app); |
| 157 | + assert!(!html.contains("async-loading")); |
| 158 | + assert!(!html.contains("async-empty")); |
| 159 | + assert!(!html.contains("async-error")); |
| 160 | + } |
| 161 | +} |
0 commit comments