Skip to content

Commit

Permalink
Start
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanbazz committed Aug 25, 2020
0 parents commit 63868b5
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
Cargo.lock

http://www.sheshbabu.com/posts/rust-wasm-yew-single-page-application/
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "rustSPA"
version = "0.1.0"
authors = ["iwanbazz <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib","rlib"]

[dependencies]
yew = "0.17"
wasm-bindgen = "0.2"
8 changes: 8 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tasks.build]
command = "wasm-pack"
args = ["build", "--dev", "--target", "web", "--out-name", "wasm", "--out-dir", "./static"]
watch = {ignore_pattern = "static/*"}

[tasks.serve]
command = "simple-http-server"
args = ["-i", "./static/", "-p", "3000", "--nocache", "--try-file", "./static/index.html"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Web assembly on top of Rust.

Single Page Applications using Rust
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod pages;

use pages::Home;
use wasm_bindgen::prelude::*;
use yew::prelude::*;

#[wasm_bindgen(start)]
pub fn run_app() {
App::<Home>::new().mount_to_body();
}
72 changes: 72 additions & 0 deletions src/pages/home.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use yew::prelude::*;

struct Product {
id: i32,
name: String,
description: String,
image: String,
price: f64,
}

struct State {
products: Vec<Product>,
}

pub struct Home {
state: State,
}

impl Component for Home {
type Message = ();
type Properties = ();

fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
let products: Vec<Product> = vec![
Product {
id: 1,
name: "Apple".to_string(),
description: "An apple a day keeps the doctor away".to_string(),
image: "/products/apple.png".to_string(),
price: 3.65,
},
Product {
id: 2,
name: "Banana".to_string(),
description: "An old banana leaf was once young and green".to_string(),
image: "/products/banana.png".to_string(),
price: 7.99,
},
];
Self {
state: State {
products,
},
}
}

fn update(&mut self, _: Self::Message) -> ShouldRender {
true
}

fn change(&mut self, _: Self::Properties) -> ShouldRender {
true
}

fn view(&self) -> Html {
let products: Vec<Html> = self
.state
.products
.iter()
.map(|product: &Product| {
html! {
<div>
<img src={&product.image}/>
<div>{&product.name}</div>
<div>{"$"}{&product.price}</div>
</div>
}
})
.collect();
html! { <span>{products}</span> }
}
}
3 changes: 3 additions & 0 deletions src/pages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod home;

pub use home::Home;

0 comments on commit 63868b5

Please sign in to comment.