bazzar/web/src/lib.rs
2022-05-09 16:17:27 +02:00

60 lines
1.6 KiB
Rust

pub mod api;
mod model;
mod pages;
use seed::empty;
use seed::prelude::*;
use crate::model::Model;
use crate::pages::{Msg, Page, PublicPage};
macro_rules! fetch_page {
(public $model: expr, $page: ident, $ret: expr) => {{
let p = match &mut $model.page {
crate::pages::Page::Public(p) => p,
_ => return $ret,
};
match p {
crate::pages::PublicPage::$page(p) => p,
_ => return $ret,
}
}};
}
fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
Model {
token: LocalStorage::get("auth-token").ok(),
page: Page::Public(PublicPage::Listing(pages::public::listing::init(
url,
&mut orders.proxy(proxy_public_listing),
))),
}
}
fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg {
Msg::UrlChanged(subs::UrlChanged(url)) => model.page = Page::init(url, orders),
Msg::Public(pages::public::Msg::Listing(msg)) => {
let page = fetch_page!(public model, Listing, ());
pages::public::listing::update(msg, page, &mut orders.proxy(proxy_public_listing));
}
}
}
fn view(model: &Model) -> Node<Msg> {
match &model.page {
Page::Public(PublicPage::Listing(page)) => pages::public::listing::view(&page)
.map_msg(|msg| Msg::Public(pages::public::Msg::Listing(msg))),
_ => empty![],
}
}
#[wasm_bindgen(start)]
pub fn start() {
App::start("main", init, update, view);
}
fn proxy_public_listing(msg: pages::public::listing::Msg) -> Msg {
Msg::Public(pages::public::Msg::Listing(msg))
}