webcomment/webui/src/lib.rs
2023-07-13 11:36:31 +02:00

110 lines
2.5 KiB
Rust

mod api;
mod components;
mod types;
use gloo::console;
use js_sys::Date;
use parking_lot::RwLock;
use wasm_bindgen::prelude::*;
use webcomment_common::types::*;
use yew::{html, Component, Context, Html, Properties};
use std::sync::Arc;
use crate::{components::{*, admin_login_form::AdminLoginFormProps}, types::*};
pub enum Msg {
Increment,
Decrement,
}
#[derive(Properties, PartialEq)]
pub struct AppProps {
root_id: String,
api: api::Api,
}
/*impl Default for AppProps {
fn default() -> Self {
Self {
root_id: String::from("comments"),
}
}
}*/
pub struct App {
value: i64,
}
impl Component for App {
type Message = Msg;
type Properties = AppProps;
fn create(_ctx: &Context<Self>) -> Self {
Self { value: 0 }
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Increment => {
self.value += 1;
console::log!("plus one"); // Will output a string to the browser console
true // Return true to cause the displayed change to update
}
Msg::Decrement => {
self.value -= 1;
console::log!("minus one");
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
let comments_props = CommentsProps {
root_id: String::from("comments"),
api: props.api.clone(),
};
let new_comment_form_props = NewCommentFormProps {
root_id: String::from("comments"),
api: props.api.clone(),
topic: String::from("test"),
comment: Default::default(),
};
let admin_login_form_props = AdminLoginFormProps {
root_id: String::from("comments"),
api: props.api.clone(),
};
html! {
<div id={ props.root_id.clone() }>
<CommentsComponent ..comments_props />
<NewCommentFormComponent ..new_comment_form_props />
<AdminLoginFormComponent ..admin_login_form_props />
</div>
}
}
}
#[wasm_bindgen(start)]
async fn main_js() {
let api = api::Api {
inner: Arc::new(RwLock::new(api::ApiInner { admin_psw: None, comments: Default::default(), url: "http://127.0.0.1:31720".into(), local_comments: Default::default() }))
};
/*let (tx, mut rx) = yew::platform::pinned::mpsc::unbounded::<AttrValue>();
// A thread is needed because of async
spawn_local(async move {
while let Some(msg) = rx.next().await {
sleep(ONE_SEC).await;
let score = joke.len() as i16;
fun_score_cb.emit(score);
}
});*/
/*api.get_comments_by_topic("test".into()).await;*/
yew::Renderer::<App>::with_props(AppProps {
root_id: String::from("comments"),
api,
}).render();
}