Skip to content

Commit

Permalink
Bridge progress
Browse files Browse the repository at this point in the history
  • Loading branch information
djkato committed Sep 27, 2024
1 parent 59c1ec0 commit b21d49a
Show file tree
Hide file tree
Showing 17 changed files with 1,036 additions and 993 deletions.
1,688 changes: 820 additions & 868 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ iso_currency = { version = "0.4.4", features = ["with-serde", "iterator"] }
pulldown-cmark = "0.11.0"
http = "1"
thiserror = "1.0.61"
wasm-bindgen = "=0.2.92"
wasm-bindgen = "=0.2.93"
console_error_panic_hook = "0.1"
leptos = { version = "0.6", features = ["nightly"] }
leptos_axum = { version = "0.6" }
Expand Down
15 changes: 13 additions & 2 deletions app-template-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ http = { workspace = true }
pulldown-cmark = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
saleor-app-sdk = { workspace = true, features = ["bridge"], optional = true }
dotenvy = { workspace = true }
envy = { workspace = true }
cynic = { workspace = true, features = ["http-surf"], optional = true }
surf = { workspace = true, optional = true }
saleor-app-sdk = { workspace = true, optional = true }

[build-dependencies]
cynic-codegen = { workspace = true, optional = true }

[features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
ssr = [
"dep:axum",
"dep:tokio",
Expand All @@ -55,6 +54,11 @@ ssr = [
"dep:leptos_axum",
"dep:tracing",
"dep:saleor-app-sdk",
"saleor-app-sdk/file_apl",
"saleor-app-sdk/redis_apl",
"saleor-app-sdk/tracing",
"saleor-app-sdk/middleware",
"saleor-app-sdk/webhook_utils",
"dep:tracing-subscriber",
"dep:anyhow",
"dep:cynic",
Expand All @@ -64,6 +68,13 @@ ssr = [
"leptos_meta/ssr",
"leptos_router/ssr",
]
hydrate = [
"leptos/hydrate",
"leptos_meta/hydrate",
"leptos_router/hydrate",
"dep:saleor-app-sdk",
"saleor-app-sdk/bridge",
]


# Defines a size-optimized profile for the WASM bundle in release mode
Expand Down
13 changes: 8 additions & 5 deletions app-template-ui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::error_template::{AppError, ErrorTemplate};
use crate::routes::extensions::order_to_pdf::Pdf;
use crate::routes::home::Home;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use saleor_app_sdk::bridge::AppBridge;

#[derive(Params, PartialEq)]
pub struct UrlAppParams {
Expand All @@ -13,22 +15,23 @@ pub struct UrlAppParams {
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context();

let app_bridge = AppBridge::new(Some(true)).unwrap();
view! {
<Stylesheet id="leptos" href="/pkg/saleor-app-template-ui.css"/>
<Stylesheet id="leptos" href="/pkg/saleor-app-template-ui.css" />

// sets the document title
<Title text="Example UI App template in Rust"/>
<Title text="Example UI App template in Rust" />

// content for this welcome page
<Router fallback=|| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
view! { <ErrorTemplate outside_errors/> }.into_view()
view! { <ErrorTemplate outside_errors /> }.into_view()
}>
<main class="p-4 md:p-8 md:px-16">
<Routes>
<Route path="/" view=Home/>
<Route path="/" view=Home />
<Route path="/extensions/order_to_pdf" view=Pdf />
</Routes>
</main>
</Router>
Expand Down
52 changes: 10 additions & 42 deletions app-template-ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn main() -> Result<(), std::io::Error> {
use leptos_axum::{generate_route_list, LeptosRoutes };
use app::*;
use fileserv::file_and_error_handler;
use saleor_app_sdk::middleware::verify_webhook_signature::webhook_signature_verifier;
use saleor_app_sdk::{manifest::{extension::AppExtensionBuilder, AppExtensionMount, AppExtensionTarget}, middleware::verify_webhook_signature::webhook_signature_verifier};
use tokio::sync::Mutex;
use saleor_app_sdk::{
cargo_info,
Expand All @@ -49,48 +49,16 @@ async fn main() -> Result<(), std::io::Error> {
let saleor_app = SaleorApp::new(&config).unwrap();

let app_manifest = AppManifestBuilder::new(&config, cargo_info!())
.add_webhook(
WebhookManifestBuilder::new(&config)
.set_query(
r#"
subscription QueryProductsChanged {
event {
... on ProductUpdated {
product {
... BaseProduct
}
}
... on ProductCreated {
product {
... BaseProduct
}
}
... on ProductDeleted {
product {
... BaseProduct
}
}
}
}
fragment BaseProduct on Product {
id
slug
name
category {
slug
}
}
"#,
)
.add_async_events(vec![
AsyncWebhookEventType::ProductCreated,
AsyncWebhookEventType::ProductUpdated,
AsyncWebhookEventType::ProductDeleted,
])
.build(),
)
.add_permission(AppPermission::ManageProducts)
.add_extension(
AppExtensionBuilder::new()
.set_url("/extensions/order_to_pdf")
.set_label("Order to PDF")
.add_permissions(vec![AppPermission::ManageOrders, AppPermission::ManageProducts, AppPermission::ManageProductTypesAndAttributes])
.set_mount(AppExtensionMount::OrderDetailsMoreActions)
.set_target(AppExtensionTarget::Popup)
.build()
)
.build();

let app_state = AppState{
Expand Down
1 change: 1 addition & 0 deletions app-template-ui/src/routes/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod order_to_pdf;
9 changes: 9 additions & 0 deletions app-template-ui/src/routes/extensions/order_to_pdf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use leptos::*;

#[component]
pub fn Pdf() -> impl IntoView {
view! {
<h1>Yello!</h1>
<p class="italic text-lg">r#"Loading AppBridge, please wait..."#</p>
}
}
1 change: 1 addition & 0 deletions app-template-ui/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "ssr")]
pub mod api;
pub mod extensions;
pub mod home;
15 changes: 15 additions & 0 deletions sdk/.neoconf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"lspconfig": {
"rust_analyzer": {
"rust-analyzer.cargo.features": "all"
/*
use this only when working with leptos
*/
// "rust-analyzer.rustfmt.overrideCommand": [
// "leptosfmt",
// "--stdin",
// "--rustfmt"
// ]
}
}
}
6 changes: 3 additions & 3 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tracing-subscriber = { workspace = true, optional = true }
## Needed for bridge
wasm-bindgen = { workspace = true, optional = true }
serde-wasm-bindgen = { version = "0.6.5", optional = true }
bus = { version = "2.4.1", optional = true }
# bus = { version = "2.4.1", optional = true }

[dependencies.web-sys]
optional = true
Expand All @@ -66,7 +66,7 @@ features = [
[dev-dependencies]

[features]
default = ["middleware", "redis_apl", "webhook_utils", "tracing"]
default = []
middleware = [
"dep:axum",
"dep:jsonwebtoken",
Expand All @@ -80,7 +80,7 @@ webhook_utils = ["dep:http"]
tracing = ["dep:tracing", "dep:tracing-subscriber"]
bridge = [
"dep:wasm-bindgen",
"dep:bus",
# "dep:bus",
"dep:serde-wasm-bindgen",
"dep:web-sys",
]
118 changes: 60 additions & 58 deletions sdk/src/bridge/event.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
use crate::locales::LocaleCode;

use super::ThemeType;
use bus::{Bus, BusReader};
// use bus::{Bus, BusReader};
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;

pub struct EventChannels {
pub handshake: Bus<PayloadHanshake>,
pub response: Bus<PayloadResponse>,
pub redirect: Bus<PayloadRedirect>,
pub theme: Bus<PayloadTheme>,
pub locale_changed: Bus<PayloadLocaleChanged>,
pub token_refreshed: Bus<PayloadTokenRefreshed>,
}

impl EventChannels {
pub fn new() -> Self {
Self {
handshake: Bus::new(10),
response: Bus::new(10),
redirect: Bus::new(10),
theme: Bus::new(10),
locale_changed: Bus::new(10),
token_refreshed: Bus::new(10),
}
}

pub fn subscribe_handshake(&mut self) -> BusReader<PayloadHanshake> {
self.handshake.add_rx()
}

pub fn subscribe_response(&mut self) -> BusReader<PayloadResponse> {
self.response.add_rx()
}

pub fn subscribe_redirect(&mut self) -> BusReader<PayloadRedirect> {
self.redirect.add_rx()
}

pub fn subscribe_theme(&mut self) -> BusReader<PayloadTheme> {
self.theme.add_rx()
}

pub fn subscribe_locale_changed(&mut self) -> BusReader<PayloadLocaleChanged> {
self.locale_changed.add_rx()
}

pub fn subscribe_token_refreshed(&mut self) -> BusReader<PayloadTokenRefreshed> {
self.token_refreshed.add_rx()
}
}

#[derive(EnumIter, Debug)]
pub enum EventType {
Handshake,
Response,
Redirect,
Theme,
LocaleChanged,
TokenRefreshed,
}
// use strum_macros::EnumIter;
// use web_sys::js_sys::Object;

// pub struct EventChannels {
// pub handshake: Bus<PayloadHanshake>,
// pub response: Bus<PayloadResponse>,
// pub redirect: Bus<PayloadRedirect>,
// pub theme: Bus<PayloadTheme>,
// pub locale_changed: Bus<PayloadLocaleChanged>,
// pub token_refreshed: Bus<PayloadTokenRefreshed>,
// }
//
// impl EventChannels {
// pub fn new() -> Self {
// Self {
// handshake: Bus::new(10),
// response: Bus::new(10),
// redirect: Bus::new(10),
// theme: Bus::new(10),
// locale_changed: Bus::new(10),
// token_refreshed: Bus::new(10),
// }
// }
//
// pub fn subscribe_handshake(&mut self) -> BusReader<PayloadHanshake> {
// self.handshake.add_rx()
// }
//
// pub fn subscribe_response(&mut self) -> BusReader<PayloadResponse> {
// self.response.add_rx()
// }
//
// pub fn subscribe_redirect(&mut self) -> BusReader<PayloadRedirect> {
// self.redirect.add_rx()
// }
//
// pub fn subscribe_theme(&mut self) -> BusReader<PayloadTheme> {
// self.theme.add_rx()
// }
//
// pub fn subscribe_locale_changed(&mut self) -> BusReader<PayloadLocaleChanged> {
// self.locale_changed.add_rx()
// }
//
// pub fn subscribe_token_refreshed(&mut self) -> BusReader<PayloadTokenRefreshed> {
// self.token_refreshed.add_rx()
// }
// }

// #[derive(EnumIter, Debug)]
// pub enum EventType {
// Handshake,
// Response,
// Redirect,
// Theme,
// LocaleChanged,
// TokenRefreshed,
// }

#[derive(Deserialize, Serialize, Debug)]
#[serde(tag = "type", content = "payload")]
Expand All @@ -71,6 +72,7 @@ pub enum Event {
Theme(PayloadTheme),
LocaleChanged(PayloadLocaleChanged),
TokenRefreshed(PayloadTokenRefreshed),
NotifyReady(String),
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
Loading

0 comments on commit b21d49a

Please sign in to comment.