From 6c47403b11e7aaf1a22778bdc7615051779cb7bd Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 14:50:45 +0100 Subject: Allow to delete events --- src/app/form.rs | 23 +++++++++++++++++++++-- src/app/update.rs | 10 ++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'src/app') diff --git a/src/app/form.rs b/src/app/form.rs index 6c42cd0..7f75db0 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -46,11 +46,14 @@ pub async fn show(app: &App, event: Event, is_new: bool) { vbox.append(&label("Fin")); vbox.append(&end); - let button = gtk::Button::with_label("Créer"); + let button = gtk::Button::builder() + .label(if is_new { "Créer" } else { "Modifier" }) + .margin_bottom(10) + .build(); vbox.append(&button); let conn = app.conn.clone(); let tx = app.tx.clone(); - button.connect_clicked(glib::clone!(@weak dialog => move |_| { + button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| { match event::validate(event.id, date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { Some(new) => { match if is_new { db::insert(&conn, &new) } else { db::update(&conn, &new) } { @@ -66,6 +69,22 @@ pub async fn show(app: &App, event: Event, is_new: bool) { } })); + if !is_new { + let button = gtk::Button::builder().label("Supprimer").build(); + vbox.append(&button); + let conn = app.conn.clone(); + let tx = app.tx.clone(); + button.connect_clicked(glib::clone!(@weak dialog => move |_| { + match db::delete(&conn, &event.id) { + Ok(_) => { + update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() }); + dialog.close() + }, + Err(_) => () + } + })); + } + dialog.run_future().await; } diff --git a/src/app/update.rs b/src/app/update.rs index f1576b5..baf4651 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -17,6 +17,7 @@ pub enum Msg { ShowUpdateForm { event: Event }, AddEvent { new: Event }, UpdateEvent { old: Event, new: Event }, + DeleteEvent { event: Event }, } pub async fn event_handler(rx: Receiver, mut app: App) { @@ -47,6 +48,15 @@ pub async fn event_handler(rx: Receiver, mut app: App) { None => println!("Event not found when updating from {:?} to {:?}", old, new), } } + Msg::DeleteEvent { event } => { + match app.events.iter().position(|e| e.id == event.id) { + Some(index) => { + app.events.remove(index); + calendar::refresh_date(&app, event.date); + } + None => println!("Event not found when trying to delete {:?}", event), + } + } } } } -- cgit v1.2.3