40 lines
1.0 KiB
Rust
40 lines
1.0 KiB
Rust
|
|
use gloo_net::http::Request;
|
||
|
|
use leptos::task::spawn_local;
|
||
|
|
use serde::de::DeserializeOwned;
|
||
|
|
|
||
|
|
pub async fn get_json<T: DeserializeOwned>(url: &str) -> Option<T> {
|
||
|
|
Request::get(url).send().await.ok()?.json().await.ok()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn get_text(url: &str) -> Option<String> {
|
||
|
|
Request::get(url).send().await.ok()?.text().await.ok()
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Returns (success, response body).
|
||
|
|
pub async fn post(url: &str, body: String) -> Option<(bool, String)> {
|
||
|
|
let r = Request::post(url).body(body).ok()?.send().await.ok()?;
|
||
|
|
Some((r.ok(), r.text().await.ok()?))
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn edit(cmd: String) {
|
||
|
|
spawn_local(async move {
|
||
|
|
let _ = post("/api/edit", cmd).await;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
fn storage() -> Option<web_sys::Storage> {
|
||
|
|
web_sys::window()?.local_storage().ok().flatten()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn vim_pref() -> bool {
|
||
|
|
storage()
|
||
|
|
.and_then(|s| s.get_item("vim").ok().flatten())
|
||
|
|
.is_none_or(|v| v != "0")
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn set_vim_pref(on: bool) {
|
||
|
|
if let Some(s) = storage() {
|
||
|
|
let _ = s.set_item("vim", if on { "1" } else { "0" });
|
||
|
|
}
|
||
|
|
}
|