Load me, show debug modal

This commit is contained in:
Adrian Woźniak 2022-05-15 13:08:42 +02:00
parent c047ce19b4
commit 09d7369b0c
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B
9 changed files with 102 additions and 16 deletions

1
Cargo.lock generated
View File

@ -829,6 +829,7 @@ version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [ dependencies = [
"js-sys",
"libc", "libc",
"num-integer", "num-integer",
"num-traits", "num-traits",

View File

@ -12,7 +12,7 @@ model = { path = "../shared/model", features = ["dummy"] }
seed = { version = "0.9.1", features = [] } seed = { version = "0.9.1", features = [] }
seed_heroicons = { git = "https://github.com/mh84/seed_heroicons.git" } seed_heroicons = { git = "https://github.com/mh84/seed_heroicons.git" }
chrono = { version = "*", features = ["wasm-bindgen"] } chrono = { version = "*", features = ["wasm-bindgen", "wasmbind"] }
gloo-timers = { version = "*", features = ["futures"] } gloo-timers = { version = "*", features = ["futures"] }
uuid = { version = "1.0.0", features = ["v4"] } uuid = { version = "1.0.0", features = ["v4"] }

48
web/src/debug/mod.rs Normal file
View File

@ -0,0 +1,48 @@
use seed::prelude::*;
use seed::*;
use crate::{Model, Msg};
#[derive(Debug)]
pub enum DebugMsg {
Open,
Close,
}
pub fn init(model: &mut Model, orders: &mut impl Orders<Msg>) {
model.debug_modal = false;
orders.stream(streams::window_event(Ev::KeyPress, |ev| {
let ke: &web_sys::KeyboardEvent = to_keyboard_event(&ev);
if ke.char_code() == 63 && ke.shift_key() {
Msg::Debug(DebugMsg::Open)
} else {
Msg::NoOp
}
}));
}
pub fn update(msg: DebugMsg, model: &mut Model) {
model.debug_modal = matches!(msg, DebugMsg::Open);
}
pub fn view(model: &Model) -> Node<Msg> {
if !model.debug_modal {
return empty![];
}
div![
attrs![At::Style => "display:block;overflow:auto;position:fixed;top:0;left:0;width:100%;height:100%;background:white;z-index:8;"],
a![
attrs![At::Style => "display:block;margin:4px;background:red;color:white;"],
ev(Ev::Click, move |ev| {
ev.stop_propagation();
ev.prevent_default();
Msg::Debug(DebugMsg::Close)
}),
"Close"
],
div![
attrs![At::Style => "display:block;"],
pre![code![format!("{model:#?}")]]
]
]
}

View File

@ -2,6 +2,7 @@ mod pl;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug)]
pub struct I18n { pub struct I18n {
store: HashMap<String, HashMap<String, &'static str>>, store: HashMap<String, HashMap<String, &'static str>>,
lang: String, lang: String,

View File

@ -1,6 +1,8 @@
#![feature(try_trait_v2)] #![feature(try_trait_v2)]
pub mod api; pub mod api;
#[cfg(debug_assertions)]
mod debug;
mod i18n; mod i18n;
mod model; mod model;
mod pages; mod pages;
@ -79,15 +81,25 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
.and_then(|el: web_sys::Element| el.get_attribute("href")), .and_then(|el: web_sys::Element| el.get_attribute("href")),
shared: shared::Model::default(), shared: shared::Model::default(),
i18n: I18n::load(), i18n: I18n::load(),
#[cfg(debug_assertions)]
debug_modal: false,
}; };
session::init(&mut model, orders); session::init(&mut model, orders);
shared::init(&mut model, orders);
#[cfg(debug_assertions)]
crate::debug::init(&mut model, orders);
model model
} }
fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) { fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
match msg { match msg {
#[cfg(debug_assertions)]
Msg::NoOp => {
orders.skip();
}
Msg::Shared(msg) => { Msg::Shared(msg) => {
shared::update(msg, &mut model.shared, orders); shared::update(msg, &mut model.shared, orders);
} }
@ -119,16 +131,27 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
Msg::Session(msg) => { Msg::Session(msg) => {
session::update(msg, model, orders); session::update(msg, model, orders);
} }
#[cfg(debug_assertions)]
Msg::Debug(msg) => {
crate::debug::update(msg, model);
}
} }
} }
fn view(model: &Model) -> Node<Msg> { fn view(model: &Model) -> Node<Msg> {
match &model.page { let view = match &model.page {
Page::Public(PublicPage::Listing(page)) => pages::public::listing::view(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::Product(page)) => pages::public::product::view(model, page),
Page::Public(PublicPage::SignIn(page)) => pages::public::sign_in::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), Page::Public(PublicPage::SignUp(page)) => pages::public::sign_up::view(model, page),
_ => empty![], _ => empty![],
};
if cfg!(debug_assertions) {
use seed::*;
div![crate::debug::view(model), view]
} else {
view
} }
} }

