diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 512adc2..22499a4 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,3 +1,5 @@ +use tauri_plugin_http::reqwest; + // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command #[tauri::command] fn greet(name: &str) -> String { @@ -11,6 +13,16 @@ fn os_info() -> String { format!("Platform Info: {}", platform) } +//TODO make this return a JS object +#[tauri::command] +async fn quote() -> String { + let Ok(res) = reqwest::get("https://perl.is/random").await else { return "ERROR".to_string() }; + println!("Status: {}", res.status()); + let Ok(text) = res.text().await else { return "TEXT ERROR".to_string() }; + + text +} + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tracing::info!("Starting up tauri app"); @@ -18,7 +30,7 @@ pub fn run() { .plugin(tauri_plugin_http::init()) .plugin(tauri_plugin_os::init()) .plugin(tauri_plugin_shell::init()) - .invoke_handler(tauri::generate_handler![greet, os_info]) + .invoke_handler(tauri::generate_handler![greet, os_info, quote]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src/index.html b/src/index.html index 0b36422..b1f8139 100644 --- a/src/index.html +++ b/src/index.html @@ -42,6 +42,12 @@

+
+
+ +
+

+ diff --git a/src/main.js b/src/main.js index 662bbfd..1523aaf 100644 --- a/src/main.js +++ b/src/main.js @@ -13,6 +13,10 @@ async function os_info(elem) { elem.textContent = await invoke("os_info", {}); } +async function get_quote(elem) { + elem.textContent = await invoke("quote", {}); +} + window.addEventListener("DOMContentLoaded", () => { greetInputEl = document.querySelector("#greet-input"); @@ -27,4 +31,10 @@ window.addEventListener("DOMContentLoaded", () => { const elem = document.querySelector("#os-msg"); os_info(elem); }); + + document.querySelector("#quote-form").addEventListener("submit", (e) => { + e.preventDefault(); + const elem = document.querySelector("#quote-msg"); + get_quote(elem); + }); });