Skip to content
Snippets Groups Projects
Commit 390da5ea authored by marcantoinem's avatar marcantoinem
Browse files

refactor: improve schedule generation call

parent babc1792
No related branches found
No related tags found
No related merge requests found
...@@ -6,11 +6,10 @@ use crate::frontend::{ ...@@ -6,11 +6,10 @@ use crate::frontend::{
pages::generator::FirstGenerationDone, pages::generator::FirstGenerationDone,
state::OptionState, state::OptionState,
}; };
use aep_schedule_generator::algorithm::{generation::SchedulesOptions, schedule::Schedule};
use leptos::*; use leptos::*;
#[component] #[component]
pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl IntoView { pub fn OptionsForms() -> impl IntoView {
let state = OptionState::from_context(); let state = OptionState::from_context();
let first_generation_done: FirstGenerationDone = use_context().unwrap(); let first_generation_done: FirstGenerationDone = use_context().unwrap();
...@@ -19,7 +18,7 @@ pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl Int ...@@ -19,7 +18,7 @@ pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl Int
if !first_generation_done.0.get() || state.step.get() != 5 { if !first_generation_done.0.get() || state.step.get() != 5 {
return; return;
} }
action.dispatch((&state).into()); state.generate();
}; };
create_local_resource(state.action_courses.pending(), move |_| { create_local_resource(state.action_courses.pending(), move |_| {
...@@ -27,7 +26,7 @@ pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl Int ...@@ -27,7 +26,7 @@ pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl Int
async move {} async move {}
}); });
let submit_mobile = move |_| action.dispatch((&state).into()); let submit_mobile = move |_| state.generate();
view! { view! {
<CoursesSelector state=state submit/> <CoursesSelector state=state submit/>
......
use std::cmp::Ordering; use std::cmp::Ordering;
use aep_schedule_generator::algorithm::{generation::SchedulesOptions, schedule::Schedule};
use leptos::*; use leptos::*;
use crate::frontend::{pages::generator::FirstGenerationDone, state::OptionState}; use crate::frontend::{pages::generator::FirstGenerationDone, state::OptionState};
...@@ -46,13 +45,13 @@ pub fn Step( ...@@ -46,13 +45,13 @@ pub fn Step(
} }
#[component] #[component]
pub fn Todo(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl IntoView { pub fn Todo() -> impl IntoView {
let state = OptionState::from_context(); let state = OptionState::from_context();
let first_generation_done: FirstGenerationDone = use_context().unwrap(); let first_generation_done: FirstGenerationDone = use_context().unwrap();
let submit = move |_| { let submit = move |_| {
first_generation_done.0.set(true); first_generation_done.0.set(true);
action.dispatch((&state).into()) state.generate();
}; };
let step = state.step; let step = state.step;
......
use std::rc::Rc; use std::rc::Rc;
use crate::frontend::components::options::todo::Todo; use crate::frontend::components::options::todo::Todo;
use crate::frontend::pages::generator::FirstGenerationDone;
use crate::frontend::state::OptionState; use crate::frontend::state::OptionState;
use crate::{backend::routes::get_calendar, frontend::components::schedule::ScheduleComponent}; use crate::{backend::routes::get_calendar, frontend::components::schedule::ScheduleComponent};
use aep_schedule_generator::algorithm::generation::SchedulesOptions;
use aep_schedule_generator::algorithm::schedule::Schedule;
use leptos::*; use leptos::*;
#[component] #[component]
pub fn SchedulesComponent( pub fn SchedulesComponent() -> impl IntoView {
read_signal: RwSignal<Option<Vec<Schedule>>>, let state = OptionState::from_context();
action: Action<SchedulesOptions, Vec<Schedule>>, let first_generation_done: FirstGenerationDone = use_context().unwrap();
) -> impl IntoView {
let step = OptionState::from_context().step;
view! { view! {
<Await <Await
future=get_calendar future=get_calendar
children=move |calendar| { children=move |calendar| {
match (read_signal.get(), step.get() == 5) { match state.step.get() == 5 && first_generation_done.0.get() {
(Some(_), true) => { true => {
let calendar = Rc::new(calendar.clone().unwrap()); let calendar = Rc::new(calendar.clone().unwrap());
view !{ view !{
<For <For
each=move || {read_signal.get().unwrap()} each=move || {state.schedule.get()}
key= |course| course.id key= |course| course.id
children= move |schedule| { children= move |schedule| {
let calendar = Rc::clone(&calendar); let calendar = Rc::clone(&calendar);
...@@ -32,9 +30,9 @@ pub fn SchedulesComponent( ...@@ -32,9 +30,9 @@ pub fn SchedulesComponent(
} }
/> />
} }
}, },
_ => view ! { _ => view ! {
<Todo action/> <Todo/>
} }
} }
} }
......
...@@ -2,7 +2,6 @@ use crate::frontend::components::icons::{caret_double_right::CaretDoubleRight, I ...@@ -2,7 +2,6 @@ use crate::frontend::components::icons::{caret_double_right::CaretDoubleRight, I
use crate::frontend::components::notifications::Notifications; use crate::frontend::components::notifications::Notifications;
use crate::frontend::components::{options::form::OptionsForms, schedules::SchedulesComponent}; use crate::frontend::components::{options::form::OptionsForms, schedules::SchedulesComponent};
use crate::frontend::state::OptionState; use crate::frontend::state::OptionState;
use aep_schedule_generator::algorithm::generation::SchedulesOptions;
use aep_schedule_generator::data::group_sigle::SigleGroup; use aep_schedule_generator::data::group_sigle::SigleGroup;
use leptos::*; use leptos::*;
...@@ -22,17 +21,7 @@ pub struct FirstGenerationDone(pub RwSignal<bool>); ...@@ -22,17 +21,7 @@ pub struct FirstGenerationDone(pub RwSignal<bool>);
pub fn GeneratorPage() -> impl IntoView { pub fn GeneratorPage() -> impl IntoView {
let (hide, set_hide) = create_signal(false); let (hide, set_hide) = create_signal(false);
let first_generation_done = create_rw_signal(false); let first_generation_done = create_rw_signal(false);
// Creates a reactive value to update the button
let action = create_action(move |s: &SchedulesOptions| {
let mut s = s.clone();
set_hide(true);
s.apply_personal_schedule();
async move { s.get_schedules().into_sorted_vec() }
});
let (modal, set_modal) = create_signal(None); let (modal, set_modal) = create_signal(None);
let state = OptionState::default(); let state = OptionState::default();
provide_context(state); provide_context(state);
...@@ -41,10 +30,10 @@ pub fn GeneratorPage() -> impl IntoView { ...@@ -41,10 +30,10 @@ pub fn GeneratorPage() -> impl IntoView {
view! { view! {
<aside class="left-panel" class=("hide-left-panel", hide)> <aside class="left-panel" class=("hide-left-panel", hide)>
<OptionsForms action/> <OptionsForms/>
</aside> </aside>
<section class="right-panel"> <section class="right-panel">
<SchedulesComponent action=action read_signal=action.value()/> <SchedulesComponent/>
</section> </section>
<Notifications modal set_modal/> <Notifications modal set_modal/>
<button on:click=move |_| {set_hide(false)} id="go-back"><CaretDoubleRight weight=IconWeight::Regular size="3vh"/></button> <button on:click=move |_| {set_hide(false)} id="go-back"><CaretDoubleRight weight=IconWeight::Regular size="3vh"/></button>
......
use aep_schedule_generator::{ use aep_schedule_generator::{
algorithm::{generation::SchedulesOptions, scores::EvaluationOption}, algorithm::{generation::SchedulesOptions, schedule::Schedule, scores::EvaluationOption},
data::time::week::Week, data::time::week::Week,
}; };
use leptos::*; use leptos::*;
...@@ -21,6 +21,8 @@ pub struct OptionState { ...@@ -21,6 +21,8 @@ pub struct OptionState {
pub section_error: RwSignal<String>, pub section_error: RwSignal<String>,
pub personal_error: RwSignal<String>, pub personal_error: RwSignal<String>,
pub step: RwSignal<u8>, pub step: RwSignal<u8>,
pub hide: RwSignal<bool>,
pub schedule: RwSignal<Vec<Schedule>>,
} }
impl OptionState { impl OptionState {
...@@ -63,6 +65,13 @@ impl OptionState { ...@@ -63,6 +65,13 @@ impl OptionState {
self.personal_error.set("".to_string()); self.personal_error.set("".to_string());
self.step.set(5); self.step.set(5);
} }
pub fn generate(&self) {
let mut schedule_option: SchedulesOptions = self.into();
schedule_option.apply_personal_schedule();
let schedules = schedule_option.get_schedules().into_sorted_vec();
self.schedule.set(schedules);
}
} }
impl Default for OptionState { impl Default for OptionState {
...@@ -96,6 +105,8 @@ impl Default for OptionState { ...@@ -96,6 +105,8 @@ impl Default for OptionState {
section_error: create_rw_signal("".to_string()), section_error: create_rw_signal("".to_string()),
personal_error: create_rw_signal("".to_string()), personal_error: create_rw_signal("".to_string()),
step: create_rw_signal(0), step: create_rw_signal(0),
schedule: create_rw_signal(vec![]),
hide: create_rw_signal(false),
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment