From ceb99599086862fcfcf660410eb5d5bb123f96bf Mon Sep 17 00:00:00 2001
From: Marc-Antoine Manningham <marc-antoine.m@outlook.com>
Date: Sat, 23 Nov 2024 18:15:48 -0500
Subject: [PATCH] feat: finish server integration, more optimization is needed
 and fixes

---
 aep-schedule-website/src/backend/fileserv.rs |  2 +-
 aep-schedule-website/src/backend/state.rs    | 16 ++++++++++------
 aep-schedule-website/src/frontend/app.rs     | 15 +++++----------
 aep-schedule-website/src/lib.rs              |  3 +--
 aep-schedule-website/src/main.rs             |  2 +-
 5 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/aep-schedule-website/src/backend/fileserv.rs b/aep-schedule-website/src/backend/fileserv.rs
index bde435f..904ebf2 100644
--- a/aep-schedule-website/src/backend/fileserv.rs
+++ b/aep-schedule-website/src/backend/fileserv.rs
@@ -21,7 +21,7 @@ pub async fn file_and_error_handler(
     if res.status() == StatusCode::OK {
         res.into_response()
     } else {
-        let handler = leptos_axum::render_app_to_stream(options.to_owned(), move || view! {<App/>});
+        let handler = leptos_axum::render_app_to_stream(move || view! {<App/>});
         handler(req).await.into_response()
     }
 }
diff --git a/aep-schedule-website/src/backend/state.rs b/aep-schedule-website/src/backend/state.rs
index 1379dc7..783d6e7 100644
--- a/aep-schedule-website/src/backend/state.rs
+++ b/aep-schedule-website/src/backend/state.rs
@@ -1,3 +1,4 @@
+use crate::frontend::app::shell;
 use crate::frontend::app::App;
 use aep_schedule_generator::data::courses::Courses;
 use aep_schedule_generator::icalendar::calendar::Calendar;
@@ -10,7 +11,7 @@ use axum::{
 };
 use leptos::prelude::*;
 use leptos_axum::handle_server_fns_with_context;
-use leptos_router::RouteListing;
+use leptos_axum::AxumRouteListing;
 use std::fs::File;
 use std::io::BufReader;
 use std::sync::Arc;
@@ -31,11 +32,11 @@ pub struct AppState {
     pub courses: Arc<RwLock<Courses>>,
     pub calendar: Arc<RwLock<Calendar>>,
     pub users_to_notify: Arc<Mutex<UsersToNotify>>,
-    pub routes: Vec<RouteListing>,
+    pub routes: Vec<AxumRouteListing>,
 }
 
 impl AppState {
-    pub async fn new(leptos_options: LeptosOptions, routes: Vec<RouteListing>) -> Self {
+    pub async fn new(leptos_options: LeptosOptions, routes: Vec<AxumRouteListing>) -> Self {
         #[cfg(not(debug_assertions))]
         {
             // Don't spam Poly when reloading the website in debug mode
@@ -130,15 +131,18 @@ pub async fn leptos_routes_handler(
     State(app_state): State<AppState>,
     req: Request<AxumBody>,
 ) -> Response {
+    let state = axum::extract::State(app_state.clone());
     let handler = leptos_axum::render_route_with_context(
-        app_state.leptos_options.clone(),
         app_state.routes.clone(),
         move || {
             provide_context(app_state.calendar.clone());
             provide_context(app_state.courses.clone());
             provide_context(app_state.users_to_notify.clone());
         },
-        App,
+        {
+            let leptos_options = app_state.leptos_options.clone();
+            move || shell(leptos_options.clone())
+        },
     );
-    handler(req).await.into_response()
+    handler(state, req).await.into_response()
 }
diff --git a/aep-schedule-website/src/frontend/app.rs b/aep-schedule-website/src/frontend/app.rs
index d0bbb14..b1648d9 100644
--- a/aep-schedule-website/src/frontend/app.rs
+++ b/aep-schedule-website/src/frontend/app.rs
@@ -14,12 +14,13 @@ use leptos_router::{
 pub fn shell(options: LeptosOptions) -> impl IntoView {
     view! {
         <!DOCTYPE html>
-        <html lang="en">
+        <html lang="fr">
             <head>
                 <meta charset="utf-8"/>
                 <meta name="viewport" content="width=device-width, initial-scale=1"/>
                 <AutoReload options=options.clone() />
                 <HydrationScripts options/>
+                <link rel="stylesheet" id="leptos" href="/pkg/aep-schedule-website.css"/>
                 <MetaTags/>
             </head>
             <body>
@@ -66,17 +67,11 @@ pub fn Nav() -> impl IntoView {
 
 #[component]
 pub fn App() -> impl IntoView {
-    view! {
-
-        // injects a stylesheet into the document <head>
-        // id=leptos means cargo-leptos will hot-reload this stylesheet
-        <Stylesheet id="leptos" href="/pkg/aep-schedule-website.css"/>
+    provide_meta_context();
 
-        // sets the document title
-        <Title text="Générateur d'horaire"/>
-
-        // content for this welcome page
+    view! {
         <Router>
+            <Title text="Générateur d'horaire"/>
             <Nav/>
             <main class="h-full">
                 <FlatRoutes fallback=|| "Not found">
diff --git a/aep-schedule-website/src/lib.rs b/aep-schedule-website/src/lib.rs
index da5d161..986ebd0 100644
--- a/aep-schedule-website/src/lib.rs
+++ b/aep-schedule-website/src/lib.rs
@@ -3,7 +3,6 @@ pub mod backend;
 pub mod frontend;
 
 cfg_if::cfg_if! { if #[cfg(feature = "hydrate")] {
-    use leptos::prelude::*;
     use wasm_bindgen::prelude::wasm_bindgen;
     use crate::frontend::app::App;
 
@@ -13,6 +12,6 @@ cfg_if::cfg_if! { if #[cfg(feature = "hydrate")] {
         _ = console_log::init_with_level(log::Level::Debug);
         console_error_panic_hook::set_once();
 
-        leptos::mount_to_body(App);
+        leptos::mount::mount_to_body(App);
     }
 }}
diff --git a/aep-schedule-website/src/main.rs b/aep-schedule-website/src/main.rs
index ae90c0a..1bb0489 100644
--- a/aep-schedule-website/src/main.rs
+++ b/aep-schedule-website/src/main.rs
@@ -19,7 +19,7 @@ cfg_if::cfg_if!(if #[cfg(feature = "ssr")] {
         // <https://github.com/leptos-rs/start-axum#executing-a-server-on-a-remote-machine-without-the-toolchain>
         // Alternately a file can be specified such as Some("Cargo.toml")
         // The file would need to be included with the executable when moved to deployment
-        let conf = get_configuration(None).await.unwrap();
+        let conf = get_configuration(None).unwrap();
         let leptos_options = conf.leptos_options;
         let routes = generate_route_list(App);
         let state = AppState::new(leptos_options.clone(), routes.clone()).await;
-- 
GitLab