From d6a102bf2972557d6804092bf62c2fe0cba4b175 Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Wed, 5 Jun 2024 14:56:36 +0200 Subject: [PATCH] Added user context and router fixes --- src/components/layout/navbar.rs | 6 ++++-- src/main.rs | 14 +++++++++++--- src/server/auth.rs | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/components/layout/navbar.rs b/src/components/layout/navbar.rs index 3cf4fec..3eb5313 100644 --- a/src/components/layout/navbar.rs +++ b/src/components/layout/navbar.rs @@ -1,3 +1,4 @@ +use crate::Route; use dioxus::prelude::*; #[component] @@ -7,15 +8,16 @@ pub fn Navbar() -> Element { class: "navbar bg-base-200", div { class: "flex-1", - a { class: "btn btn-ghost text-xl", href: "/", "XVMCMM" }, + Link { class: "btn btn-ghost text-xl", to: Route::Home {}, "XVMCMM" }, } div { class: "flex-none", ul { class: "px-1 py-1", - li { a { class: "btn btn-primary", href: "/auth", "Sign In" } }, + li { Link { class: "btn btn-primary", to: Route::Auth {}, "Sign In" } }, } } } + Outlet:: {} } } diff --git a/src/main.rs b/src/main.rs index 1f2e29f..2f7434c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,16 @@ use tracing::{info, Level}; use components::auth::Auth; use components::home::Home; use components::layout::navbar::Navbar; +use server::auth; -#[derive(Clone, Routable, Debug, PartialEq, serde::Serialize, serde::Deserialize)] -enum Route { +pub type UserState = Resource>; + +#[derive(Clone, Routable)] +pub enum Route { + #[layout(Navbar)] #[route("/")] Home {}, + #[route("/auth")] Auth {}, } @@ -57,8 +62,11 @@ fn main() { } fn app() -> Element { + let user = use_resource(auth::get_current_user); + + use_context_provider(|| user); + rsx! { - Navbar {}, Router:: {}, } } diff --git a/src/server/auth.rs b/src/server/auth.rs index de0229d..352269f 100644 --- a/src/server/auth.rs +++ b/src/server/auth.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; #[cfg(feature = "server")] use surrealdb::sql::Thing; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct User { pub id: String, pub email: String,