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::{
pages::generator::FirstGenerationDone,
state::OptionState,
};
use aep_schedule_generator::algorithm::{generation::SchedulesOptions, schedule::Schedule};
use leptos::*;
#[component]
pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl IntoView {
pub fn OptionsForms() -> impl IntoView {
let state = OptionState::from_context();
let first_generation_done: FirstGenerationDone = use_context().unwrap();
......@@ -19,7 +18,7 @@ pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl Int
if !first_generation_done.0.get() || state.step.get() != 5 {
return;
}
action.dispatch((&state).into());
state.generate();
};
create_local_resource(state.action_courses.pending(), move |_| {
......@@ -27,7 +26,7 @@ pub fn OptionsForms(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl Int
async move {}
});
let submit_mobile = move |_| action.dispatch((&state).into());
let submit_mobile = move |_| state.generate();
view! {
<CoursesSelector state=state submit/>
......
use std::cmp::Ordering;
use aep_schedule_generator::algorithm::{generation::SchedulesOptions, schedule::Schedule};
use leptos::*;
use crate::frontend::{pages::generator::FirstGenerationDone, state::OptionState};
......@@ -46,13 +45,13 @@ pub fn Step(
}
#[component]
pub fn Todo(action: Action<SchedulesOptions, Vec<Schedule>>) -> impl IntoView {
pub fn Todo() -> impl IntoView {
let state = OptionState::from_context();
let first_generation_done: FirstGenerationDone = use_context().unwrap();
let submit = move |_| {
first_generation_done.0.set(true);
action.dispatch((&state).into())
state.generate();
};
let step = state.step;
......
use std::rc::Rc;
use crate::frontend::components::options::todo::Todo;
use crate::frontend::pages::generator::FirstGenerationDone;
use crate::frontend::state::OptionState;
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::*;
#[component]
pub fn SchedulesComponent(
read_signal: RwSignal<Option<Vec<Schedule>>>,
action: Action<SchedulesOptions, Vec<Schedule>>,
) -> impl IntoView {
let step = OptionState::from_context().step;
pub fn SchedulesComponent() -> impl IntoView {
let state = OptionState::from_context();
let first_generation_done: FirstGenerationDone = use_context().unwrap();
view! {
<Await
future=get_calendar
children=move |calendar| {
match (read_signal.get(), step.get() == 5) {
(Some(_), true) => {
match state.step.get() == 5 && first_generation_done.0.get() {
true => {
let calendar = Rc::new(calendar.clone().unwrap());
view !{
<For
each=move || {read_signal.get().unwrap()}
each=move || {state.schedule.get()}
key= |course| course.id
children= move |schedule| {
let calendar = Rc::clone(&calendar);
......@@ -32,9 +30,9 @@ pub fn SchedulesComponent(
}
/>
}
},
},
_ => view ! {
<Todo action/>
<Todo/>
}
}
}
......
......@@ -2,7 +2,6 @@ use crate::frontend::components::icons::{caret_double_right::CaretDoubleRight, I
use crate::frontend::components::notifications::Notifications;
use crate::frontend::components::{options::form::OptionsForms, schedules::SchedulesComponent};
use crate::frontend::state::OptionState;
use aep_schedule_generator::algorithm::generation::SchedulesOptions;
use aep_schedule_generator::data::group_sigle::SigleGroup;
use leptos::*;
......@@ -22,17 +21,7 @@ pub struct FirstGenerationDone(pub RwSignal<bool>);
pub fn GeneratorPage() -> impl IntoView {
let (hide, set_hide) = create_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 state = OptionState::default();
provide_context(state);
......@@ -41,10 +30,10 @@ pub fn GeneratorPage() -> impl IntoView {
view! {
<aside class="left-panel" class=("hide-left-panel", hide)>
<OptionsForms action/>
<OptionsForms/>
</aside>
<section class="right-panel">
<SchedulesComponent action=action read_signal=action.value()/>
<SchedulesComponent/>
</section>
<Notifications modal set_modal/>
<button on:click=move |_| {set_hide(false)} id="go-back"><CaretDoubleRight weight=IconWeight::Regular size="3vh"/></button>
......
use aep_schedule_generator::{
algorithm::{generation::SchedulesOptions, scores::EvaluationOption},
algorithm::{generation::SchedulesOptions, schedule::Schedule, scores::EvaluationOption},
data::time::week::Week,
};
use leptos::*;
......@@ -21,6 +21,8 @@ pub struct OptionState {
pub section_error: RwSignal<String>,
pub personal_error: RwSignal<String>,
pub step: RwSignal<u8>,
pub hide: RwSignal<bool>,
pub schedule: RwSignal<Vec<Schedule>>,
}
impl OptionState {
......@@ -63,6 +65,13 @@ impl OptionState {
self.personal_error.set("".to_string());
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 {
......@@ -96,6 +105,8 @@ impl Default for OptionState {
section_error: create_rw_signal("".to_string()),
personal_error: create_rw_signal("".to_string()),
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