Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 10, 2024
1 parent 5dfec76 commit 9a29709
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ pub fn index(users: Vec<User>) -> String {
let page = rsx! {
Layout { // <-- Use our layout
title: "Users Table",
stylesheets: vec![],
header: rsx!(),
sidebar: rsx!(),
sidebar_header: rsx!(),
sidebar_footer: rsx!(),
table {
thead {
tr {
Expand All @@ -186,8 +181,9 @@ pub fn index(users: Vec<User>) -> String {
for user in users {
tr {
td {
// 👇 We added the image
img {
src: format!("/static/{}", favicon_svg.name),
src: favicon_svg.name,
width: "16",
height: "16"
}
Expand Down
15 changes: 2 additions & 13 deletions rust-on-nails.com/content/docs/full-stack-web/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ In the following form we use an email type and a required attribute. The browser
</form>
```

We can write this same form using Dioxus. Update `crates/ui-components/src/root.rs` with a form to add users.
We can write this same form using Dioxus. Update `crates/web-pages/src/root.rs` with a form to add users.

```rust
use crate::{layout::Layout, render};
Expand All @@ -40,11 +40,6 @@ pub fn index(users: Vec<User>) -> String {
let page = rsx! {
Layout { // <-- Use our layout
title: "Users Table",
stylesheets: vec![],
header: rsx!(),
sidebar: rsx!(),
sidebar_header: rsx!(),
sidebar_footer: rsx!(),
table {
thead {
tr {
Expand Down Expand Up @@ -90,14 +85,8 @@ pub fn index(users: Vec<User>) -> String {

_Note:_ `for` and `type` are Rust keywords. We must prefix them with `r#` so Rust knows that we want the raw string literal of "for" and "type".

## Handling form submission
## Handling form submission with Actions

We need to install [serde](https://serde.rs/) to transform the HTTP body into a Rust struct.

```bash
cd crates/web-server
cargo add [email protected] --features derive
```

[Axum](https://github.com/tokio-rs/axum) has support for [Handlers](https://docs.rs/axum/latest/axum/handler/index.html). We can use those in a lot of different ways and one way is for form implementations. We are going to create a `create_form` handler to save new users to our database.

Expand Down
38 changes: 26 additions & 12 deletions rust-on-nails.com/content/docs/full-stack-web/web-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,26 @@ It's a big one as I've added some styling which we won't use until later.
#![allow(non_snake_case)]
use dioxus::prelude::*;

#[component]
pub fn Layout(title: String, children: Element) -> Element {
rsx! {
BaseLayout {
title,
stylesheets: vec![],
header: rsx!(),
sidebar: rsx!(),
sidebar_header: rsx!(),
sidebar_footer: rsx!(),
children,
}
}
}


#[derive(Props, Clone, PartialEq)]
pub struct LayoutProps {
pub struct BaseLayoutProps {
title: String,
fav_icon_src: String,
fav_icon_src: Option<String>,
stylesheets: Vec<String>,
js_href: Option<String>,
header: Element,
Expand All @@ -60,7 +76,7 @@ pub struct LayoutProps {
sidebar_header: Element,
}

pub fn Layout(props: LayoutProps) -> Element {
pub fn BaseLayout(props: BaseLayoutProps) -> Element {
rsx!(
head {
title {
Expand Down Expand Up @@ -90,10 +106,12 @@ pub fn Layout(props: LayoutProps) -> Element {
src: "{js_href}"
}
}
link {
rel: "icon",
"type": "image/svg+xml",
href: "{props.fav_icon_src}"
if let Some(fav_icon_src) = props.fav_icon_src {
link {
rel: "icon",
"type": "image/svg+xml",
href: "{fav_icon_src}"
}
}
}
body {
Expand Down Expand Up @@ -184,16 +202,12 @@ Create a file `crates/web-pages/src/root.rs`. we call it `root.rs` because it's
use crate::{layout::Layout, render};
use db::User;
use dioxus::prelude::*;
use web_assets::files::favicon_svg;

pub fn index(users: Vec<User>) -> String {
let page = rsx! {
Layout { // <-- Use our layout
title: "Users Table",
stylesheets: vec![],
header: rsx!(),
sidebar: rsx!(),
sidebar_header: rsx!(),
sidebar_footer: rsx!(),
table {
thead {
tr {
Expand Down

0 comments on commit 9a29709

Please sign in to comment.