1
- use crate :: models:: timeline:: { LifePeriod , LifePeriodEvent , MyLifeApp , Yaml } ;
2
1
use crate :: managers:: timeline_manager:: get_timeline_manager;
2
+ use crate :: models:: timeline:: { LifePeriod , LifePeriodEvent , MyLifeApp , Yaml } ;
3
3
use crate :: utils:: date_utils:: is_valid_date;
4
4
use chrono:: NaiveDate ;
5
5
use dioxus:: prelude:: * ;
@@ -10,15 +10,15 @@ fn is_valid_hex_color(color: &str) -> bool {
10
10
color. len ( ) == 7 && color. starts_with ( '#' ) && color[ 1 ..] . chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) )
11
11
}
12
12
13
-
14
13
#[ component]
15
14
pub fn EditLegendItem ( ) -> Element {
16
15
let mut app_state = use_context :: < Signal < MyLifeApp > > ( ) ;
17
16
let mut yaml_state = use_context :: < Signal < Yaml > > ( ) ;
18
17
let mut color_input = use_signal ( String :: new) ;
19
18
let mut date_error = use_signal ( String :: new) ;
20
19
let mut current_date = use_signal ( String :: new) ;
21
- let mut pending_update = use_signal ( || None :: < ( Option < LifePeriod > , Option < ( Uuid , LifePeriodEvent ) > ) > ) ;
20
+ let mut pending_update =
21
+ use_signal ( || None :: < ( Option < LifePeriod > , Option < ( Uuid , LifePeriodEvent ) > ) > ) ;
22
22
23
23
let ( min_date, max_date) = use_memo ( move || {
24
24
if let Some ( item) = & app_state ( ) . item_state {
@@ -51,7 +51,7 @@ pub fn EditLegendItem() -> Element {
51
51
} ) ( ) ;
52
52
use_effect ( move || {
53
53
to_owned ! [ pending_update, yaml_state] ;
54
-
54
+
55
55
spawn ( async move {
56
56
if let Some ( ( period, event) ) = pending_update ( ) {
57
57
// Handle period updates
@@ -61,50 +61,48 @@ pub fn EditLegendItem() -> Element {
61
61
} else {
62
62
get_timeline_manager ( ) . add_life_period ( period) . await
63
63
} ;
64
-
64
+
65
65
if let Err ( e) = result {
66
66
warn ! ( "Failed to update/add life period: {}" , e) ;
67
67
}
68
68
}
69
-
69
+
70
70
// Handle event updates
71
71
if let Some ( ( period_id, event) ) = event {
72
72
let result = if event. id . is_some ( ) {
73
73
get_timeline_manager ( ) . update_event ( period_id, event) . await
74
74
} else {
75
75
get_timeline_manager ( ) . add_event ( period_id, event) . await
76
76
} ;
77
-
77
+
78
78
if let Err ( e) = result {
79
79
warn ! ( "Failed to update/add event: {}" , e) ;
80
80
}
81
81
}
82
-
82
+
83
83
// Update the entire timeline
84
- if let Err ( e) = get_timeline_manager ( )
85
- . update_timeline ( & yaml_state ( ) )
86
- . await
87
- {
84
+ if let Err ( e) = get_timeline_manager ( ) . update_timeline ( & yaml_state ( ) ) . await {
88
85
warn ! ( "Failed to update timeline: {}" , e) ;
89
86
}
90
87
}
91
88
} ) ;
92
89
} ) ;
93
-
90
+
94
91
let update_yaml_item = move |_| {
95
- debug ! ( "Attempting to update yaml item" ) ;
96
92
if date_error ( ) . is_empty ( ) {
97
93
if let Some ( item) = app_state ( ) . item_state {
98
94
let new_yaml = yaml_state ( ) ;
99
-
95
+
100
96
if item. is_event {
101
97
let mut new_yaml_event = new_yaml. clone ( ) ;
102
98
if let Some ( period) = new_yaml_event
103
99
. life_periods
104
100
. iter_mut ( )
105
101
. find ( |p| p. id == Some ( app_state ( ) . selected_life_period . unwrap ( ) ) )
106
102
{
107
- let event = if let Some ( event) = period. events . iter_mut ( ) . find ( |e| e. id == Some ( item. id ) ) {
103
+ let event = if let Some ( event) =
104
+ period. events . iter_mut ( ) . find ( |e| e. id == Some ( item. id ) )
105
+ {
108
106
event. name = item. name . clone ( ) ;
109
107
event. color = item. color . clone ( ) ;
110
108
event. start = item. start . clone ( ) ;
@@ -120,9 +118,12 @@ pub fn EditLegendItem() -> Element {
120
118
new_event
121
119
} ;
122
120
period. events . sort_by ( |a, b| a. start . cmp ( & b. start ) ) ;
123
-
121
+
124
122
// Queue the update
125
- pending_update. set ( Some ( ( None , Some ( ( app_state ( ) . selected_life_period . unwrap ( ) , event) ) ) ) ) ;
123
+ pending_update. set ( Some ( (
124
+ None ,
125
+ Some ( ( app_state ( ) . selected_life_period . unwrap ( ) , event) ) ,
126
+ ) ) ) ;
126
127
}
127
128
yaml_state. set ( new_yaml_event) ;
128
129
} else {
@@ -147,8 +148,10 @@ pub fn EditLegendItem() -> Element {
147
148
new_yaml_period. life_periods . push ( new_period. clone ( ) ) ;
148
149
new_period
149
150
} ;
150
- new_yaml_period. life_periods . sort_by ( |a, b| a. start . cmp ( & b. start ) ) ;
151
-
151
+ new_yaml_period
152
+ . life_periods
153
+ . sort_by ( |a, b| a. start . cmp ( & b. start ) ) ;
154
+
152
155
// Queue the update
153
156
pending_update. set ( Some ( ( Some ( period) , None ) ) ) ;
154
157
yaml_state. set ( new_yaml_period) ;
@@ -159,7 +162,6 @@ pub fn EditLegendItem() -> Element {
159
162
}
160
163
} ;
161
164
162
-
163
165
let close_modal = move |_| {
164
166
app_state. write ( ) . item_state = None ;
165
167
app_state. write ( ) . temp_start_date = String :: new ( ) ;
@@ -177,7 +179,6 @@ pub fn EditLegendItem() -> Element {
177
179
178
180
let update_date = move |evt : Event < FormData > | {
179
181
let new_date = evt. value ( ) . to_string ( ) ;
180
- debug ! ( "Updating date: {}" , new_date) ;
181
182
current_date. set ( new_date. clone ( ) ) ;
182
183
183
184
let is_event = app_state ( )
@@ -229,7 +230,6 @@ pub fn EditLegendItem() -> Element {
229
230
}
230
231
} ;
231
232
232
-
233
233
let delete_item = move |_| {
234
234
if let Some ( item) = app_state ( ) . item_state {
235
235
let mut new_yaml = yaml_state ( ) ;
@@ -240,11 +240,11 @@ pub fn EditLegendItem() -> Element {
240
240
. find ( |p| p. id == Some ( app_state ( ) . selected_life_period . unwrap ( ) ) )
241
241
{
242
242
period. events . retain ( |e| e. id != Some ( item. id ) ) ;
243
-
243
+
244
244
// Clone required data for the async operation
245
245
let period_id = app_state ( ) . selected_life_period . unwrap ( ) ;
246
246
let event_id = item. id ;
247
-
247
+
248
248
use_future ( move || async move {
249
249
if let Err ( e) = get_timeline_manager ( )
250
250
. delete_event ( period_id, event_id)
@@ -256,15 +256,12 @@ pub fn EditLegendItem() -> Element {
256
256
}
257
257
} else {
258
258
new_yaml. life_periods . retain ( |p| p. id != Some ( item. id ) ) ;
259
-
259
+
260
260
// Clone required data for the async operation
261
261
let period_id = item. id ;
262
-
262
+
263
263
use_future ( move || async move {
264
- if let Err ( e) = get_timeline_manager ( )
265
- . delete_life_period ( period_id)
266
- . await
267
- {
264
+ if let Err ( e) = get_timeline_manager ( ) . delete_life_period ( period_id) . await {
268
265
warn ! ( "Failed to delete life period: {}" , e) ;
269
266
}
270
267
} ) ;
0 commit comments