1use restless_gloo::GlooRequest;
2use std::{borrow::Cow, rc::Rc};
3use yew::functional::hook;
4use yew_hooks::prelude::{use_async_with_options, UseAsyncHandle, UseAsyncOptions};
5
6pub type UseRequestHandle<R, E> = UseAsyncHandle<R, Rc<E>>;
7
8#[hook]
13pub fn use_request<R: GlooRequest + 'static, F: Fn(&Result<R::Response, R::Error>) + 'static>(
14 prefix: Cow<'static, str>,
15 request: R,
16 after: F,
17 auto: bool,
18) -> UseRequestHandle<R::Response, R::Error>
19where
20 R::Response: Clone + 'static,
21{
22 use_async_with_options(
23 async move {
24 let result = request.send_prefix(&prefix).await;
25 after(&result);
26 result.map_err(Rc::new)
27 },
28 UseAsyncOptions { auto },
29 )
30}