diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-26 17:43:43 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-26 17:43:43 +0200 |
| commit | 32847efa04029d81f9d8cf7a37999cb3cbb1e145 (patch) | |
| tree | 8a0a7bc135643a6fbf45bf48141d9dfcb2085183 /src | |
| download | web-client-32847efa04029d81f9d8cf7a37999cb3cbb1e145.zip | |
chore: Initial Commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/mod.rs | 1 | ||||
| -rw-r--r-- | src/lib.rs | 59 | ||||
| -rw-r--r-- | src/main.rs | 13 | ||||
| -rw-r--r-- | src/pages/home.rs | 95 | ||||
| -rw-r--r-- | src/pages/mod.rs | 2 | ||||
| -rw-r--r-- | src/pages/not_found.rs | 6 |
6 files changed, 176 insertions, 0 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/components/mod.rs @@ -0,0 +1 @@ + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7d58735 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,59 @@ +#![expect( + unreachable_pub, + reason = "leptos' component macro generates this warning" +)] +#![expect( + clippy::must_use_candidate, + reason = "Can't add it to leptos' components" +)] + +mod components; +mod pages; + +use std::sync::Arc; + +use leptos::prelude::{AddAnyAttr, IntoView, component, view}; +use leptos_meta::{Html, Meta, Title, provide_meta_context}; +use leptos_router::{ + components::{Route, Router, Routes}, + path, +}; +use rocie_client::apis::configuration::Configuration; + +use crate::pages::home::Home; + +#[component] +pub fn App() -> impl IntoView { + // Provides context that manages stylesheets, titles, meta tags, etc. + provide_meta_context(); + + let config = { + let mut config = Configuration::new(); + + config.user_agent = Some("rocie-mobile".to_owned()); + config.base_path = "http://127.0.0.1:8080".to_owned(); + + Arc::new(config) + }; + + view! { + <Html attr:lang="en" attr:dir="ltr" attr:data-theme="light" /> + + <Title text="Welcome to Leptos CSR" /> + + <Meta charset="UTF-8" /> + <Meta name="viewport" content="width=device-width, initial-scale=1.0" /> + + <Router> + <Routes fallback=|| view! { NotFound }> + <Route + path=path!("/") + view=move || { + let local_config = Arc::clone(&config); + view! { <Home config=local_config /> } + } + /> + </Routes> + </Router> + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c377484 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,13 @@ +use leptos::prelude::{mount_to_body, view}; + +use rocie_mobile::App; + +fn main() { + // set up logging + _ = console_log::init_with_level(log::Level::Debug); + console_error_panic_hook::set_once(); + + mount_to_body(|| { + view! { <App /> } + }); +} diff --git a/src/pages/home.rs b/src/pages/home.rs new file mode 100644 index 0000000..9c86833 --- /dev/null +++ b/src/pages/home.rs @@ -0,0 +1,95 @@ +use std::sync::Arc; + +use leptos::{ + IntoView, component, + error::ErrorBoundary, + prelude::{ + ClassAttribute, CollectView, ElementChild, Get, Read, ReadSignal, Set, With, signal, + }, + reactive::spawn_local, + server::{LocalResource, Resource}, + view, +}; +use rocie_client::apis::{ + api_get_product_api::{ProductsError, products}, + configuration::Configuration, +}; + +#[component] +pub fn Home(config: Arc<Configuration>) -> impl IntoView { + let (read_status, write_status) = signal("Loading..".to_owned()); + + { + let local_config = Arc::clone(&config); + + spawn_local(async move { + let products = products(&local_config).await; + + write_status.set( + products + .as_ref() + .map(move |products| { + let products_num = products.len(); + let plural_s = if products_num == 1 { "" } else { "s" }; + let products_value = 2; + let products_currency = "EUR"; + format!( + "You have {products_num} product{plural_s} \ + in stock with a value \ + of {products_value} {products_currency}.", + ) + }) + .unwrap(), + ); + }); + } + + view! { + <ErrorBoundary fallback=|errors| { + view! { + <h1>"Uh oh! Something went wrong!"</h1> + + <p>"Errors: "</p> + // Render a list of errors as strings - good for development purposes + <ul> + {move || { + errors + .get() + .into_iter() + .map(|(_, e)| view! { <li>{e.to_string()}</li> }) + .collect_view() + }} + </ul> + } + }> + + <nav> + <div class="nav"> + <img + src="https://raw.githubusercontent.com/leptos-rs/leptos/main/docs/logos/Leptos_logo_RGB.svg" + alt="Rocie Logo" + height="200" + width="400" + /> + <h1>Rocie</h1> + </div> + </nav> + + <div class="container"> + <div class="container"> + <h1>"Inventory"</h1> + + <p>{read_status}</p> + </div> + + <div class="container"> + <h1>"Recipies"</h1> + </div> + + <div class="container"> + <h1>"Shopping list"</h1> + </div> + </div> + </ErrorBoundary> + } +} diff --git a/src/pages/mod.rs b/src/pages/mod.rs new file mode 100644 index 0000000..8829694 --- /dev/null +++ b/src/pages/mod.rs @@ -0,0 +1,2 @@ +pub mod home; +pub mod not_found; diff --git a/src/pages/not_found.rs b/src/pages/not_found.rs new file mode 100644 index 0000000..7b5c127 --- /dev/null +++ b/src/pages/not_found.rs @@ -0,0 +1,6 @@ +use leptos::{IntoView, component, prelude::ElementChild, view}; + +#[component] +pub fn NotFound() -> impl IntoView { + view! { <h1>"Uh oh!" <br /> "We couldn't find that page!"</h1> } +} |
