From 3c43331474ba95303ae8c03f7e9e8c335c568264 Mon Sep 17 00:00:00 2001 From: WaoziXyz Date: Thu, 12 Dec 2024 23:07:34 -0500 Subject: [PATCH] fix load id bug --- src/ui/lifetime_view.rs | 39 +++++++++++++++++++++++-------------- src/yaml_manager.rs | 43 +++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/ui/lifetime_view.rs b/src/ui/lifetime_view.rs index 949d53e..56a7b9c 100644 --- a/src/ui/lifetime_view.rs +++ b/src/ui/lifetime_view.rs @@ -169,7 +169,6 @@ pub fn get_svg_content() -> Option { let size_info = size_info(); Some(generate_svg_content(&yaml, &size_info)) } - fn get_color_and_period_for_date( date: NaiveDate, life_periods: &[LifePeriod], @@ -177,32 +176,44 @@ fn get_color_and_period_for_date( ) -> (String, Option) { debug!("Searching for period for date: {}", date); - for (index, period) in life_periods.iter().rev().enumerate() { - let period_start = parse_date( - &period.start, - &format!("Failed to parse start date for period: {}", period.start), + // Return default if no periods + if life_periods.is_empty() { + return ("#fafafa".to_string(), None); + } + + // Sort periods by start date + let mut periods = life_periods.to_vec(); + periods.sort_by(|a, b| a.start.cmp(&b.start)); + + // Iterate through consecutive periods + for i in 0..periods.len() { + let current_period = &periods[i]; + let current_start = parse_date( + ¤t_period.start, + &format!("Failed to parse start date for period: {}", current_period.start), ); - let period_end = if index == 0 { + + let period_end = if i == periods.len() - 1 { + // Last period extends to current date current_date } else { + // End of period is start of next period parse_date( - &life_periods[life_periods.len() - index].start, - &format!( - "Failed to parse start date for next period: {}", - life_periods[life_periods.len() - index].start - ), + &periods[i + 1].start, + &format!("Failed to parse start date for next period: {}", periods[i + 1].start), ) }; - if date >= period_start && date < period_end { + if date >= current_start && date < period_end { debug!( "Found matching period: start={}, end={}, color={}", - period_start, period_end, period.color + current_start, period_end, current_period.color ); - return (period.color.clone(), Some(period.clone())); + return (current_period.color.clone(), Some(current_period.clone())); } } + // No matching period found ("#fafafa".to_string(), None) } diff --git a/src/yaml_manager.rs b/src/yaml_manager.rs index 07de583..a61f7b2 100644 --- a/src/yaml_manager.rs +++ b/src/yaml_manager.rs @@ -126,29 +126,28 @@ impl YamlManager for NativeYamlManager { Err("File save cancelled".to_string()) } } - fn update_yaml(&self, yaml: &Yaml, yaml_file: &str) -> Result<(), String> { let mut yaml_to_save = yaml.clone(); - - // Remove IDs before saving + + // Remove IDs before saving by setting them to None for period in &mut yaml_to_save.life_periods { - period.id = Some(Uuid::nil()); + period.id = None; // Set to None instead of nil UUID for event in &mut period.events { - event.id = Some(Uuid::nil()); + event.id = None; // Set to None instead of nil UUID } } - + let yaml_content = serde_yaml::to_string(&yaml_to_save) .map_err(|e| format!("Failed to serialize YAML: {:?}", e))?; - + let file_path = Path::new(&self.data_folder).join(yaml_file); fs::create_dir_all(file_path.parent().unwrap()) .map_err(|e| format!("Failed to create directory: {:?}", e))?; - + fs::write(file_path, yaml_content) .map_err(|e| format!("Failed to update YAML file: {:?}", e)) } - + fn get_available_yamls(&self) -> Result, String> { let data_folder = Path::new(&self.data_folder); let yamls = fs::read_dir(data_folder) @@ -264,17 +263,23 @@ impl YamlManager for WasmYamlManager { } fn update_yaml(&self, yaml: &Yaml, yaml_file: &str) -> Result<(), String> { - let yaml_content = serde_yaml::to_string(yaml) + let mut yaml_to_save = yaml.clone(); + + // Remove IDs before saving + for period in &mut yaml_to_save.life_periods { + period.id = None; // Clear period ID + for event in &mut period.events { + event.id = None; // Clear event IDs + } + } + + let yaml_content = serde_yaml::to_string(&yaml_to_save) .map_err(|e| format!("Failed to serialize YAML: {:?}", e))?; - let window = web_sys::window().ok_or_else(|| "Failed to get window".to_string())?; - let storage = window - .local_storage() - .map_err(|_| "Failed to get localStorage".to_string())? - .ok_or_else(|| "localStorage not available".to_string())?; - - storage - .set_item(yaml_file, &yaml_content) - .map_err(|_| "Failed to set item in localStorage".to_string()) + + // Save the YAML without IDs + let file_path = Path::new(&self.data_folder).join(yaml_file); + fs::write(file_path, yaml_content) + .map_err(|e| format!("Failed to update YAML file: {:?}", e)) } fn get_available_yamls(&self) -> Result, String> {