2022-05-09 16:17:27 +02:00
|
|
|
pub mod api;
|
2022-05-10 08:23:35 +02:00
|
|
|
mod i18n;
|
2022-05-05 16:30:25 +02:00
|
|
|
mod model;
|
|
|
|
mod pages;
|
2022-05-09 16:41:11 +02:00
|
|
|
pub mod shared;
|
2022-05-05 16:30:25 +02:00
|
|
|
|
|
|
|
use seed::empty;
|
|
|
|
use seed::prelude::*;
|
|
|
|
|
2022-05-09 16:17:27 +02:00
|
|
|
use crate::model::Model;
|
2022-05-05 16:30:25 +02:00
|
|
|
use crate::pages::{Msg, Page, PublicPage};
|
|
|
|
|
2022-05-11 15:56:41 +02:00
|
|
|
#[macro_export]
|
2022-05-09 16:17:27 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}};
|
2022-05-11 15:56:41 +02:00
|
|
|
(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,
|
|
|
|
}
|
|
|
|
}};
|
2022-05-09 16:17:27 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:30:25 +02:00
|
|
|
fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
2022-05-10 22:32:22 +02:00
|
|
|
orders
|
|
|
|
.stream(streams::interval(500, || Msg::CheckAccessToken))
|
|
|
|
.subscribe(Msg::UrlChanged);
|
2022-05-10 16:20:37 +02:00
|
|
|
|
2022-05-05 16:30:25 +02:00
|
|
|
Model {
|
2022-05-11 15:56:41 +02:00
|
|
|
url: url.clone().set_path(&[] as &[&str]),
|
2022-05-05 16:30:25 +02:00
|
|
|
token: LocalStorage::get("auth-token").ok(),
|
2022-05-11 15:56:41 +02:00
|
|
|
page: Page::init(url, orders),
|
2022-05-10 16:20:37 +02:00
|
|
|
logo: seed::document()
|
|
|
|
.query_selector("link[rel=icon]")
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
.and_then(|el: web_sys::Element| el.get_attribute("href")),
|
|
|
|
shared: shared::Model::default(),
|
2022-05-05 16:30:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|
|
|
match msg {
|
2022-05-10 16:20:37 +02:00
|
|
|
Msg::Shared(msg) => {
|
|
|
|
shared::update(msg, &mut model.shared, orders);
|
|
|
|
}
|
|
|
|
Msg::CheckAccessToken => {
|
|
|
|
orders.skip();
|
2022-05-10 16:32:10 +02:00
|
|
|
if model.shared.refresh_token.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-10 16:20:37 +02:00
|
|
|
if let Some(exp) = model.shared.exp {
|
|
|
|
if exp > chrono::Utc::now().naive_utc() - chrono::Duration::seconds(1) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-10 16:32:10 +02:00
|
|
|
if let Some(token) = model.shared.refresh_token.take() {
|
2022-05-10 16:20:37 +02:00
|
|
|
orders.send_msg(Msg::Shared(shared::Msg::RefreshToken(token)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 15:56:41 +02:00
|
|
|
Msg::UrlChanged(subs::UrlChanged(url)) => model.page.page_changed(url),
|
2022-05-09 16:17:27 +02:00
|
|
|
Msg::Public(pages::public::Msg::Listing(msg)) => {
|
2022-05-11 15:56:41 +02:00
|
|
|
let page = fetch_page!(public model, Listing);
|
2022-05-09 16:17:27 +02:00
|
|
|
pages::public::listing::update(msg, page, &mut orders.proxy(proxy_public_listing));
|
|
|
|
}
|
2022-05-11 15:56:41 +02:00
|
|
|
Msg::Public(pages::public::Msg::Product(msg)) => {
|
|
|
|
let page = fetch_page!(public model, Product);
|
|
|
|
pages::public::product::update(msg, page, &mut orders.proxy(proxy_public_product))
|
|
|
|
}
|
2022-05-05 16:30:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(model: &Model) -> Node<Msg> {
|
|
|
|
match &model.page {
|
2022-05-11 15:56:41 +02:00
|
|
|
Page::Public(PublicPage::Listing(page)) => pages::public::listing::view(model, page),
|
|
|
|
Page::Public(PublicPage::Product(page)) => pages::public::product::view(model, page),
|
2022-05-05 16:30:25 +02:00
|
|
|
_ => empty![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(start)]
|
|
|
|
pub fn start() {
|
|
|
|
App::start("main", init, update, view);
|
|
|
|
}
|
2022-05-09 16:17:27 +02:00
|
|
|
|
|
|
|
fn proxy_public_listing(msg: pages::public::listing::Msg) -> Msg {
|
|
|
|
Msg::Public(pages::public::Msg::Listing(msg))
|
|
|
|
}
|
2022-05-11 15:56:41 +02:00
|
|
|
|
|
|
|
fn proxy_public_product(msg: pages::public::product::Msg) -> Msg {
|
|
|
|
Msg::Public(pages::public::Msg::Product(msg))
|
|
|
|
}
|