From 165d68ca50dd24a9b8bc3d7c55db271b7f51c2a5 Mon Sep 17 00:00:00 2001 From: Franz Dietrich Date: Wed, 31 Jan 2024 20:32:53 +0100 Subject: [PATCH] =?UTF-8?q?Datum=20ausw=C3=A4hlbar=20machen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- terminwahl_back/src/api/read.rs | 8 +++ terminwahl_back/src/db/read.rs | 18 +++++- terminwahl_back/src/main.rs | 1 + terminwahl_front/src/main.rs | 101 ++++++++++++++++++++++++------- terminwahl_front/src/requests.rs | 10 +-- terminwahl_typen/src/lib.rs | 10 +-- 6 files changed, 113 insertions(+), 35 deletions(-) diff --git a/terminwahl_back/src/api/read.rs b/terminwahl_back/src/api/read.rs index 1902230..c209f16 100644 --- a/terminwahl_back/src/api/read.rs +++ b/terminwahl_back/src/api/read.rs @@ -40,3 +40,11 @@ pub async fn get_unavailable_json(pool: web::Data) -> Result) -> Result { + let dates = db::read::get_dates(&pool) + .await + .map_err(error::ErrorInternalServerError)?; + + Ok(HttpResponse::Ok().json(dates)) +} diff --git a/terminwahl_back/src/db/read.rs b/terminwahl_back/src/db/read.rs index aa91ca5..7052c47 100644 --- a/terminwahl_back/src/db/read.rs +++ b/terminwahl_back/src/db/read.rs @@ -5,7 +5,8 @@ use serde::{Deserialize, Serialize}; use sqlx::query_as; use terminwahl_typen::{ - AppointmentSlot, AppointmentSlots, IdType, Nutzer, SlotId, Subject, Subjects, Teacher, Teachers, + AppointmentSlot, AppointmentSlots, Date, Dates, IdType, Nutzer, SlotId, Subject, Subjects, + Teacher, Teachers, }; use super::Pool; @@ -93,7 +94,20 @@ pub async fn get_unavailable(db: &Pool) -> Result, sqlx::Error> Err(e) => Err(e), } } - +pub async fn get_dates(db: &Pool) -> Result { + match query_as!( + Date, + r#" + SELECT * + FROM `date`;"#, + ) + .fetch_all(db) + .await + { + Ok(elems) => Ok(elems.into_iter().collect()), + Err(e) => Err(e), + } +} #[derive(Debug, Deserialize, Serialize, Clone)] pub struct TeacherWithAppointments { teacher: Teacher, diff --git a/terminwahl_back/src/main.rs b/terminwahl_back/src/main.rs index e71370f..11348b0 100644 --- a/terminwahl_back/src/main.rs +++ b/terminwahl_back/src/main.rs @@ -69,6 +69,7 @@ async fn main() -> std::io::Result<()> { .wrap(Logger::default()) .wrap(session_store) .wrap(error_handlers) + .service(web::resource("/get/dates").route(web::get().to(api::read::get_dates_json))) .service( web::resource("/get/teachers/{date_key}") .route(web::get().to(api::read::get_teachers_json)), diff --git a/terminwahl_front/src/main.rs b/terminwahl_front/src/main.rs index 4f46382..0c8abef 100644 --- a/terminwahl_front/src/main.rs +++ b/terminwahl_front/src/main.rs @@ -4,8 +4,8 @@ use std::collections::{HashMap, HashSet}; use gloo::console::log; use requests::{fetch_dates, fetch_slots, fetch_teachers, fetch_unavailable, send_appointments}; use terminwahl_typen::{ - AppointmentSlot, AppointmentSlots, Dates, IdType, Nutzer, PlannedAppointment, RequestState, - SlotId, Teacher, Teachers, + AppointmentSlot, AppointmentSlots, Date, Dates, IdType, Nutzer, PlannedAppointment, + RequestState, SlotId, Teacher, Teachers, }; use web_sys::HtmlInputElement; use yew::prelude::*; @@ -16,11 +16,12 @@ pub enum Msg { UpdateSchüler(String), UpdateEmail(String), DataEntered(Nutzer), - GetTeachers, + GetTeachers(IdType), ReceivedTeachers(Teachers), GetDates, ReceivedDates(Dates), - GetSlots, + SelectDate(IdType), + GetSlots(IdType), ReceivedSlots(AppointmentSlots), Selected(PlannedAppointment), TooMany, @@ -33,6 +34,7 @@ pub struct App { nutzer: Option, tmp_nutzer: Nutzer, dates: Option, + selected_date: Option, teachers: Option, slots: Option, appointments: HashMap, @@ -58,6 +60,7 @@ impl Component for App { appointments: HashMap::new(), slots: None, dates: None, + selected_date: None, unavailable: None, teachers: None, nutzer: None, @@ -89,8 +92,8 @@ impl Component for App { true } Msg::TooMany => todo!(), - Msg::GetTeachers => { - ctx.link().send_future(fetch_teachers()); + Msg::GetTeachers(id) => { + ctx.link().send_future(fetch_teachers(id)); false } Msg::ReceivedTeachers(teachers) => { @@ -102,13 +105,25 @@ impl Component for App { false } Msg::ReceivedDates(dates) => { + if dates.len() == 1 { + ctx.link() + .send_message(Msg::SelectDate(dates.first().unwrap().id)) + } self.dates = Some(dates); - ctx.link().send_message(Msg::GetTeachers); - ctx.link().send_message(Msg::GetSlots); true } - Msg::GetSlots => { - ctx.link().send_future(fetch_slots()); + Msg::SelectDate(date_id) => { + ctx.link().send_message(Msg::GetTeachers(date_id)); + ctx.link().send_message(Msg::GetSlots(date_id)); + let date = self + .dates + .as_ref() + .map(|dts| dts.iter().find(|d| d.id == date_id).unwrap()); + self.selected_date = Some(date.expect("A date should be found").clone()); + true + } + Msg::GetSlots(id) => { + ctx.link().send_future(fetch_slots(id)); ctx.link().send_future(fetch_unavailable()); false } @@ -164,24 +179,35 @@ impl Component for App { html! {<>
- -

- {"Am 28.02.23"} -

+ + {if let Some(d) = self.selected_date.as_ref(){ html!{ + <>

+ {&d.subtitle} +

+ {"Am "}{d.start_time.format("%d.%m.%Y")} +

}}else{html!()}}
{ - if let Some(_saved) = self.successfully_saved.as_ref(){self.view_dank_dialog(ctx)} else if self.nutzer.is_none(){ - self.view_eingabe_daten(ctx) - } - else - { - self.view_auswahl_termine(ctx) - } + if let Some(dates) = self.dates.as_ref(){ + if let Some(date) = self.selected_date.as_ref(){ + if let Some(_saved) = self.successfully_saved.as_ref(){ + self.view_dank_dialog(ctx) + } else if self.nutzer.is_none(){ + self.view_eingabe_daten(ctx) + } + else + { + self.view_auswahl_termine(ctx) + } + } else { + self.view_auswahl_date(dates, ctx) + } + }else{html!(

{"Loading"}

)} }
@@ -192,6 +218,35 @@ impl Component for App { } impl App { + fn view_auswahl_date(&self, dates: &Dates, ctx: &Context) -> Html { + let onchange = ctx.link().callback(|e: Event| { + let input: HtmlInputElement = e.target_unchecked_into(); + Msg::SelectDate(input.value().parse().unwrap()) + }); + html! { +
+
+
+ +
+

{"Anmeldung zum Elternsprechtag!"}

{"Bitte wählen Sie den Sprechtag zu welchem Sie sich anmelden möchten:"}

+
+
+ +
+
+
+
+ } + } + fn view_eingabe_daten(&self, ctx: &Context) -> Html { html! {
diff --git a/terminwahl_front/src/requests.rs b/terminwahl_front/src/requests.rs index 61dafba..b965c30 100644 --- a/terminwahl_front/src/requests.rs +++ b/terminwahl_front/src/requests.rs @@ -1,5 +1,5 @@ use gloo::net::http::Request; -use terminwahl_typen::{Nutzer, PlannedAppointment, RequestState}; +use terminwahl_typen::{IdType, Nutzer, PlannedAppointment, RequestState}; use crate::Msg; @@ -15,9 +15,9 @@ pub async fn fetch_dates() -> Result { Ok(Msg::ReceivedDates(response)) } -pub async fn fetch_teachers() -> Result { +pub async fn fetch_teachers(id: IdType) -> Result { // Send the request to the specified URL. - let response = Request::get("/get/teachers/1").send().await; + let response = Request::get(&format!("/get/teachers/{}", id)).send().await; // Return the ZuordnungMessage with the given network object and the response. let response = response .map_err(|_| Msg::AppointmentsSent(RequestState::Error))? @@ -27,9 +27,9 @@ pub async fn fetch_teachers() -> Result { Ok(Msg::ReceivedTeachers(response)) } -pub async fn fetch_slots() -> Result { +pub async fn fetch_slots(id: IdType) -> Result { // Send the request to the specified URL. - let response = Request::get("/get/slots/1").send().await; + let response = Request::get(&format!("/get/slots/{}", id)).send().await; // Return the ZuordnungMessage with the given network object and the response. let response = response .map_err(|_| Msg::AppointmentsSent(RequestState::Error))? diff --git a/terminwahl_typen/src/lib.rs b/terminwahl_typen/src/lib.rs index 03c62cf..e01555d 100644 --- a/terminwahl_typen/src/lib.rs +++ b/terminwahl_typen/src/lib.rs @@ -88,11 +88,11 @@ impl Nutzer { #[derive(Debug, Deserialize, Serialize, Clone)] pub struct Date { - id: IdType, - name: String, - subtitle: String, - start_time: NaiveDateTime, - end_time: NaiveDateTime, + pub id: IdType, + pub name: String, + pub subtitle: String, + pub start_time: NaiveDateTime, + pub end_time: NaiveDateTime, } pub type Dates = Vec;