Use http functionality to make a quote

This commit is contained in:
Greg Shuflin 2024-10-02 21:04:35 -07:00
parent 5e1c9c47c3
commit db9377fa44
3 changed files with 29 additions and 1 deletions

View File

@ -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");
}

View File

@ -42,6 +42,12 @@
</form>
<p id="os-msg"></p>
<br/>
<form class="row" id="quote-form">
<button type="submit">Get a Quote</button>
</form>
<p id="quote-msg"></p>
</div>
</body>
</html>

View File

@ -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);
});
});