This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
rust-web/frontend/src/main.rs
2024-06-11 23:32:26 -07:00

83 lines
2.1 KiB
Rust

mod finder;
mod question;
use finder::*;
use question::*;
use std::collections::HashSet;
extern crate serde;
// use gloo_console::log;
use gloo_net::http;
extern crate wasm_bindgen_futures;
use web_sys::HtmlTextAreaElement;
use yew::prelude::*;
pub type QuestionResult = Result<QuestionStruct, gloo_net::Error>;
struct App {
question: QuestionResult,
}
pub enum Msg {
GotQuestion(QuestionResult),
GetQuestion(Option<String>),
}
impl App {
fn refresh_question(ctx: &Context<Self>, key: Option<String>) {
let got_question = QuestionStruct::get_question(key);
ctx.link().send_future(got_question);
}
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
App::refresh_question(ctx, None);
let question = Err(gloo_net::Error::GlooError("Loading Question…".to_string()));
Self { question }
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::GotQuestion(question) => {
self.question = question;
true
}
Msg::GetQuestion(key) => {
// log!(format!("GetQuestion: {:?}", key));
App::refresh_question(ctx, key);
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let question = &self.question;
html! {
<>
<h1>{ "Question and Answer" }</h1>
if let Ok(ref question) = question {
<Question question={question.clone()}/>
}
if let Err(ref error) = question {
<div>
<span class="error">{format!("Server Error: {error}")}</span>
</div>
}
<div>
<button onclick={ctx.link().callback(|_| Msg::GetQuestion(None))}>{"Show me another!"}</button>
</div>
<Finder on_find={ctx.link().callback(Msg::GetQuestion)}/>
</>
}
}
}
fn main() {
yew::Renderer::<App>::new().render();
}