#![feature(try_trait_v2)] pub mod api; mod i18n; mod model; mod pages; pub mod session; pub mod shared; use seed::empty; use seed::prelude::*; use crate::i18n::I18n; use crate::model::Model; use crate::pages::{Msg, Page, PublicPage}; use crate::session::SessionMsg; #[macro_export] 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, } }}; (public $model: expr, $page: ident) => {{ let p = match &mut $model.page { crate::pages::Page::Public(p) => p, _ => return, }; match p { crate::pages::PublicPage::$page(p) => p, _ => return, } }}; (public page $page: expr, $page_name: ident) => {{ let p = match $page { crate::pages::Page::Public(p) => p, _ => return, }; match p { crate::pages::PublicPage::$page_name(p) => p, _ => return, } }}; (public page $page: expr, $page_name: ident, $ret: expr) => {{ let p = match $page { crate::pages::Page::Public(p) => p, _ => { *$page = $ret; return; } }; match p { crate::pages::PublicPage::$page_name(p) => p, _ => { *$page = $ret; return; } } }}; } fn init(url: Url, orders: &mut impl Orders) -> Model { orders.subscribe(Msg::UrlChanged); let mut model = Model { url: url.clone().set_path(&[] as &[&str]), token: LocalStorage::get("auth-token").ok(), page: Page::init(url, orders), logo: seed::document() .query_selector("link[rel=icon]") .ok() .flatten() .and_then(|el: web_sys::Element| el.get_attribute("href")), shared: shared::Model::default(), i18n: I18n::load(), }; session::init(&mut model, orders); model } fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders) { match msg { Msg::Shared(msg) => { shared::update(msg, &mut model.shared, orders); } Msg::UrlChanged(subs::UrlChanged(url)) => model.page.page_changed(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(Into::into)); } Msg::Public(pages::public::Msg::Product(msg)) => { let page = fetch_page!(public model, Product); pages::public::product::update(msg, page, &mut orders.proxy(Into::into)) } Msg::Public(pages::public::Msg::SignIn(msg)) => { let page = fetch_page!(public model, SignIn); pages::public::sign_in::update(msg, page, &mut orders.proxy(Into::into)) } Msg::Public(pages::public::Msg::SignUp(pages::public::sign_up::Msg::AccountCreated( res, ))) => { orders .skip() .send_msg(Msg::Session(SessionMsg::TokenRefreshed(res))); } Msg::Public(pages::public::Msg::SignUp(msg)) => { let page = fetch_page!(public model, SignUp); pages::public::sign_up::update(msg, page, &mut orders.proxy(Into::into)) } Msg::Admin(_) => {} Msg::Session(msg) => { session::update(msg, model, orders); } } } fn view(model: &Model) -> Node { match &model.page { Page::Public(PublicPage::Listing(page)) => pages::public::listing::view(model, page), Page::Public(PublicPage::Product(page)) => pages::public::product::view(model, page), Page::Public(PublicPage::SignIn(page)) => pages::public::sign_in::view(model, page), Page::Public(PublicPage::SignUp(page)) => pages::public::sign_up::view(model, page), _ => empty![], } } #[wasm_bindgen(start)] pub fn start() { App::start("main", init, update, view); }