Skip to content
This repository has been archived by the owner on Jan 5, 2025. It is now read-only.

Commit

Permalink
fix load id bug
Browse files Browse the repository at this point in the history
  • Loading branch information
waozixyz committed Dec 13, 2024
1 parent c12bcaf commit 3c43331
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
39 changes: 25 additions & 14 deletions src/ui/lifetime_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,40 +169,51 @@ pub fn get_svg_content() -> Option<String> {
let size_info = size_info();
Some(generate_svg_content(&yaml, &size_info))
}

fn get_color_and_period_for_date(
date: NaiveDate,
life_periods: &[LifePeriod],
current_date: NaiveDate,
) -> (String, Option<LifePeriod>) {
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(
&current_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)
}

Expand Down
43 changes: 24 additions & 19 deletions src/yaml_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>, String> {
let data_folder = Path::new(&self.data_folder);
let yamls = fs::read_dir(data_folder)
Expand Down Expand Up @@ -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<Vec<String>, String> {
Expand Down

0 comments on commit 3c43331

Please sign in to comment.