View File

@ -2,6 +2,7 @@ use seed::Url;
use crate::{I18n, Page}; use crate::{I18n, Page};
#[derive(Debug)]
pub struct Model { pub struct Model {
pub url: Url, pub url: Url,
pub token: Option<String>, pub token: Option<String>,
@ -9,4 +10,6 @@ pub struct Model {
pub logo: Option<String>, pub logo: Option<String>,
pub shared: crate::shared::Model, pub shared: crate::shared::Model,
pub i18n: I18n, pub i18n: I18n,
#[cfg(debug_assertions)]
pub debug_modal: bool,
} }

View File

@ -8,13 +8,18 @@ use crate::shared;
#[derive(Debug)] #[derive(Debug)]
pub enum Msg { pub enum Msg {
#[cfg(debug_assertions)]
NoOp,
Public(public::Msg), Public(public::Msg),
Admin(admin::Msg), Admin(admin::Msg),
UrlChanged(subs::UrlChanged), UrlChanged(subs::UrlChanged),
Shared(shared::Msg), Shared(shared::Msg),
Session(crate::session::SessionMsg), Session(crate::session::SessionMsg),
#[cfg(debug_assertions)]
Debug(crate::debug::DebugMsg),
} }
#[derive(Debug)]
pub enum AdminPage { pub enum AdminPage {
Landing, Landing,
Dashboard, Dashboard,
@ -22,6 +27,7 @@ pub enum AdminPage {
Product, Product,
} }
#[derive(Debug)]
pub enum PublicPage { pub enum PublicPage {
Listing(public::listing::ListingPage), Listing(public::listing::ListingPage),
Product(public::product::ProductPage), Product(public::product::ProductPage),
@ -31,6 +37,7 @@ pub enum PublicPage {
Checkout, Checkout,
} }
#[derive(Debug)]
pub enum Page { pub enum Page {
Admin(AdminPage), Admin(AdminPage),
Public(PublicPage), Public(PublicPage),

View File

@ -1,11 +1,17 @@
use seed::app::Orders; use seed::app::Orders;
pub use crate::shared::msg::Msg;
pub mod msg;
pub mod notification; pub mod notification;
pub mod view; pub mod view;
#[derive(Debug)]
pub enum Msg {
LoadMe,
MeLoaded(crate::api::NetRes<model::Account>),
SignIn(model::api::SignInInput),
SignedIn(crate::api::NetRes<model::api::SessionOutput>),
Notification(crate::shared::notification::NotificationMsg),
}
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Model { pub struct Model {
pub access_token: Option<model::AccessTokenString>, pub access_token: Option<model::AccessTokenString>,
@ -15,11 +21,18 @@ pub struct Model {
pub notifications: Vec<notification::Notification>, pub notifications: Vec<notification::Notification>,
} }
pub fn init(_model: &mut crate::Model, orders: &mut impl Orders<crate::Msg>) {
orders.send_msg(crate::Msg::Shared(Msg::LoadMe));
}
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<crate::Msg>) { pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<crate::Msg>) {
match msg { match msg {
Msg::LoadMe => { Msg::LoadMe => {
seed::log!("1");
if let Some(token) = model.access_token.as_ref().cloned() { if let Some(token) = model.access_token.as_ref().cloned() {
orders.skip().perform_cmd(async { seed::log!("2");
orders.skip().perform_cmd(async move {
seed::log!("3");
Msg::MeLoaded(crate::api::public::fetch_me(token).await) Msg::MeLoaded(crate::api::public::fetch_me(token).await)
}); });
} }

View File

@ -1,10 +0,0 @@
use seed::fetch::Result;
#[derive(Debug)]
pub enum Msg {
LoadMe,
MeLoaded(crate::api::NetRes<model::Account>),
SignIn(model::api::SignInInput),
SignedIn(crate::api::NetRes<model::api::SessionOutput>),
Notification(crate::shared::notification::NotificationMsg),
}