Skip to content

Commit 3c43331

Browse files
committed
fix load id bug
1 parent c12bcaf commit 3c43331

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/ui/lifetime_view.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,40 +169,51 @@ pub fn get_svg_content() -> Option<String> {
169169
let size_info = size_info();
170170
Some(generate_svg_content(&yaml, &size_info))
171171
}
172-
173172
fn get_color_and_period_for_date(
174173
date: NaiveDate,
175174
life_periods: &[LifePeriod],
176175
current_date: NaiveDate,
177176
) -> (String, Option<LifePeriod>) {
178177
debug!("Searching for period for date: {}", date);
179178

180-
for (index, period) in life_periods.iter().rev().enumerate() {
181-
let period_start = parse_date(
182-
&period.start,
183-
&format!("Failed to parse start date for period: {}", period.start),
179+
// Return default if no periods
180+
if life_periods.is_empty() {
181+
return ("#fafafa".to_string(), None);
182+
}
183+
184+
// Sort periods by start date
185+
let mut periods = life_periods.to_vec();
186+
periods.sort_by(|a, b| a.start.cmp(&b.start));
187+
188+
// Iterate through consecutive periods
189+
for i in 0..periods.len() {
190+
let current_period = &periods[i];
191+
let current_start = parse_date(
192+
&current_period.start,
193+
&format!("Failed to parse start date for period: {}", current_period.start),
184194
);
185-
let period_end = if index == 0 {
195+
196+
let period_end = if i == periods.len() - 1 {
197+
// Last period extends to current date
186198
current_date
187199
} else {
200+
// End of period is start of next period
188201
parse_date(
189-
&life_periods[life_periods.len() - index].start,
190-
&format!(
191-
"Failed to parse start date for next period: {}",
192-
life_periods[life_periods.len() - index].start
193-
),
202+
&periods[i + 1].start,
203+
&format!("Failed to parse start date for next period: {}", periods[i + 1].start),
194204
)
195205
};
196206

197-
if date >= period_start && date < period_end {
207+
if date >= current_start && date < period_end {
198208
debug!(
199209
"Found matching period: start={}, end={}, color={}",
200-
period_start, period_end, period.color
210+
current_start, period_end, current_period.color
201211
);
202-
return (period.color.clone(), Some(period.clone()));
212+
return (current_period.color.clone(), Some(current_period.clone()));
203213
}
204214
}
205215

216+
// No matching period found
206217
("#fafafa".to_string(), None)
207218
}
208219

src/yaml_manager.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,28 @@ impl YamlManager for NativeYamlManager {
126126
Err("File save cancelled".to_string())
127127
}
128128
}
129-
130129
fn update_yaml(&self, yaml: &Yaml, yaml_file: &str) -> Result<(), String> {
131130
let mut yaml_to_save = yaml.clone();
132-
133-
// Remove IDs before saving
131+
132+
// Remove IDs before saving by setting them to None
134133
for period in &mut yaml_to_save.life_periods {
135-
period.id = Some(Uuid::nil());
134+
period.id = None; // Set to None instead of nil UUID
136135
for event in &mut period.events {
137-
event.id = Some(Uuid::nil());
136+
event.id = None; // Set to None instead of nil UUID
138137
}
139138
}
140-
139+
141140
let yaml_content = serde_yaml::to_string(&yaml_to_save)
142141
.map_err(|e| format!("Failed to serialize YAML: {:?}", e))?;
143-
142+
144143
let file_path = Path::new(&self.data_folder).join(yaml_file);
145144
fs::create_dir_all(file_path.parent().unwrap())
146145
.map_err(|e| format!("Failed to create directory: {:?}", e))?;
147-
146+
148147
fs::write(file_path, yaml_content)
149148
.map_err(|e| format!("Failed to update YAML file: {:?}", e))
150149
}
151-
150+
152151
fn get_available_yamls(&self) -> Result<Vec<String>, String> {
153152
let data_folder = Path::new(&self.data_folder);
154153
let yamls = fs::read_dir(data_folder)
@@ -264,17 +263,23 @@ impl YamlManager for WasmYamlManager {
264263
}
265264

266265
fn update_yaml(&self, yaml: &Yaml, yaml_file: &str) -> Result<(), String> {
267-
let yaml_content = serde_yaml::to_string(yaml)
266+
let mut yaml_to_save = yaml.clone();
267+
268+
// Remove IDs before saving
269+
for period in &mut yaml_to_save.life_periods {
270+
period.id = None; // Clear period ID
271+
for event in &mut period.events {
272+
event.id = None; // Clear event IDs
273+
}
274+
}
275+
276+
let yaml_content = serde_yaml::to_string(&yaml_to_save)
268277
.map_err(|e| format!("Failed to serialize YAML: {:?}", e))?;
269-
let window = web_sys::window().ok_or_else(|| "Failed to get window".to_string())?;
270-
let storage = window
271-
.local_storage()
272-
.map_err(|_| "Failed to get localStorage".to_string())?
273-
.ok_or_else(|| "localStorage not available".to_string())?;
274-
275-
storage
276-
.set_item(yaml_file, &yaml_content)
277-
.map_err(|_| "Failed to set item in localStorage".to_string())
278+
279+
// Save the YAML without IDs
280+
let file_path = Path::new(&self.data_folder).join(yaml_file);
281+
fs::write(file_path, yaml_content)
282+
.map_err(|e| format!("Failed to update YAML file: {:?}", e))
278283
}
279284

280285
fn get_available_yamls(&self) -> Result<Vec<String>, String> {

0 commit comments

Comments
 (0)