Add first day of the week feature to user settings and calendar functionality
This commit is contained in:
parent
39f5b968b4
commit
fdbf2e4f27
15 changed files with 191 additions and 42 deletions
|
|
@ -66,6 +66,7 @@ impl CalendarState {
|
|||
pub struct CalendarOptions {
|
||||
pub can_select_date: bool,
|
||||
pub with_link_and_remove: bool,
|
||||
pub first_day_of_the_week: Weekday,
|
||||
}
|
||||
|
||||
pub fn setup(
|
||||
|
|
@ -118,7 +119,10 @@ pub fn setup(
|
|||
// gloo::console::log!(event); // TODO: Remove.
|
||||
|
||||
if target.class_name() == "number" && options.can_select_date {
|
||||
let first_day = first_grid_day(state_clone.get_displayed_date());
|
||||
let first_day = first_grid_day(
|
||||
state_clone.get_displayed_date(),
|
||||
options.first_day_of_the_week,
|
||||
);
|
||||
let day_grid_id = target.parent_element().unwrap().id();
|
||||
let day_offset = day_grid_id[9..10].parse::<u64>().unwrap() * 7
|
||||
+ day_grid_id[10..11].parse::<u64>().unwrap();
|
||||
|
|
@ -212,7 +216,7 @@ fn display_month(
|
|||
}
|
||||
}
|
||||
|
||||
let first_day = first_grid_day(date);
|
||||
let first_day = first_grid_day(date, options.first_day_of_the_week);
|
||||
let mut current = first_day;
|
||||
|
||||
for i in 0..NB_CALENDAR_ROW {
|
||||
|
|
@ -302,11 +306,11 @@ fn display_month(
|
|||
});
|
||||
}
|
||||
|
||||
fn first_grid_day(mut date: NaiveDate) -> NaiveDate {
|
||||
fn first_grid_day(mut date: NaiveDate, first_day_of_the_week: Weekday) -> NaiveDate {
|
||||
while (date - Days::new(1)).month() == date.month() {
|
||||
date = date - Days::new(1);
|
||||
}
|
||||
while date.weekday() != Weekday::Mon {
|
||||
while date.weekday() != first_day_of_the_week {
|
||||
date = date - Days::new(1);
|
||||
}
|
||||
date
|
||||
|
|
|
|||
|
|
@ -44,6 +44,12 @@ pub fn main() -> Result<(), JsValue> {
|
|||
.map(|v| v == "true")
|
||||
.unwrap_or_default();
|
||||
|
||||
let first_day_of_the_week = selector::<HtmlElement>("html")
|
||||
.dataset()
|
||||
.get("userFirstDayOfTheWeek")
|
||||
.map(|v| v.parse().unwrap_or(chrono::Weekday::Mon))
|
||||
.unwrap_or(chrono::Weekday::Mon);
|
||||
|
||||
match path[..] {
|
||||
["recipe", "edit", id] => {
|
||||
let id = id.parse::<i64>().unwrap(); // TODO: remove unwrap.
|
||||
|
|
@ -51,11 +57,11 @@ pub fn main() -> Result<(), JsValue> {
|
|||
}
|
||||
["recipe", "view", id] => {
|
||||
let id = id.parse::<i64>().unwrap(); // TODO: remove unwrap.
|
||||
pages::recipe_view::setup_page(id, is_user_logged)
|
||||
pages::recipe_view::setup_page(id, is_user_logged, first_day_of_the_week)
|
||||
}
|
||||
["dev_panel"] => pages::dev_panel::setup_page(),
|
||||
// Home.
|
||||
[""] => pages::home::setup_page(is_user_logged),
|
||||
[""] => pages::home::setup_page(is_user_logged, first_day_of_the_week),
|
||||
_ => log!("Path unknown: ", location),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use chrono::Weekday;
|
||||
use gloo::events::EventListener;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
|
|
@ -10,7 +11,7 @@ use crate::{
|
|||
utils::{SelectorExt, by_id, get_current_lang, get_locale, selector},
|
||||
};
|
||||
|
||||
pub fn setup_page(is_user_logged: bool) {
|
||||
pub fn setup_page(is_user_logged: bool, first_day_of_the_week: Weekday) {
|
||||
let recipe_scheduler = RecipeScheduler::new(!is_user_logged);
|
||||
|
||||
calendar::setup(
|
||||
|
|
@ -18,6 +19,7 @@ pub fn setup_page(is_user_logged: bool) {
|
|||
calendar::CalendarOptions {
|
||||
can_select_date: false,
|
||||
with_link_and_remove: true,
|
||||
first_day_of_the_week,
|
||||
},
|
||||
recipe_scheduler,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
use chrono::Weekday;
|
||||
use common::utils::substitute_with_names;
|
||||
use gloo::events::EventListener;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use web_sys::{Element, HtmlInputElement};
|
||||
use web_sys::{Element, HtmlElement, HtmlInputElement};
|
||||
|
||||
use crate::{
|
||||
calendar, modal_dialog,
|
||||
|
|
@ -10,7 +11,7 @@ use crate::{
|
|||
utils::{SelectorExt, get_locale, selector},
|
||||
};
|
||||
|
||||
pub fn setup_page(recipe_id: i64, is_user_logged: bool) {
|
||||
pub fn setup_page(recipe_id: i64, is_user_logged: bool, first_day_of_the_week: Weekday) {
|
||||
let recipe_scheduler = RecipeScheduler::new(!is_user_logged);
|
||||
|
||||
let add_to_planner: Element = selector("#recipe-view .add-to-planner");
|
||||
|
|
@ -26,6 +27,7 @@ pub fn setup_page(recipe_id: i64, is_user_logged: bool) {
|
|||
calendar::CalendarOptions {
|
||||
can_select_date: true,
|
||||
with_link_and_remove: false,
|
||||
first_day_of_the_week,
|
||||
},
|
||||
recipe_scheduler,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue